/
amplifeeder.py
448 lines (343 loc) · 12.4 KB
/
amplifeeder.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python
import wsgiref.handlers
import re # Regular expressions
import random
import os
import datetime
import time
import md5
import base64
import urllib
import sys
import logging
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext import db
from google.appengine.ext.db import djangoforms
from django.utils import simplejson
import friendfeedproxy
import simplejsondate
import model
import pinger
import admin
def prettydate(timestamp):
ageresult = datetime.datetime.today() - timestamp
if ageresult.days < 1:
hours = ageresult.seconds / 3600
if hours == 0:
return "not long "
elif hours == 1:
return str(hours) + " hour "
else:
return str(hours) + " hours "
else:
days = str(ageresult.days)
if days == "1":
return days + " day "
else:
return days + " days "
return
def smart_bool(s):
if s is True or s is False:
return s
s = str(s).strip().lower()
return not s in ['false','f','n','0','']
def RenderThemeTemplate(templatename, data):
path = os.path.join(os.path.dirname(__file__), "Templates", templatename)
logging.info(path)
return template.render(path, data)
def Render(name, data):
path = os.path.join(os.path.dirname(__file__), name)
return template.render(path, data)
channelMap = {
"blog" : "CustomRSS20",
"delicious" : "Delicious",
"digg" : "Digg",
"disqus" : "Digg",
"facebook" : "Facebook",
"flickr" : "Flickr",
"googlereader": "GoogleReader",
"internal" : "CustomRSS20",
"linkedin" : "LinkedIn",
"reddit" : "Reddit",
"twitter": "Twitter",
"vimeo" : "Vimeo",
"mixx" : "Mixx",
"youtube" : "YouTube",
"picasa" : "Picasa",
"tumblr" : "Tumblr",
"upcoming" : "Upcoming",
"stumbleupon" : "Stumbleupon"
}
serviceTypeMap = {
"Bookmark" : ["digg", "delicious", "reddit", "stumbleupon", "mixx"],
"Video" : ["youtube", "vimeo"],
"Image" : ["flickr", "picasa"],
"Post" : ["upcoming", "tumblr", "microblog", "customatom", "customrss20", "googlereader"],
"Note": ["twitter", "facebook", "lastfm", "brighkite"]
}
class FriendFeedConverter():
def ConvertChannels(self, profile):
channels = []
for service in profile[u"services"]:
if u"profileUrl" in service:
channel = {}
channel.update(ChannelType = service[u"name"])
channel.update(FeedUri = service[u"profileUrl"])
channel.update(Name = service[u"name"])
channel.update(Title = service[u"name"])
channel.update(IsEnabled = True)
channel.update(Id = service[u"id"])
if service[u"id"] in channelMap:
channel.update(SourceTypeName = channelMap[service[u"id"]])
else:
channel.update(SourceTypeName = channelMap["none"])
channels.append(channel)
return channels
def ConvertUserFeed(self, profile):
#Get The Service
# Convert Comment Counts
d = {}
feeditems = []
comments = []
for entry in profile[u"entries"]:
item = {}
timeago = prettydate(entry[u"published"])
item.update(Title = entry[u"title"])
item.update(PublishDate = entry[u"published"])
item.update(PrettyDate = timeago + " ago")
item.update(Date = entry[u"published"])
item.update(Description = entry[u"title"])
#If the link contains media embed that.
if u"media" in entry and len(entry[u"media"]) > 0:
logging.info(entry)
if u"thumbnails" in entry[u"media"][0] and entry[u"media"][0][u"thumbnails"] != None:
item.update(ItemContentPreview = entry[u"media"][0][u"thumbnails"][0][u"url"])
item.update(SourceLink = entry[u"link"])
if entry[u"service"][u"id"] in channelMap:
item.update(SourceTypeName = channelMap[entry[u"service"][u"id"]])
else:
item.update(SourceTypeName = channelMap["none"])
item.update(SourceTitle = entry[u"service"][u"name"])
item.update(Id = entry[u"id"])
item.update(CommentCount = len(entry[u"comments"]))
for comment in entry[u"comments"]:
comm = {}
comm.update(Date = comment[u"date"])
comm.update(CommentDate = comment[u"date"])
comm.update(CommentBody = comment[u"body"])
comm.update(Name = comment[u"user"][u"name"])
comm.update(Email = comment[u"user"][u"profileUrl"])
comm.update(AmplifeederItemId = entry[u"id"])
comments.append(comm)
feeditems.append(item)
d.update(FeedItems = feeditems)
d.update(Comments = comments)
return d
def ConvertEntry(self, entry):
#Get The Service
# Convert Comment Counts
d = {}
feeditems = None
comments = []
for entry in entry[u"entries"]:
item = {}
timeago = prettydate(entry[u"published"])
item.update(Title = entry[u"title"])
item.update(PublishDate = entry[u"published"])
item.update(PrettyDate = timeago + " ago")
item.update(Date = entry[u"published"])
item.update(SourceLink = entry[u"link"])
if entry[u"service"][u"id"] in channelMap:
item.update(SourceTypeName = channelMap[entry[u"service"][u"id"]])
else:
item.update(SourceTypeName = channelMap["none"])
item.update(SourceTitle = entry[u"service"][u"name"])
item.update(Description = "")
item.update(Id = entry[u"id"])
#If the link contains media embed that.
if u"media" in entry and len(entry[u"media"]) > 0:
logging.info(entry)
item.update(ItemContent = entry[u"media"][0][u"thumbnails"][0][u"url"])
#Work out a decent way to get the data inline.
item.update(Data = "<img src=\"%s\" />" %entry[u"media"][0][u"content"][0][u"url"])
item.update(CommentCount = len(entry[u"comments"]))
for comment in entry[u"comments"]:
comm = {}
comm.update(Date = comment[u"date"])
comm.update(CommentDate = comment[u"date"])
comm.update(CommentBody = comment[u"body"])
comm.update(Name = comment[u"user"][u"name"])
comm.update(Email = comment[u"user"][u"profileUrl"])
comm.update(AmplifeederItemId = entry[u"id"])
comments.append(comm)
feeditems = item
d.update(Item = feeditems)
d.update(Comments = comments)
return d
class Init(webapp.RequestHandler):
def get(self):
s = model.Settings.CreateDefault()
class Index(webapp.RequestHandler):
def get(self):
s = model.Settings.Get()
#If there are no settings create them.
if s is None:
s = model.Settings.CreateDefault()
output = ""
if s.Configured == False:
s.Configured = True
s.put()
output = RenderThemeTemplate("Default.tmpl", { "startup": "var ThemeName = '%s'" % s.Theme })
# First load will create load the user info in to the db.
self.response.out.write(output)
class Detail(webapp.RequestHandler):
def get(self):
s = model.Settings.Get()
#If there are no settings create them.
if s is None:
s = model.Settings.CreateDefault()
output = RenderThemeTemplate("Detail.tmpl", { "startup": "var ThemeName = '%s'" % s.Theme })
self.response.out.write(output)
class AssetCombiner(webapp.RequestHandler):
def get(self):
filestr = self.request.get("files", "")
files = filestr.split(',')
for file in files:
self.response.out.write(Render(file, {}))
class UIService(webapp.RequestHandler):
def post(self, method):
if method not in ["GetEnabledChannels","GetItemsPackage","GetPageCount", "SubmitComment", "GetFeature", "GetEnabledChannels", "GetDetailItem", "GetActiveSources", "GetTags","GetActiveSources","GetInitItemsPackage"]:
# Don't allow anyone to call any un-meant method.
return
#Load this module so we can dyanmically load the class
m = __import__("amplifeeder")
#Import the module and then call it.
m = getattr(m, method)()
# Pass in the request object
obj = m.Render(self)
class GetEnabledChannels():
def Render(self, request):
channels = model.Channel.GetChannelsByEnabled(True)
request.response.out.write(simplejson.dumps(channels))
class GetTags():
def Render(self, request):
Page = request.request.get("Page", 1)
Page = int(Page)
tags = model.Tag.GetTags(Page, 10)
request.response.out.write("{ \"d\": [")
tags_serialized = []
for tag in tags:
output = tag.to_json()
tags_serialized.append(output)
request.response.out.write(','.join(tags_serialized))
request.response.out.write("]}")
class GetInitItemsPackage():
def Render(self, request):
settings = model.Settings.Get()
converter = FriendFeedConverter()
input = simplejson.loads(request.request.body)
#{"PageSize":"16", "PageNumber":"1", "ItemFilterType":"None", "ItemFilterArgument":"None"}
json = {}
pageNumber = int(input["PageNumber"])
# Might want to cache this
if input["ItemFilterType"] == u"None":
# Front page
ff = friendfeedproxy.FriendFeed()
feed = ff.fetch_user_feed(settings.Username, pageNumber)
json = converter.ConvertUserFeed(feed)
feedItems = []
item = None
pageCount = 100
channel = None
comments = None
#json = {}
#json.update(FeedItems = feedItems)
json.update(Item = item)
json.update(PageCount = pageCount)
json.update(Channel = channel)
#json.update(Comments = comments)
json.update(Settings = settings.to_dict())
d = {}
d.update(d = json)
request.response.out.write(simplejsondate.dumps(d))
class GetItemsPackage():
def Render(self, request):
input = simplejson.loads(request.request.body)
settings = model.Settings.Get()
converter = FriendFeedConverter()
feed = {}
pageNumber = int(input["PageNumber"])
if input["ItemFilterType"] is None or input["ItemFilterType"] == "None" :
ff = friendfeedproxy.FriendFeed()
feed = ff.fetch_user_feed(settings.Username, pageNumber, service = input["ItemFilterArgument"])
elif input["ItemFilterType"] == "Source":
ff = friendfeedproxy.FriendFeed()
feed = ff.fetch_user_service_feed(settings.Username, input["ItemFilterArgument"], pageNumber)
elif input["ItemFilterType"] == "Search":
ff = friendfeedproxy.FriendFeed()
kwargs = {}
kwargs["from"] = settings.Username
query = input["ItemFilterArgument"]
feed = ff.search(query, pageNumber, **kwargs )
elif input["ItemFilterType"] == "ItemType":
argument = input["ItemFilterArgument"]
logging.info("Filtering on Type %s" % argument)
ff = friendfeedproxy.FriendFeed()
feed = ff.fetch_user_services_feed(settings.Username, serviceTypeMap[argument], pageNumber)
json = converter.ConvertUserFeed(feed)
d= {}
d.update ( d = json)
request.response.out.write(simplejsondate.dumps(d))
class GetPageCount():
def Render(self, request):
PageSize = request.request.get("PageSize","")
ItemFilterType = request.request.get("ItemFilterType","")
ItemFilterArgument = request.request.get("ItemFilterArgument","")
request.response.out.write("{}")
class SubmitComment():
def Render(self, request):
request.response.out.write("{}")
class GetEnabledChannels():
def Render(self, request):
request.response.out.write("{}")
class GetActiveSources():
'Gets all Active Sources'
def Render(self, request):
settings = model.Settings.Get()
converter = FriendFeedConverter()
channels = []
ff = friendfeedproxy.FriendFeed()
feed = ff.fetch_user_profile(settings.Username)
channels = converter.ConvertChannels(feed)
d = { }
d.update(d = channels)
request.response.out.write(simplejsondate.dumps(d))
class GetDetailItem():
def Render(self, request):
input = simplejson.loads(request.request.body)
#{"itemID":"75377fc9-1b93-58ff-c066-defcb55ff45e", "numberToReturn":"10"}
settings = model.Settings.Get()
converter = FriendFeedConverter()
channels = []
ff = friendfeedproxy.FriendFeed()
entry = ff.fetch_entry(input[u"itemID"])
detail = converter.ConvertEntry(entry)
feed = ff.fetch_user_feed(settings.Username, 1 , service = detail["Item"]["SourceTypeName"].lower())
json = converter.ConvertUserFeed(feed)
detail.update(Settings = settings.to_dict())
detail.update(FeedItems = json["FeedItems"])
d = { }
d.update(d = detail)
request.response.out.write(simplejsondate.dumps(d))
class GetFeature():
def Render(self, request):
itemtype = request.request.get("itemtype", "")
numberToReturn = request.request.get("numberToReturn", 10)
numberToReturn = int(numberToReturn)
request.response.out.write("{}")
def main():
application = webapp.WSGIApplication([(r'/', Index), (r'/default.aspx', Index), (r'/admin/Settings', admin.Settings),(r'/Init', Init) ,(r'/friendfeed/ping', pinger.Ping), (r'/AssetCombiner.ashx', AssetCombiner), (r'/UIService.asmx/(.+)', UIService), (r'/detail.aspx', Detail)], debug=False)
wsgiref.handlers.CGIHandler().run(application);
if __name__ == '__main__':
main()