forked from migurski/dwim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwim.py
77 lines (53 loc) · 2.14 KB
/
dwim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class EpistemicClosure(Exception): pass
def time2str(s, fancy=False, tz=None):
"""
"""
from datetime import datetime
from time import strftime
time = datetime.fromtimestamp(s, tz)
if fancy:
zone = str(time)[-6:]
full = strftime('%a, %d %b %Y %H:%M:%S ', time.timetuple())
return full + zone
else:
return str(time)
def str2time(s):
"""
"""
from dateutil.parser import parse
try:
dt = parse(s)
except ValueError:
raise EpistemicClosure('I don\'t know what you mean by "%s"' % s)
if dt.utcoffset() is None:
from time import mktime as tuple2epoch
# no timezone was specified in the input, so we will assume local time.
else:
from calendar import timegm as tuple2epoch
# here, dt has a populated UTC offset.
# we subtract it from dt so we can return a value in UTC.
dt -= dt.utcoffset()
return int(tuple2epoch(dt.timetuple()))
def oauthdance(consumer_key, consumer_secret, request_token_url='http://twitter.com/oauth/request_token', authorize_url='http://twitter.com/oauth/authorize', access_token_url='http://twitter.com/oauth/access_token'):
"""
"""
from oauth2 import Consumer, Client, Token
from urlparse import parse_qsl
con = Consumer(consumer_key, consumer_secret)
cli = Client(con)
res, bod = cli.request(request_token_url, 'GET')
assert res['status'] == '200', 'Expected status=200, got %s.' % res['status']
tok = dict(parse_qsl(bod))
tok = Token(tok['oauth_token'], tok['oauth_token_secret'])
print 'Visit this URL to get a PIN:'
print ' %s?oauth_token=%s' % (authorize_url, tok.key)
pin = raw_input('PIN: ').strip()
tok.set_verifier(pin)
cli = Client(con, tok)
res, bod = cli.request(access_token_url, 'GET')
assert res['status'] == '200', 'Expected status=200, got %s.' % res['status']
tok = dict(parse_qsl(bod))
tok = Token(tok['oauth_token'], tok['oauth_token_secret'])
print 'Your token key is: ', tok.key
print 'And your secret is:', tok.secret
return tok