Skip to content

Commit cdd300b

Browse files
committed
Implemented tr.im service shrink() method.
1 parent ce6780c commit cdd300b

File tree

2 files changed

+70
-3
lines changed

2 files changed

+70
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.pyc

shorty.py

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
"""
2+
_ _
3+
___| |__ ___ _ __| |_ _ _
4+
/ __| '_ \ / _ \| '__| __| | | |
5+
\__ \ | | | (_) | | | |_| |_| |
6+
|___/_| |_|\___/|_| \__|\__, |
7+
__/ |
8+
|___/
9+
10+
Access many URL shortening services from one library.
11+
12+
Python 2.4+
13+
Versions before 2.6 require simplejson.
14+
15+
http://gitorious.org/shorty
16+
217
MIT License
3-
Copyright (c) 2009 Joshua Roesslein
18+
Copyright (c) 2009 Joshua Roesslein <jroesslein at gmail.com>
419
520
Permission is hereby granted, free of charge, to any person obtaining a copy
621
of this software and associated documentation files (the "Software"), to deal
@@ -22,14 +37,21 @@
2237
"""
2338

2439
from urllib2 import urlopen, URLError, HTTPError
40+
from urllib import urlencode
41+
import base64
42+
43+
try:
44+
import json
45+
except:
46+
import simplejson as json
2547

2648
class ShortyError(Exception):
2749

2850
def __init__(self, reason):
29-
self.reason = reason
51+
self.reason = str(reason)
3052

3153
def __str__(self):
32-
repr(self.reason)
54+
return repr(self.reason)
3355

3456
"""Wrap urlopen to raise shorty errors instead"""
3557
def request(*args, **kargs):
@@ -39,6 +61,11 @@ def request(*args, **kargs):
3961
except URLError, e:
4062
raise ShortyError(e)
4163

64+
"""Build basic auth header value"""
65+
def basic_auth(username, password):
66+
67+
return 'Basic %s' % base64.b64encode('%s:%s' % (username, password))
68+
4269
"""Base interface that all services implement."""
4370
class Service(object):
4471

@@ -57,4 +84,43 @@ class Tinyurl(Service):
5784
def shrink(bigurl):
5885
resp = request('http://tinyurl.com/api-create.php?url=%s' % bigurl)
5986
return resp.readline()
87+
88+
"""Tr.im"""
89+
class _Trim(Service):
90+
91+
def __init__(self, apikey=None, username=None, password=None):
92+
self.base_param = {}
93+
if apikey:
94+
self.base_param['api_key'] = apikey
95+
if username and password:
96+
self.base_param['Authorization'] = basic_auth(username, password)
97+
98+
def shrink(self, bigurl, custom=None, searchtags=None, privacycode=None,
99+
newtrim=False, sandbox=False):
100+
parameters = {}
101+
parameters.update(self.base_param)
102+
parameters['url'] = bigurl
103+
if custom:
104+
parameters['custom'] = custom
105+
if searchtags:
106+
parameters['searchtags'] = searchtags
107+
if privacycode:
108+
parameters['privacycode'] = privacycode
109+
if newtrim:
110+
parameters['newtrim'] = '1'
111+
if sandbox:
112+
parameters['sandbox'] = '1'
113+
url = 'http://api.tr.im/api/trim_url.json?%s' % urlencode(parameters)
114+
resp = request(url)
115+
jdata = json.loads(resp.read())
116+
self.status = (int(jdata['status']['code']), str(jdata['status']['message']))
117+
if not 200 <= self.status[0] < 300:
118+
raise ShortyError(self.status[1])
119+
self.trimpath = str(jdata['trimpath'])
120+
self.reference = str(jdata['reference'])
121+
self.destination = str(jdata['destination'])
122+
self.domain = str(jdata['domain'])
123+
return str(jdata['url'])
124+
125+
Trim = _Trim() # non-authenticated, no apikey instance
60126

0 commit comments

Comments
 (0)