-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.py
59 lines (43 loc) · 1.47 KB
/
functions.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
from urllib2 import urlopen
from json import loads
from data import data
SIMULATE_TWITTER = False
class FakeTwitter(object):
def PostUpdate(self, msg, **kwargs):
with open('twitter.out', 'w') as outfile:
outfile.write(msg)
if not SIMULATE_TWITTER:
from twitter import Api
twitter = Api(consumer_key = data.twitter_consumer_key,
consumer_secret = data.twitter_consumer_secret,
access_token_key = data.twitter_access_token,
access_token_secret = data.twitter_access_token_secret)
else:
twitter = FakeTwitter()
def twitter_post(msg):
twitter.PostUpdate(msg,
latitude = data.last_latitude,
longitude = data.last_longitude)
def get_beverage_description(upc):
upc = int(upc)
beverage = data.get_beverage(upc)
if beverage: return beverage.description
url = ('http://www.upcdatabase.org/api/json/%s/%0.12d/' %
(data.upc_database_key, upc))
resp = urlopen(url)
jsondata = resp.read()
upcdata = loads(jsondata)
if upcdata['valid'] == 'true':
description = upcdata['description']
try:
data.new_beverage(upc, description)
finally:
return description
else:
return None
def get_user():
return data.get_current_user()
def update_user(user_id):
data.update_user(user_id)
def log(upc):
data.log(upc)