/
opmlimport.py
62 lines (48 loc) · 2.02 KB
/
opmlimport.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
from optparse import make_option
from django.db import connection
from django.core.management.base import BaseCommand, CommandError
from manage import path
from twas.utils import opml
from main.models import Profile, FeedSubscription
PROFILES_BY_USER_NAME_VIEW = 'main/Profile-by-user-name'
FEEDSUBSCRIPTION_BY_PROFILE_ID_VIEW = 'main/FeedSubscription-by-profile-id'
class Command(BaseCommand):
help = 'Import OPML subscriptions'
args = 'filename'
option_list = BaseCommand.option_list + (
make_option('-u', '--user',
action='store', dest='user_name',
default="",
help='User name'),
)
can_import_settings = True
def handle(self, *arg, **kwargs):
if len(arg) < 1:
raise CommandError('OPML filename required')
# Grab the profile for supplied user name
p = Profile.view(PROFILES_BY_USER_NAME_VIEW,
key=kwargs['user_name']).first()
if not p:
raise CommandError('No profile named %s found' %
kwargs['user_name'])
# Look up subscriptions for profile.
subs = FeedSubscription.view(FEEDSUBSCRIPTION_BY_PROFILE_ID_VIEW,
key=p._id).all()
subs_by_url = dict((s.url, s) for s in subs)
with open(arg[0]) as fin:
entries = opml.get_subscriptions(fin)
for entry in entries:
url = unicode(entry['xmlurl'])
if url in subs_by_url:
sub = subs_by_url[url]
print "Updating %s" % entry['title']
else:
sub = FeedSubscription.get_or_create()
print "Creating %s" % entry['title']
sub.profile_id = p._id
sub.feed_type = 'rss'
sub.url = url
sub.link = entry['url']
sub.title = entry['title']
sub.parents = [x['title'] for x in entry['parents']]
sub.save()