remove unnecessary imports
[mygpo.git] / mygpo / data / podcast.py
blob8a462b305005aeb8fed4acf3bfa7a40a40875fcf
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 django.contrib.auth.models import User
19 from mygpo.api.models import Episode, Subscription
20 from mygpo.data.models import PodcastTag
22 def avg_update_interval(podcast):
23 """
24 returns the average interval between episodes for a given podcast
25 """
26 unique_timestamps = Episode.objects.filter(podcast=p, timestamp__isnull=False).order_by('timestamp').values('timestamp').distinct()
27 c = unique_timestamps.count()
28 t1 = unique_timestamps[0]['timestamp']
29 t2 = unique_timestamps[c-1]['timestamp']
30 return max(1, (t2 - t1).days / c)
33 def calc_similar_podcasts(podcast):
34 """
35 calculates and returns a list of podcasts that seem to be similar
36 to the given one.
38 Probably an expensive operation
39 """
40 tags = [t.tag for t in PodcastTag.objects.filter(podcast=podcast).only('tag').distinct()]
41 users = User.objects.filter(subscription__podcast=podcast).only('id').distinct()
42 subscribed = Subscription.objects.filter(user__in=users).only('podcast')
43 podcast_list = {}
44 for s in subscribed:
45 if s.podcast == podcast:
46 continue
47 podcast_list[s.podcast] = podcast_list.get(s.podcast, 0) + 1
49 for p in podcast_list.iterkeys():
50 ps_tags = [t.tag for t in PodcastTag.objects.filter(podcast=p).only('tag').distinct()]
51 matching_tags = filter(lambda t: t in tags, ps_tags)
52 podcast_list[p] = podcast_list[p] * max(len(matching_tags), 1)
54 l = list(podcast_list.iteritems())
55 l.sort(key=lambda (p, count): count, reverse=True)
56 return l