1
1
"""
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
+
2
17
MIT License
3
- Copyright (c) 2009 Joshua Roesslein
18
+ Copyright (c) 2009 Joshua Roesslein <jroesslein at gmail.com>
4
19
5
20
Permission is hereby granted, free of charge, to any person obtaining a copy
6
21
of this software and associated documentation files (the "Software"), to deal
22
37
"""
23
38
24
39
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
25
47
26
48
class ShortyError (Exception ):
27
49
28
50
def __init__ (self , reason ):
29
- self .reason = reason
51
+ self .reason = str ( reason )
30
52
31
53
def __str__ (self ):
32
- repr (self .reason )
54
+ return repr (self .reason )
33
55
34
56
"""Wrap urlopen to raise shorty errors instead"""
35
57
def request (* args , ** kargs ):
@@ -39,6 +61,11 @@ def request(*args, **kargs):
39
61
except URLError , e :
40
62
raise ShortyError (e )
41
63
64
+ """Build basic auth header value"""
65
+ def basic_auth (username , password ):
66
+
67
+ return 'Basic %s' % base64 .b64encode ('%s:%s' % (username , password ))
68
+
42
69
"""Base interface that all services implement."""
43
70
class Service (object ):
44
71
@@ -57,4 +84,43 @@ class Tinyurl(Service):
57
84
def shrink (bigurl ):
58
85
resp = request ('http://tinyurl.com/api-create.php?url=%s' % bigurl )
59
86
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
60
126
0 commit comments