fix imports of mygpo.couch
[mygpo.git] / mygpo / data / podcast.py
blob14b51dc83575087ad3cab858c60fc24a12fb451d
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 mygpo.core.models import Podcast
19 from mygpo.couch import get_main_database
21 try:
22 from collections import Counter
23 except ImportError:
24 from mygpo.counter import Counter
27 def calc_similar_podcasts(podcast, num=20):
28 """
29 calculates and returns a list of podcasts that seem to be similar
30 to the given one.
32 Probably an expensive operation
33 """
35 db = get_main_database()
37 res = db.view('subscriptions/by_podcast',
38 startkey = [podcast.get_id(), None, None],
39 endkey = [podcast.get_id(), {}, {}],
40 group = True,
41 group_level = 2,
42 stale = 'update_after',
45 users = (r['key'][1] for r in res)
47 podcasts = Counter()
50 for user_id in users:
51 subscribed = db.view('subscriptions/by_user',
52 startkey = [user_id, True, None, None],
53 endkey = [user_id, True, {}, {}],
54 group = True,
55 group_level = 3,
56 stale = 'update_after',
58 user_subscriptions = set(r['key'][2] for r in subscribed)
59 user_counter = Counter(user_subscriptions)
60 podcasts.update(user_counter)
63 return podcasts.most_common(num)