def update_episodes(user, actions, now, ua_string): update_urls = [] # group all actions by their episode for action in actions: podcast_url = action.get('podcast', '') podcast_url = sanitize_append(podcast_url, update_urls) if not podcast_url: continue episode_url = action.get('episode', '') episode_url = sanitize_append(episode_url, update_urls) if not episode_url: continue podcast = Podcast.objects.get_or_create_for_url(podcast_url).object episode = Episode.objects.get_or_create_for_url(podcast, episode_url).object # parse_episode_action returns a EpisodeHistoryEntry obj history = parse_episode_action(action, user, update_urls, now, ua_string) EpisodeHistoryEntry.create_entry(user, episode, history.action, history.client, history.timestamp, history.started, history.stopped, history.total, podcast_url, episode_url) return update_urls
def test_merge_podcasts(self): action1 = EpisodeHistoryEntry.create_entry( self.user, self.episode1, EpisodeHistoryEntry.PLAY, ) action2 = EpisodeHistoryEntry.create_entry( self.user, self.episode2, EpisodeHistoryEntry.DOWNLOAD, ) # decide which episodes to merge groups = [(0, [self.episode1, self.episode2])] counter = Counter() pm = PodcastMerger([self.podcast1, self.podcast2], counter, groups) pm.merge() history = EpisodeHistoryEntry.objects.filter( episode=self.episode1, user=self.user, ) # both actions must be present for the merged episode self.assertIn(action1, history) self.assertIn(action2, history)
def update_episodes(user, actions, now, ua_string): update_urls = [] auto_flattr = user.profile.settings.get_wksetting(FLATTR_AUTO) # group all actions by their episode for action in actions: podcast_url = action['podcast'] podcast_url = sanitize_append(podcast_url, update_urls) if podcast_url == '': continue episode_url = action['episode'] episode_url = sanitize_append(episode_url, update_urls) if episode_url == '': continue podcast = Podcast.objects.get_or_create_for_url(podcast_url) episode = Episode.objects.get_or_create_for_url(podcast, episode_url) # parse_episode_action returns a EpisodeHistoryEntry obj history = parse_episode_action(action, user, update_urls, now, ua_string) EpisodeHistoryEntry.create_entry(user, episode, history.action, history.client, history.timestamp, history.started, history.stopped, history.total, podcast_url, episode_url) if history.action == EpisodeHistoryEntry.PLAY and auto_flattr: auto_flattr_episode.delay(user, episode.id) return update_urls
def test_merge_podcasts(self): podcast1 = Podcast.objects.get(pk=self.podcast1.pk) podcast2 = Podcast.objects.get(pk=self.podcast2.pk) podcast3 = Podcast.objects.get(pk=self.podcast3.pk) # assert that the podcasts are actually grouped self.assertEqual(podcast2.group, podcast3.group) action1 = EpisodeHistoryEntry.create_entry(self.user, self.episode1, EpisodeHistoryEntry.PLAY) action2 = EpisodeHistoryEntry.create_entry( self.user, self.episode2, EpisodeHistoryEntry.DOWNLOAD) # decide which episodes to merge groups = [(0, [self.episode1.id, self.episode2.id])] counter = Counter() episode2_id = self.episode2.id pm = PodcastMerger([podcast2, podcast1], counter, groups) pm.merge() history = EpisodeHistoryEntry.objects.filter(episode=self.episode1, user=self.user) self.assertIn(action1, history) self.assertIn(action2, history) episode1 = Episode.objects.get(pk=self.episode1.pk) # episode2 has been merged into episode1, so it must contain its # merged _id self.assertEqual([x.uuid for x in episode1.merged_uuids.all()], [episode2_id])
def update_episodes(user, actions, now, ua_string): update_urls = [] auto_flattr = user.profile.settings.get_wksetting(FLATTR_AUTO) # group all actions by their episode for action in actions: podcast_url = action.get('podcast', '') podcast_url = sanitize_append(podcast_url, update_urls) if not podcast_url: continue episode_url = action.get('episode', '') episode_url = sanitize_append(episode_url, update_urls) if not episode_url: continue podcast = Podcast.objects.get_or_create_for_url(podcast_url) episode = Episode.objects.get_or_create_for_url(podcast, episode_url) # parse_episode_action returns a EpisodeHistoryEntry obj history = parse_episode_action(action, user, update_urls, now, ua_string) EpisodeHistoryEntry.create_entry(user, episode, history.action, history.client, history.timestamp, history.started, history.stopped, history.total, podcast_url, episode_url) if history.action == EpisodeHistoryEntry.PLAY and auto_flattr: auto_flattr_episode.delay(user.pk, episode.pk) return update_urls
def auto_flattr_episode(user, episode_id): """ Task to auto-flattr an episode In addition to the flattring itself, it also records the event """ success, msg = flattr_thing(user, episode_id, None, False, 'Episode') if not success: return False episode = Episode.objects.get(id=episode_id) EpisodeHistoryEntry.create_entry(user, episode, EpisodeHistoryEntry.FLATTR) return True
def auto_flattr_episode(user_id, episode_id): """ Task to auto-flattr an episode In addition to the flattring itself, it also records the event """ success, msg = flattr_thing(user_id, episode_id, None, False, 'Episode') if not success: return False episode = Episode.objects.get(id=episode_id) user = User.objects.get(pk=user_id) EpisodeHistoryEntry.create_entry(user, episode, EpisodeHistoryEntry.FLATTR) return True
def add_action(request, episode): user = request.user client = user.client_set.get(id=request.POST.get("device")) action_str = request.POST.get("action") timestamp = request.POST.get("timestamp", "") if timestamp: try: timestamp = dateutil.parser.parse(timestamp) except (ValueError, AttributeError, TypeError): timestamp = datetime.utcnow() else: timestamp = datetime.utcnow() EpisodeHistoryEntry.create_entry(user, episode, action_str, client, timestamp) podcast = episode.podcast return HttpResponseRedirect(get_episode_link_target(episode, podcast))
def test_merge_podcasts(self): podcast1 = Podcast.objects.get(pk=self.podcast1.pk) podcast2 = Podcast.objects.get(pk=self.podcast2.pk) podcast3 = Podcast.objects.get(pk=self.podcast3.pk) # assert that the podcasts are actually grouped self.assertEqual(podcast2.group, podcast3.group) action1 = EpisodeHistoryEntry.create_entry( self.user, self.episode1, EpisodeHistoryEntry.PLAY, ) action2 = EpisodeHistoryEntry.create_entry( self.user, self.episode2, EpisodeHistoryEntry.DOWNLOAD, ) # decide which episodes to merge groups = [(0, [self.episode1, self.episode2])] counter = Counter() episode2_id = self.episode2.id pm = PodcastMerger([podcast2, podcast1], counter, groups) pm.merge() history = EpisodeHistoryEntry.objects.filter( episode = self.episode1, user = self.user, ) self.assertIn(action1, history) self.assertIn(action2, history) episode1 = Episode.objects.get(pk=self.episode1.pk) # episode2 has been merged into episode1, so it must contain its # merged _id self.assertEqual([x.uuid for x in episode1.merged_uuids.all()], [episode2_id])
def add_action(request, episode): user = request.user client = user.client_set.get(id=request.POST.get('device')) action_str = request.POST.get('action') timestamp = request.POST.get('timestamp', '') if timestamp: try: timestamp = dateutil.parser.parse(timestamp) except (ValueError, AttributeError, TypeError): timestamp = datetime.utcnow() else: timestamp = datetime.utcnow() EpisodeHistoryEntry.create_entry(user, episode, action_str, client, timestamp) podcast = episode.podcast return HttpResponseRedirect(get_episode_link_target(episode, podcast))
def update_episodes(user, actions, now, ua_string): update_urls = [] # group all actions by their episode for action in actions: podcast_url = action.get('podcast', '') podcast_url = sanitize_append(podcast_url, update_urls) if not podcast_url: continue episode_url = action.get('episode', '') episode_url = sanitize_append(episode_url, update_urls) if not episode_url: continue podcast = Podcast.objects.get_or_create_for_url(podcast_url).object episode = Episode.objects.get_or_create_for_url(podcast, episode_url).object # parse_episode_action returns a EpisodeHistoryEntry obj history = parse_episode_action(action, user, update_urls, now, ua_string) EpisodeHistoryEntry.create_entry( user, episode, history.action, history.client, history.timestamp, history.started, history.stopped, history.total, podcast_url, episode_url, ) return update_urls
def parse_episode_action(action, user, update_urls, now, ua_string): action_str = action.get('action', None) if not valid_episodeaction(action_str): raise Exception('invalid action %s' % action_str) history = EpisodeHistoryEntry() history.action = action['action'] if action.get('device', False): client = get_device(user, action['device'], ua_string) history.client = client if action.get('timestamp', False): history.timestamp = dateutil.parser.parse(action['timestamp']) else: history.timestamp = now history.started = action.get('started', None) history.stopped = action.get('position', None) history.total = action.get('total', None) return history
def parse_episode_action(action, user, update_urls, now, ua_string): action_str = action.get("action", None) if not valid_episodeaction(action_str): raise Exception("invalid action %s" % action_str) history = EpisodeHistoryEntry() history.action = action["action"] if action.get("device", False): client = get_device(user, action["device"], ua_string) history.client = client if action.get("timestamp", False): history.timestamp = dateutil.parser.parse(action["timestamp"]) else: history.timestamp = now history.started = action.get("started", None) history.stopped = action.get("position", None) history.total = action.get("total", None) return history
def test_merge(self): p1 = Podcast.objects.get_or_create_for_url( "http://example.com/podcast1.rss" ).object p2 = Podcast.objects.get_or_create_for_url( "http://example.com/podcast2.rss" ).object e1 = Episode.objects.get_or_create_for_url( p1, "http://example.com/podcast1/e1.mp3" ).object e1.title = "Episode 1" e1.save() e2 = Episode.objects.get_or_create_for_url( p2, "http://example.com/podcast1/e2.mp3" ).object e2.title = "Episode 2" e2.save() e3 = Episode.objects.get_or_create_for_url( p2, "http://example.com/podcast2/e2.mp3" ).object e3.title = "Episode 3" e3.save() e4 = Episode.objects.get_or_create_for_url( p2, "http://example.com/podcast2/e3.mp3" ).object e4.title = "Episode 4" e4.save() User = get_user_model() user = User() user.username = "******" user.email = "*****@*****.**" user.set_password("secret") user.save() device1 = Client.objects.create(user=user, uid="dev1", id=uuid.uuid1()) device2 = Client.objects.create(user=user, uid="dev2", id=uuid.uuid1()) subscribe(p1.pk, user.pk, device1.uid) unsubscribe(p1.pk, user.pk, device1.uid) subscribe(p1.pk, user.pk, device1.uid) subscribe(p2.pk, user.pk, device2.uid) action1 = EpisodeHistoryEntry.create_entry(user, e1, EpisodeHistoryEntry.PLAY) action3 = EpisodeHistoryEntry.create_entry(user, e3, EpisodeHistoryEntry.PLAY) # we need that for later e3_id = e3.pk actions = Counter() # decide which episodes to merge groups = [(0, [e1.id]), (1, [e2.id, e3.id]), (2, [e4.id])] # carry out the merge pm = PodcastMerger([p1, p2], actions, groups) pm.merge() e1 = Episode.objects.get(pk=e1.pk) history1 = EpisodeHistoryEntry.objects.filter(episode=e1, user=user) self.assertEqual(len(history1), 1) # check if merged episode's id can still be accessed e3 = Episode.objects.filter(podcast=p1).get_by_any_id(e3_id) history3 = EpisodeHistoryEntry.objects.filter(episode=e3, user=user) self.assertEqual(len(history3), 1) p1 = Podcast.objects.get(pk=p1.get_id()) subscribed_clients = Client.objects.filter(subscription__podcast=p1) self.assertEqual(len(subscribed_clients), 2) episodes = p1.episode_set.all() self.assertEqual(len(episodes), 3)
def test_merge(self): p1 = Podcast.objects.get_or_create_for_url( 'http://example.com/podcast1.rss' ).object p2 = Podcast.objects.get_or_create_for_url( 'http://example.com/podcast2.rss' ).object e1 = Episode.objects.get_or_create_for_url( p1, 'http://example.com/podcast1/e1.mp3' ).object e1.title = 'Episode 1' e1.save() e2 = Episode.objects.get_or_create_for_url( p2, 'http://example.com/podcast1/e2.mp3' ).object e2.title = 'Episode 2' e2.save() e3 = Episode.objects.get_or_create_for_url( p2, 'http://example.com/podcast2/e2.mp3' ).object e3.title = 'Episode 3' e3.save() e4 = Episode.objects.get_or_create_for_url( p2, 'http://example.com/podcast2/e3.mp3' ).object e4.title = 'Episode 4' e4.save() User = get_user_model() user = User() user.username = '******' user.email = '*****@*****.**' user.set_password('secret') user.save() device1 = Client.objects.create(user=user, uid='dev1', id=uuid.uuid1()) device2 = Client.objects.create(user=user, uid='dev2', id=uuid.uuid1()) subscribe(p1, user, device1) unsubscribe(p1, user, device1) subscribe(p1, user, device1) subscribe(p2, user, device2) action1 = EpisodeHistoryEntry.create_entry(user, e1, EpisodeHistoryEntry.PLAY) action3 = EpisodeHistoryEntry.create_entry(user, e3, EpisodeHistoryEntry.PLAY) # we need that for later e3_id = e3.pk actions = Counter() # decide which episodes to merge groups = [(0, [e1]), (1, [e2, e3]), (2, [e4])] # carry out the merge pm = PodcastMerger([p1, p2], actions, groups) pm.merge() e1 = Episode.objects.get(pk=e1.pk) history1 = EpisodeHistoryEntry.objects.filter(episode=e1, user=user) self.assertEqual(len(history1), 1) # check if merged episode's id can still be accessed e3 = Episode.objects.filter(podcast=p1).get_by_any_id(e3_id) history3 = EpisodeHistoryEntry.objects.filter(episode=e3, user=user) self.assertEqual(len(history3), 1) p1 = Podcast.objects.get(pk=p1.get_id()) subscribed_clients = Client.objects.filter(subscription__podcast=p1) self.assertEqual(len(subscribed_clients), 2) episodes = p1.episode_set.all() self.assertEqual(len(episodes), 3)