import os import re from django core management import BaseCommand fro

 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
import os
import re
from django.core.management import BaseCommand
from django.conf import settings
from common import friendfeed
from ffeed.models import Link
from ffeed.util import parse_uri
class Command(BaseCommand):
help = u'Fetch new links from friendfeed.com'
def handle(self, *args, **kwargs):
update_links(fetch_room('killerapp'))
def fetch_room(room, num=settings.FFEED_FETCH_SIZE):
s = friendfeed.FriendFeed(room)
return s._fetch_feed('/api/feed/room/%s' % room, num=num)['entries']
def extract_link(item):
"""
Search the link in the friendfeed entry.
"""
if item['service']['entryType'] == 'link':
return item['link']
else:
return parse_uri(item['title'])
def is_safelink(link):
for match in settings.FFEED_IGNORE_LIST:
if re.search(match, link):
return False
return True
def update_links(items):
for item in items:
try:
link = Link.objects.get(entry_id=item['id'])
except Link.DoesNotExist:
link = extract_link(item)
if link:
Link(uri=extract_link(item),
entry_id=item['id'],
ctime=item['published']).save()
print 'found a link: %s' % link
else:
updated = max([link.ctime] + [x['date'] for x in item['comments']])
if link.ctime != updated:
link.ctime = updated
link.save()
print 'update time for link: %s' % link.uri