remove gevent monkey-patch from feed-downloader
[mygpo.git] / mygpo / api / legacy.py
blobc7404e25f63a66a71471590645d51dd80d08b593
2 # This file is part of my.gpodder.org.
4 # my.gpodder.org is free software: you can redistribute it and/or modify it
5 # under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or (at your
7 # option) any later version.
9 # my.gpodder.org is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
12 # License for more details.
14 # You should have received a copy of the GNU Affero General Public License
15 # along with my.gpodder.org. If not, see <http://www.gnu.org/licenses/>.
18 from datetime import datetime
20 from django.http import HttpResponse
21 from django.utils.datastructures import MultiValueDictKeyError
22 from django.views.decorators.csrf import csrf_exempt
23 from django.views.decorators.cache import never_cache
25 from mygpo.log import log
26 from mygpo.api.sanitizing import sanitize_urls
27 from mygpo.users.models import User
28 from mygpo.api.opml import Importer, Exporter
29 from mygpo.core.models import Podcast, SubscriptionException
30 from mygpo.api.backend import get_device
31 from mygpo.db.couchdb.podcast import podcast_for_url
34 LEGACY_DEVICE_NAME = 'Legacy Device'
35 LEGACY_DEVICE_UID = 'legacy'
37 @never_cache
38 @csrf_exempt
39 def upload(request):
40 try:
41 emailaddr = request.POST['username']
42 password = request.POST['password']
43 action = request.POST['action']
44 protocol = request.POST['protocol']
45 opml = request.FILES['opml'].read()
46 except MultiValueDictKeyError:
47 return HttpResponse("@PROTOERROR", mimetype='text/plain')
49 user = auth(emailaddr, password)
50 if (not user):
51 return HttpResponse('@AUTHFAIL', mimetype='text/plain')
53 dev = get_device(user, LEGACY_DEVICE_UID,
54 request.META.get('HTTP_USER_AGENT', ''))
56 existing_urls = [x.url for x in dev.get_subscribed_podcasts()]
58 i = Importer(opml)
60 podcast_urls = [p['url'] for p in i.items]
61 podcast_urls = sanitize_urls(podcast_urls)
62 podcast_urls = filter(lambda x: x, podcast_urls)
64 new = [u for u in podcast_urls if u not in existing_urls]
65 rem = [u for e in existing_urls if u not in podcast_urls]
67 #remove duplicates
68 new = list(set(new))
69 rem = list(set(rem))
71 for n in new:
72 p = podcast_for_url(n, create=True)
74 try:
75 p.subscribe(user, dev)
76 except SubscriptionException as e:
77 log('Legacy API: %(username)s: could not subscribe to podcast %(podcast_url)s on device %(device_id)s: %(exception)s' %
78 {'username': user.username, 'podcast_url': p.url, 'device_id': dev.id, 'exception': e})
80 for r in rem:
81 p = podcast_for_url(r, create=True)
82 try:
83 p.unsubscribe(user, dev)
84 except SubscriptionException as e:
85 log('Legacy API: %(username): could not unsubscribe from podcast %(podcast_url) on device %(device_id): %(exception)s' %
86 {'username': user.username, 'podcast_url': p.url, 'device_id': dev.id, 'exception': e})
88 return HttpResponse('@SUCCESS', mimetype='text/plain')
90 @never_cache
91 @csrf_exempt
92 def getlist(request):
93 emailaddr = request.GET.get('username', None)
94 password = request.GET.get('password', None)
96 user = auth(emailaddr, password)
97 if user is None:
98 return HttpResponse('@AUTHFAIL', mimetype='text/plain')
100 dev = get_device(user, LEGACY_DEVICE_UID,
101 request.META.get('HTTP_USER_AGENT', ''),
102 undelete=True)
103 podcasts = dev.get_subscribed_podcasts()
105 title = "{username}'s subscriptions".format(username=user.username)
106 exporter = Exporter(title)
108 opml = exporter.generate(podcasts)
110 return HttpResponse(opml, mimetype='text/xml')
113 def auth(emailaddr, password):
114 if emailaddr is None or password is None:
115 return None
117 user = User.get_user_by_email(emailaddr)
118 if not user:
119 return None
121 if not user.check_password(password):
122 return None
124 if not user.is_active:
125 return None
127 return user