1 from collections
import Counter
5 from django
.conf
import settings
7 from mygpo
.podcasts
.models
import Podcast
8 from mygpo
.subscriptions
.models
import Subscription
9 from mygpo
.pubsub
import utils
11 logger
= logging
.getLogger(__name__
)
14 def calc_similar_podcasts(podcast
, num
=20, user_sample
=100):
15 """Get a list of podcasts that seem to be similar to the given one.
17 The similarity is based on similar subscriptions; for performance
18 reasons, only a sample of subscribers is considered"""
20 logger
.info("Calculating podcasts similar to {podcast}".format(podcast
=podcast
))
22 # get all users that subscribe to this podcast
24 Subscription
.objects
.filter(podcast
=podcast
)
27 .values_list("user", flat
=True)
30 "Found {num_subscribers} subscribers, taking a sample "
31 "of {sample_size}".format(
32 num_subscribers
=len(user_ids
), sample_size
=user_sample
36 # take a random sample of ``user_sample`` subscribers
37 user_ids
= list(user_ids
) # evaluate ValuesQuerySet
38 random
.shuffle(user_ids
)
39 user_ids
= user_ids
[:user_sample
]
41 # get other podcasts that the user sample subscribes to
43 for user_id
in user_ids
:
45 Podcast
.objects
.filter(subscription__user__id__in
=user_ids
)
47 .exclude(pk
=podcast
.pk
)
49 podcasts
.update(Counter(subscriptions
))
51 "Found {num_podcasts}, returning top {num_results}".format(
52 num_podcasts
=len(podcasts
), num_results
=num
56 return podcasts
.most_common(num
)
59 def subscribe_at_hub(podcast
):
60 """Tries to subscribe to the given podcast at its hub"""
65 base_url
= settings
.DEFAULT_BASE_URL
69 "Could not subscribe to podcast {podcast} "
70 "at hub {hub} because DEFAULT_BASE_URL is not "
71 "set.".format(podcast
=podcast
, hub
=podcast
.hub
)
76 "subscribing to {podcast} at {hub}.".format(podcast
=podcast
, hub
=podcast
.hub
)
78 utils
.subscribe(podcast
, podcast
.url
, podcast
.hub
, base_url
)