Seite 1 von 1

bit.ly

Verfasst: Montag 17. Mai 2010, 16:48
von Dav1d
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.

Re: bit.ly

Verfasst: Montag 17. Mai 2010, 17:16
von BlackJack
Da möchte ich mal dezent auf Bit.ly is Harmful to Your Reputation verweisen.

Re: bit.ly

Verfasst: Montag 17. Mai 2010, 17:18
von Leonidas
Und zusätzlich solltest du deiner Lib einen eigenen User-Agent mitgeben, gehört IMHO zum guten Ton.

Re: bit.ly

Verfasst: Montag 17. Mai 2010, 18:17
von Dav1d
Was würdes du als User-Agent benutzen?

Re: bit.ly

Verfasst: Montag 17. Mai 2010, 19:48
von Leonidas
``NameDeinerLib/Version``

bzw. ggf.

``NameDeinerLib/Version (+url_deines_projektes)``

Re: bit.ly

Verfasst: Dienstag 18. Mai 2010, 10:29
von mitsuhiko
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.

Re: bit.ly

Verfasst: Dienstag 18. Mai 2010, 11:10
von lunar
@mitsuhiko: Die Warnungen, die bit.ly ausgibt, sind ein Problem von bit.ly.

Re: bit.ly

Verfasst: Dienstag 18. Mai 2010, 15:02
von Dav1d
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?

Re: bit.ly

Verfasst: Dienstag 18. Mai 2010, 15:13
von 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.