Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7cf2944

Browse files
committed
Move services into their own modules.
1 parent601463e commit7cf2944

File tree

14 files changed

+418
-0
lines changed

14 files changed

+418
-0
lines changed

‎services/agd.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# a.gd
6+
classAgd(Service):
7+
8+
defshrink(self,bigurl,tag=None,password=None,expires=None):
9+
post_param= {'url':bigurl}
10+
iftag:
11+
post_param['tag']=tag
12+
ifpassword:
13+
post_param['pass']=password
14+
ifexpires:
15+
post_param['validTill']=expires
16+
resp=request('http://a.gd/?module=ShortURL&file=Add&mode=API',
17+
post_data=urlencode(post_param))
18+
url=resp.read()
19+
ifurl.startswith('http://'):
20+
returnurl
21+
else:
22+
raiseShortyError(url[url.find('>')+1:url.rfind('<')])
23+

‎services/bitly.py‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# bit.ly
6+
classBitly(Service):
7+
8+
version='2.0.1'
9+
10+
def__init__(self,login=None,apikey=None,password=None):
11+
self.login=login
12+
self.apikey=apikey
13+
self.password=password
14+
15+
def_setup(self):
16+
parameters= {'version':self.version}
17+
ifself.apikey:
18+
parameters['apiKey']=self.apikey
19+
parameters['login']=self.login
20+
username_pass=None
21+
elifself.password:
22+
username_pass= (self.login,self.password)
23+
else:
24+
raiseShortyError('Must set an apikey or password')
25+
returnparameters,username_pass
26+
27+
defshrink(self,bigurl):
28+
ifnotself.login:
29+
raiseShortyError('Must set a login')
30+
parameters,username_pass=self._setup()
31+
parameters['longUrl']=bigurl
32+
resp=request('http://api.bit.ly/shorten',parameters,username_pass)
33+
jdata=json.loads(resp.read())
34+
ifjdata['errorCode']!=0:
35+
raiseShortyError(jdata['errorMessage'])
36+
returnstr(jdata['results'][bigurl]['shortUrl'])
37+
38+
defexpand(self,tinyurl):
39+
ifnotself.login:
40+
returnget_redirect(tinyurl)
41+
parameters,username_pass=self._setup()
42+
parameters['shortUrl']=tinyurl
43+
resp=request('http://api.bit.ly/expand',parameters,username_pass)
44+
jdata=json.loads(resp.read())
45+
ifjdata['errorCode']!=0:
46+
raiseShortyError(jdata['errorMessage'])
47+
returnstr(jdata['results'].values()[0]['longUrl'])
48+

‎services/bukme.py‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# buk.me
6+
classBukme(Service):
7+
8+
def_process(self,resp):
9+
# bukme returns some markup after the url, so chop it off
10+
url=resp[:resp.find('<')]
11+
ifurl.startswith('http://'):
12+
returnurl
13+
else:
14+
raiseShortyError(url)
15+
16+
defshrink(self,bigurl,tag=None):
17+
parameters= {'url':bigurl}
18+
iftag:
19+
parameters['buk']=tag
20+
resp=request('http://buk.me/api.php',parameters)
21+
returnself._process(resp.read())
22+
23+
defexpand(self,tinyurl):
24+
resp=request('http://buk.me/api.php', {'rev':tinyurl})
25+
returnself._process(resp.read())
26+

‎services/chilpit.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# chilp.it
6+
classChilpit(Service):
7+
8+
defshrink(self,bigurl):
9+
resp=request('http://chilp.it/api.php', {'url':bigurl})
10+
url=resp.read()
11+
ifurl.startswith('http://'):
12+
returnurl
13+
else:
14+
raiseShortyError(url)
15+
16+
defexpand(self,tinyurl):
17+
turl=urlparse(tinyurl)
18+
ifturl.netloc.lstrip('www.')!='chilp.it':
19+
raiseShortyError('Not a chilp.it url')
20+
resp=request('http://p.chilp.it/api.php?'+turl.path.strip('/'))
21+
url=resp.read()
22+
ifurl.startswith('http://'):
23+
returnurl
24+
else:
25+
raiseShortyError(url)
26+
27+
# get click stats of the tinyurl
28+
defstats(self,tinyurl):
29+
turl=urlparse(tinyurl)
30+
ifturl.netloc.lstrip('www.')!='chilp.it':
31+
raiseShortyError('Not a chilp.it url')
32+
resp=request('http://s.chilp.it/api.php?'+turl.query)
33+
hit_count=resp.read()
34+
try:
35+
returnint(hit_count)
36+
except:
37+
raiseShortyError('Url not found or invalid')
38+

‎services/cligs.py‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
4+
# cli.gs
5+
classCligs(Service):
6+
7+
def__init__(self,apikey=None,appid=None):
8+
self.apikey=apikey
9+
self.appid=appid
10+
11+
defshrink(self,bigurl,title=None):
12+
parameters= {'url':bigurl}
13+
iftitle:
14+
parameters['title']=title
15+
ifself.apikey:
16+
parameters['key']=self.apikey
17+
ifself.appid:
18+
parameters['appid']=self.appid
19+
resp=request('http://cli.gs/api/v1/cligs/create',parameters)
20+
returnresp.read()
21+
22+
defexpand(self,tinyurl):
23+
# TODO: debug this some more, not working properly
24+
'''resp = request('http://cli.gs/api/v1/cligs/expand', {'clig': tinyurl})
25+
return resp.read()'''
26+
returnget_redirect(tinyurl)
27+

‎services/digg.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# digg
6+
classDigg(Service):
7+
8+
def__init__(self,appkey=None):
9+
self.appkey=appkey
10+
self.itemid=None
11+
self.view_count=None
12+
13+
defshrink(self,bigurl):
14+
ifnotself.appkey:
15+
raiseShortyError('Must set an appkey')
16+
resp=request('http://services.digg.com/url/short/create',
17+
{'url':bigurl,'appkey':self.appkey,'type':'json'})
18+
jdata=json.loads(resp.read())['shorturls'][0]
19+
self.itemid=jdata['itemid']
20+
self.view_count=jdata['view_count']
21+
returnstr(jdata['short_url'])
22+
23+
defexpand(self,tinyurl):
24+
ifself.appkey:
25+
turl=urlparse(tinyurl)
26+
ifturl.netloc!='digg.com'andturl.netloc!='www.digg.com':
27+
raiseShortyError('Not a valid digg url')
28+
resp=request('http://services.digg.com/url/short/%s'%quote(
29+
turl.path.strip('/')),
30+
{'appkey':self.appkey,'type':'json'})
31+
jdata=json.loads(resp.read())['shorturls'][0]
32+
self.itemid=jdata['itemid']
33+
self.view_count=jdata['view_count']
34+
returnstr(jdata['link'])
35+
else:
36+
self.itemid=None
37+
self.view_count=None
38+
returnget_redirect(tinyurl)
39+

‎services/fongs.py‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# fon.gs
6+
classFongs(Service):
7+
8+
defshrink(self,bigurl,tag=None):
9+
parameters= {'url':bigurl}
10+
iftag:
11+
parameters['linkname']=tag
12+
resp=request('http://fon.gs/create.php',parameters)
13+
data=resp.read()
14+
ifdata.startswith('OK:'):
15+
returndata.lstrip('OK: ')
16+
elifdata.startswith('MODIFIED:'):
17+
returndata.lstrip('MODIFIED: ')
18+
else:
19+
raiseShortyError(data)
20+
21+
# check if the given tag is taken
22+
# returns true if available false if taken
23+
defcheck(self,tag):
24+
resp=request('http://fon.gs/check.php', {'linkname':tag})
25+
data=resp.read()
26+
ifdata.startswith('AVAILABLE'):
27+
returnTrue
28+
elifdata.startswith('TAKEN'):
29+
returnFalse
30+
else:
31+
raiseShortyError(data)
32+
33+
defexpand(self,tinyurl):
34+
iftinyurl[-1]!='/':
35+
turl=tinyurl+'/'
36+
else:
37+
turl=tinyurl
38+
returnService.expand(self,turl)
39+

‎services/fwd4me.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# fwd4.me
6+
classFwd4me(Service):
7+
8+
ecodes= {
9+
'1':'Invalid long url',
10+
'2':'Invalid api key',
11+
'3':'Account suspended or revoked',
12+
'4':'Long url is black listed',
13+
'5':'Internal system error'
14+
}
15+
16+
defshrink(self,bigurl):
17+
resp=request('http://api.fwd4.me/', {'url':bigurl})
18+
data=resp.read()
19+
ifdata.startswith('ERROR:'):
20+
ecode=data.lstrip('ERROR:')
21+
raiseShortyError(Fwd4me.ecodes.get(ecode,'Unkown error'))
22+
else:
23+
returndata
24+

‎services/isgd.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
# is.gd
6+
classIsgd(Service):
7+
8+
defshrink(self,bigurl):
9+
resp=request('http://is.gd/api.php', {'longurl':bigurl})
10+
turl=resp.read()
11+
ifturl.startswith('Error:'):
12+
raiseShortyError(turl)
13+
returnturl
14+

‎services/sandbox.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Shorty
2+
## Copyright 2009 Joshua Roesslein
3+
## See LICENSE
4+
5+
classSandbox(Service):
6+
7+
def__init__(self,length=4,letters='abcdefghijklmnopqrstuvwxyz'):
8+
self.urls= {}
9+
self.letters=letters
10+
self.length=length
11+
self.base=len(letters)-1
12+
13+
defshrink(self,bigurl):
14+
# generate the tiny path
15+
tpath=''
16+
whileTrue:
17+
foriinrange(self.length):
18+
tpath+=self.letters[randint(0,self.base)]
19+
ifself.urls.has_key(tpath):
20+
# tpath already in use, regen another
21+
tpath=''
22+
else:
23+
break
24+
25+
# store url and return tiny link
26+
self.urls[tpath]=bigurl
27+
return'http://sandbox/'+tpath
28+
29+
defexpand(self,tinyurl):
30+
# lookup big url and return
31+
turl=urlparse(tinyurl)
32+
ifturl.netloc!='sandbox':
33+
raiseShortyError('Not a sandbox url')
34+
returnself.urls.get(turl.path.strip('/'))
35+

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp