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
22 from collections
import Counter
24 from mygpo
.counter
import Counter
27 def calc_similar_podcasts(podcast
, num
=20):
29 calculates and returns a list of podcasts that seem to be similar
32 Probably an expensive operation
35 db
= get_main_database()
37 res
= db
.view('subscriptions/by_podcast',
38 startkey
= [podcast
.get_id(), None, None],
39 endkey
= [podcast
.get_id(), {}, {}],
42 stale
= 'update_after',
45 users
= (r
['key'][1] for r
in res
)
51 subscribed
= db
.view('subscriptions/by_user',
52 startkey
= [user_id
, True, None, None],
53 endkey
= [user_id
, True, {}, {}],
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
)