bit.ly

Code-Stücke können hier veröffentlicht werden.
Antworten
Dav1d
User
Beiträge: 1437
Registriert: Donnerstag 30. Juli 2009, 12:03
Kontaktdaten:

http://bit.ly ist ein URL-Shortener mit ner API, für die API, hab ich das geschrieben:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
from __future__ import with_statement
import sys
if sys.version_info[0] < 3:
    from urllib2 import urlopen, Request
    from urllib import urlencode
else:
    from urllib.request import urlopen, Request
    from urllib.parse import urlencode
try:
    import json
except ImportError:
    import simplejson as json
from contextlib import closing
 
API_URL = 'http://api.bit.ly/v3/shorten'
 
class ShortError(Exception):
    def __init__(self, val):
        self.val = val
    def __str__(self):
        return repr(self.val)
 
def get_short_url(login, api_key, long_url):
    data = urlencode({'login': login, 'apiKey' : api_key,
                      'longUrl' : long_url, 'format' : 'json'})
    with closing(urlopen(Request(API_URL, data, {'User-agent' : 'Python-bitly/2.6'}))) as stream:
        parsed_response = json.load(stream)
 
    if not parsed_response['status_code'] == 200:
        raise ShortError(parsed_response['status_txt'])
 
    return parsed_response['data']['url']
 
if __name__ == '__main__':
    login = raw_input('Login?: ')
    api_key = raw_input('Api_key?: ')
    long_url = raw_input('Long url?: ')
 
    print get_short_url(login, api_key, long_url)
Um an den API-Key zu kommen muss man sich nur auf bit.ly registrieren.
Zuletzt geändert von Dav1d am Dienstag 18. Mai 2010, 17:34, insgesamt 2-mal geändert.
the more they change the more they stay the same
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

Und zusätzlich solltest du deiner Lib einen eigenen User-Agent mitgeben, gehört IMHO zum guten Ton.
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
Dav1d
User
Beiträge: 1437
Registriert: Donnerstag 30. Juli 2009, 12:03
Kontaktdaten:

Was würdes du als User-Agent benutzen?
the more they change the more they stay the same
Leonidas
Python-Forum Veteran
Beiträge: 16025
Registriert: Freitag 20. Juni 2003, 16:30
Kontaktdaten:

``NameDeinerLib/Version``

bzw. ggf.

``NameDeinerLib/Version (+url_deines_projektes)``
My god, it's full of CARs! | Leonidasvoice vs (former) Modvoice
mitsuhiko
User
Beiträge: 1790
Registriert: Donnerstag 28. Oktober 2004, 16:33
Wohnort: Graz, Steiermark - Österreich
Kontaktdaten:

BlackJack hat geschrieben:Da möchte ich mal dezent auf Bit.ly is Harmful to Your Reputation verweisen.
Das ist kein bit.ly Problem. Das ist das Problem vom TweetDeck, das einfach Links shortened, egal ob die schon gekuerzt wurden oder nicht.
TUFKAB – the user formerly known as blackbird
lunar

@mitsuhiko: Die Warnungen, die bit.ly ausgibt, sind ein Problem von bit.ly.
Dav1d
User
Beiträge: 1437
Registriert: Donnerstag 30. Juli 2009, 12:03
Kontaktdaten:

Leonidas hat geschrieben:``NameDeinerLib/Version``

bzw. ggf.

``NameDeinerLib/Version (+url_deines_projektes)``
Danke, dann änder ich das mal ;)

//Edit. Mit urllib2.urlopen ist das nicht möglich, nur mit `Request` oder `build_opener`, oder?
the more they change the more they stay the same
lunar

Du musst ein Request-Objekt verwenden, um eigene Header zu übergeben. Das Request-Objekt kannst Du dann allerdings einfach "urllib2.urlopen()" übergeben. Einen eigenen Opener musst Du dafür nicht erzeugen.
Antworten