-
Notifications
You must be signed in to change notification settings - Fork 0
/
shamer.py
142 lines (133 loc) · 6.08 KB
/
shamer.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python
import constants
import cronjobs
import fbgraph
import shamerdb
import urllib
import web
urls = (
'/', 'index',
'/change_options/', 'change_options'
)
app = web.application(urls, globals())
render = web.template.render('templates/')
class index:
def GET(self):
i = web.input(code=None)
code = None
if i.code:
code = i.code
web.setcookie("fb_code", i.code)
else:
code = web.cookies().get("fb_code")
if code:
access_token = fbgraph.get_access_token(code)
user_dict = fbgraph.get_current_user_info(access_token)
if "error" in user_dict:
auth_url = constants.FB_OAUTH_DIALOG_URL + urllib.urlencode(
{"client_id": constants.FB_APP_ID,
"redirect_uri": constants.FB_REDIRECT_URI,
"scope": "publish_stream,offline_access"})
return render.index(oauth_dialog_url=auth_url)
if not shamerdb.get_user_by_fb_id(user_dict["id"]):
shamerdb.create_user(user_dict["id"],
user_dict["name"],
user_dict["first_name"],
user_dict["last_name"],
access_token)
return render.index(fb_id=user_dict["id"],
fb_first_name=user_dict["first_name"],
dayofweek_menu=get_day_of_week_menu(0),
hour_menu=get_hour_menu(0))
else:
user_row = shamerdb.get_user_by_fb_id(user_dict["id"])
d = user_row["Reminder_Time_Day_Of_Week"]
h = user_row["Reminder_Time_Hour"]
# Convert to PST
if h < 8:
d = (d - 1) % 7
h = (h - 8) % 24
dayofweek_menu = get_day_of_week_menu(d)
hour_menu = get_hour_menu(h)
return render.index(fb_id=user_row["Facebook_Id"],
fb_first_name=user_row["First_Name"],
lastfm_pref=user_row["LastFm"],
lastfm_username=user_row["LastFm_Username"],
nikeplus_pref=user_row["NikePlus"],
nikeplus_username=user_row["NikePlus_Username"],
dayofweek_menu=dayofweek_menu,
hour_menu=hour_menu)
else:
auth_url = constants.FB_OAUTH_DIALOG_URL + urllib.urlencode(
{"client_id": constants.FB_APP_ID,
"redirect_uri": constants.FB_REDIRECT_URI,
"scope": "publish_stream,offline_access"})
return render.index(oauth_dialog_url=auth_url)
class change_options:
def POST(self):
i = web.input(fb_id=None, lastfmcheckbox=None,
lastfmusername=None, nikepluscheckbox=None,
nikeplususername=None, dayofweek=None,
hour=None, deleteinfo=None)
if not i.fb_id:
raise web.seeother('/')
if i.deleteinfo:
web.setcookie("fb_code", "", expires=-1)
cronjobs.remove_cron_jobs(i.fb_id)
shamerdb.delete_user_by_fb_id(i.fb_id)
raise web.seeother('/')
d, h = None, None
if i.dayofweek != None and i.hour != None:
try:
d = int(i.dayofweek)
h = int(i.hour)
# Convert to UTC
if h >= 16:
d = (d + 1) % 7
h = (h + 8) % 24
except ValueError:
raise web.seeother('/')
if 0 <= d and d <= 6 and 0 <= h and h <= 23:
shamerdb.set_user_reminder_time(i.fb_id, d, h)
else:
raise web.seeother('/')
cronjobs.remove_cron_jobs(i.fb_id)
if i.lastfmcheckbox == "True" and i.lastfmusername:
shamerdb.set_user_lastfm_pref(i.fb_id, True, i.lastfmusername)
if d != None and h != None:
cronjobs.add_cron_job(i.fb_id, d, h,
i.lastfmusername, "last.fm")
else:
shamerdb.set_user_last_fm_pref(i.fb_id, False, "")
if i.nikepluscheckbox == "True" and i.nikeplususername:
shamerdb.set_user_nikeplus_pref(i.fb_id, True, i.nikeplususername)
if d != None and h != None:
cronjobs.add_cron_job(i.fb_id, d, h,
i.nikeplususername, "nikeplus")
else:
shamerdb.set_user_nikeplus_pref(i.fb_id, False, "")
return render.optionsset()
def get_day_of_week_menu(default_value):
return web.form.Dropdown(name="dayofweek",
args=[(0,"Sunday"), (1,"Monday"), (2,"Tuesday"),
(3,"Wednesday"), (4,"Thursday"),
(5,"Friday"), (6,"Saturday")],
value=default_value)
def get_hour_menu(default_value):
return web.form.Dropdown(name="hour",
args=[(0, '12:00am'), (1, '1:00am'),
(2, '2:00am'), (3, '3:00am'),
(4, '4:00am'), (5, '5:00am'),
(6, '6:00am'), (7, '7:00am'),
(8, '8:00am'), (9, '9:00am'),
(10, '10:00am'), (11, '11:00am'),
(12, '12:00pm'), (13, '1:00pm'),
(14, '2:00pm'), (15, '3:00pm'),
(16, '4:00pm'), (17, '5:00pm'),
(18, '6:00pm'), (19, '7:00pm'),
(20, '8:00pm'), (21, '9:00pm'),
(22, '10:00pm'), (23, '11:00pm')],
value=default_value)
if __name__ == "__main__":
shamerdb.database_init()
app.run()