def set_subscriptions(urls, user, device_uid, user_agent): device = get_device(user, device_uid, user_agent, undelete=True) subscriptions = dict( (p.url, p) for p in device.get_subscribed_podcasts()) new = [p for p in urls if p not in subscriptions.keys()] rem = [p for p in subscriptions.keys() if p not in urls] subscriber = BulkSubscribe(user, device, podcasts=subscriptions) for r in rem: subscriber.add_action(r, 'unsubscribe') for n in new: subscriber.add_action(n, 'subscribe') try: errors = subscriber.execute() except BulkException as be: for err in be.errors: logger.warn('Simple API: %(username)s: Updating subscription for ' '%(podcast_url)s on %(device_uid)s failed: ' '%(error)s (%(reason)s)'.format(username=user.username, podcast_url=err.doc, device_uid=device.uid, error=err.error, reason=err.reason) ) # Only an empty response is a successful response return HttpResponse('', mimetype='text/plain')
def update_subscriptions(self, user, device, add, remove): conflicts = intersect(add, remove) if conflicts: msg = "can not add and remove '{}' at the same time".format( str(conflicts)) raise RequestException(msg) add_s = map(normalize_feed_url, add) rem_s = map(normalize_feed_url, remove) assert len(add) == len(add_s) and len(remove) == len(rem_s) pairs = zip(add + remove, add_s + rem_s) updated_urls = filter(lambda (a, b): a != b, pairs) 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: msg = 'Advanced API: {user}: Updating subscription for ' \ '{podcast} on {device} failed: {err} {reason}'.format( user=user.username, podcast=err.doc, device=device.uid, err=err.error, reason=err.reason) loger.error(msg) return updated_urls
def update_subscriptions(user, device, add, remove): for a in add: if a in remove: raise ValueError('can not add and remove %s at the same time' % a) add_s = map(normalize_feed_url, add) rem_s = map(normalize_feed_url, remove) 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: loger.error('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