def update_subscriptions(user, device, add, remove): for a in add: if a in remove: raise IntegrityError('can not add and remove %s at the same time' % a) add_s = list(sanitize_urls(add, 'podcast')) rem_s = list(sanitize_urls(remove, 'podcast')) assert len(add) == len(add_s) and len(remove) == len(rem_s) updated_urls = filter(lambda (a, b): a != b, zip(add + remove, add_s + rem_s)) add_s = filter(None, add_s) rem_s = filter(None, rem_s) # If two different URLs (in add and remove) have # been sanitized to the same, we ignore the removal rem_s = filter(lambda x: x not in add_s, rem_s) subscriber = BulkSubscribe(user, device) for a in add_s: subscriber.add_action(a, 'subscribe') for r in rem_s: subscriber.add_action(r, 'unsubscribe') try: subscriber.execute() except BulkException as be: for err in be.errors: log('Advanced API: %(username)s: Updating subscription for ' '%(podcast_url)s on %(device_uid)s failed: ' '%(rerror)s (%(reason)s)'.format(username=user.username, podcast_url=err.doc, device_uid=device.uid, error=err.error, reason=err.reason) ) return updated_urls
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", mimetype='text/plain') user = auth(emailaddr, password) if (not user): return HttpResponse('@AUTHFAIL', mimetype='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 = sanitize_urls(podcast_urls) podcast_urls = filter(lambda x: x, podcast_urls) new = [u for u in podcast_urls if u not in existing_urls] rem = [u for e in existing_urls if u not in podcast_urls] #remove duplicates new = list(set(new)) rem = list(set(rem)) for n in new: try: p = Podcast.for_url(n, create=True) except IntegrityError, e: log('/upload: Error trying to get podcast object: %s (error: %s)' % (n, e)) continue try: p.subscribe(user, dev) except Exception as e: log('Legacy API: %(username)s: could not subscribe to podcast %(podcast_url)s on device %(device_id)s: %(exception)s' % {'username': user.username, 'podcast_url': p.url, 'device_id': dev.id, 'exception': e})
def parse_subscription(raw_post_data, 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]) else: return [] urls = sanitize_urls(urls) urls = filter(None, urls) urls = set(urls) return urls