]> jfr.im git - irc/rizon/acid.git/blame - pyva/pyva/src/main/python/internets/api/urls.py
Split pyva plugin into pyva.core and pyva.pyva
[irc/rizon/acid.git] / pyva / pyva / src / main / python / internets / api / urls.py
CommitLineData
685e346e
A
1import urllib
2import httplib
3from feed import get_json, FeedError
4
5class Urls(object):
6 def __init__(self, bitly_user, bitly_key):
7 self.bitly_user = bitly_user
8 self.bitly_key = bitly_key
9
10 def shorten(self, long_url):
11 url = 'http://api.bitly.com/v3/shorten?login=%s&apiKey=%s&format=json&' % (self.bitly_user, self.bitly_key)
12 url += urllib.urlencode({'longUrl': long_url})
13 reply = get_json(url)
14 return reply
15
16 def expand(self, short_url):
17 url = 'http://api.longurl.org/v2/expand?'
18 url += urllib.urlencode({
19 'url': short_url,
20# 'all-redirects': 1,
21 'content-type': 1,
22 'title': 1,
23 'format': 'json'
24 })
25 try:
26 reply = get_json(url)
27 errmsg = reply['messages'][0]['message']
28 return {'error': errmsg}
29 except KeyError as ke:
30 return reply
31 except FeedError as fe:
32 if fe.code == 400:
33 url = 'http://longurlplease.appspot.com/api/v1.1?'
34 url += urllib.urlencode({
35 'q': short_url})
36 reply = get_json(url)
37 result = reply.get(short_url, None)
38 if not result:
39 return {'error': 'invalid shortened URL'}
40 else:
41 return {'long-url': result, 'content-type': 'N/A'}
42 elif fe.code == 504:
43 return {'error': 'connection timed out'}