refactor retrieval of devices
[mygpo.git] / mygpo / data / podcast.py
blob0ee2a561c14914232306fd38cf5c0dfc17a60820
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 collections import Counter
19 import logging
21 from django.conf import settings
23 from mygpo.db.couchdb.podcast_state import subscribed_users, \
24 subscribed_podcast_ids_by_user_id
25 from mygpo import pubsub
27 logger = logging.getLogger(__name__)
30 def calc_similar_podcasts(podcast, num=20):
31 """
32 calculates and returns a list of podcasts that seem to be similar
33 to the given one.
35 Probably an expensive operation
36 """
38 users = subscribed_users(podcast)
40 podcasts = Counter()
42 for user_id in users:
43 user_subscriptions = subscribed_podcast_ids_by_user_id(user_id)
44 user_counter = Counter(user_subscriptions)
45 podcasts.update(user_counter)
47 return podcasts.most_common(num)
50 def subscribe_at_hub(podcast):
51 """ Tries to subscribe to the given podcast at its hub """
53 if not podcast.hub:
54 return
56 base_url = settings.DEFAULT_BASE_URL
58 if not base_url:
59 logger.warn('Could not subscribe to podcast {podcast} '
60 'at hub {hub} because DEFAULT_BASE_URL is not '
61 'set.'.format(podcast=podcast, hub=podcast.hub))
62 return
64 logger.info('subscribing to {podcast} at {hub}.'.format(podcast=podcast,
65 hub=podcast.hub))
66 pubsub.subscribe(podcast.url, podcast.hub, base_url)