def parse_subscription(raw_post_data, format): """Parses the data according to the format""" if format == "txt": urls = raw_post_data.split("\n") elif format == "opml": begin = raw_post_data.find("<?xml") end = raw_post_data.find("</opml>") + 7 i = Importer(content=raw_post_data[begin:end]) urls = [p["url"] for p in i.items] elif format == "json": begin = raw_post_data.find("[") end = raw_post_data.find("]") + 1 urls = json.loads(raw_post_data[begin:end]) if not isinstance(urls, list): raise ValueError("A list of feed URLs was expected") else: return [] urls = filter(None, urls) urls = list(map(normalize_feed_url, urls)) return urls
def parse_subscription(raw_post_data, format): """ Parses the data according to the format """ if format == 'txt': urls = raw_post_data.split('\n') elif format == 'opml': begin = raw_post_data.find('<?xml') end = raw_post_data.find('</opml>') + 7 i = Importer(content=raw_post_data[begin:end]) urls = [p['url'] for p in i.items] elif format == 'json': begin = raw_post_data.find('[') end = raw_post_data.find(']') + 1 urls = json.loads(raw_post_data[begin:end]) if not isinstance(urls, list): raise ValueError('A list of feed URLs was expected') else: return [] urls = filter(None, urls) urls = list(map(normalize_feed_url, urls)) return urls
def test_get_subscriptions_empty(self): testers = { 'txt': lambda c: self.assertEqual(c, b''), 'json': lambda c: self.assertEqual(c, b'[]'), 'jsonp': lambda c: self.assertEqual(c, b'test([])'), 'opml': lambda c: self.assertListEqual(Importer(c).items, []), } for fmt in self.formats: url = self.subscriptions_urls[fmt] response = self.client.get(url, data={'jsonp': 'test'}, **self.extra) self.assertEqual(response.status_code, 200, response.content) testers[fmt](response.content)
def test_get_subscriptions_empty(self): testers = { "txt": lambda c: self.assertEqual(c, b""), "json": lambda c: self.assertEqual(c, b"[]"), "jsonp": lambda c: self.assertEqual(c, b"test([])"), "opml": lambda c: self.assertListEqual(Importer(c).items, []), } for fmt in self.formats: url = self.subscriptions_urls[fmt] response = self.client.get(url, data={"jsonp": "test"}, **self.extra) self.assertEqual(response.status_code, 200, response.content) testers[fmt](response.content)
def upload(request): try: emailaddr = request.POST['username'] password = request.POST['password'] action = request.POST['action'] protocol = request.POST['protocol'] opml = request.FILES['opml'].read() except MultiValueDictKeyError: return HttpResponse("@PROTOERROR", content_type='text/plain') user = auth(emailaddr, password) if not user: return HttpResponse('@AUTHFAIL', content_type='text/plain') dev = get_device(user, LEGACY_DEVICE_UID, request.META.get('HTTP_USER_AGENT', '')) existing_urls = [x.url for x in dev.get_subscribed_podcasts()] i = Importer(opml) podcast_urls = [p['url'] for p in i.items] podcast_urls = map(normalize_feed_url, podcast_urls) podcast_urls = list(filter(None, podcast_urls)) new = [u for u in podcast_urls if u not in existing_urls] rem = [u for u in existing_urls if u not in podcast_urls] # remove duplicates new = list(set(new)) rem = list(set(rem)) for n in new: p = Podcast.objects.get_or_create_for_url(n).object subscribe(p.pk, user.pk, dev.uid) for r in rem: p = Podcast.objects.get_or_create_for_url(r).object unsubscribe(p.pk, user.pk, dev.uid) return HttpResponse('@SUCCESS', content_type='text/plain')