-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
79 lines (64 loc) · 2.09 KB
/
engine.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
"""
Feed Engine
~~~~~~~~~~~~
a module manages feeds & articles
:copyright: (c) 2013 by Aldrich Huang.
:license: BSD, see LICENSE for more detials.
"""
import urllib2
import utils.feedfinder as feedfinder
import utils.urlnorm as urlnorm
import time
import datetime
from operator import itemgetter
from models.feed import Feed, Subscription, Story
def subscribe(user_id, url):
"""
subscribe a feed with address: url
return: @state 'ok'/'network error'/'duplication'
@feed feed information
@articles latested articles
"""
state = 'ok'
url = urlnorm.normalize(url)
if not feedfinder.isFeed(url):
return {'state': 'invalid feed url'}
feed, feed_exist = Feed.get_or_create(url)
sub, sub_exist = Subscription.get_or_create(user_id, feed.feed_id)
if sub_exist:
state = 'duplication'
return {'state': state}
return { 'state': state, 'feed': feed, 'stories': get_stories_for_feed(feed.feed_id) }
def subscribe_imported_feeds(user_id, items):
"""
[item${i}: {
'title': ${title},
'url': ${url}
'link': ${link}
'category': []
}]
return: [newly subscribed feeds]
"""
state = 'ok'
feedlist = []
for item in items:
feed, feed_exist = Feed.get_or_create(item['url'], item['title'], item['link'])
sub, sub_exist = Subscription.get_or_create(user_id, feed.feed_id)
if not sub_exist: feedlist.append(feed)
return { 'state': state, 'feedlist': feedlist }
def get_feedlist_for_user(user_id):
subs = Subscription.get_by_user(user_id)
feedlist = [Feed.get_by_id(sub.feed_id) for sub in subs]
state = 'ok'
return { 'state': state, 'feedlist': feedlist }
def get_stories_for_feed(feed_id, offset=0, limit=20):
stories = Story.get_stories_for_feed(feed_id)
return sorted(stories, key=lambda x: x.publish_date, reverse=True)[offset: limit]
def fetch_history_stories(feed_ids):
from models.fetch_feed_history import HistoryStoryFetcher
fetcher = HistoryStoryFetcher()
states = fetcher.add_feeds(feed_ids)
# start a thread to do the fetch
fetcher.start()
#fetcher.join()
return { 'state':'ok', 'feed_states': states}