1 from collections
import defaultdict
, namedtuple
, Counter
2 from operator
import itemgetter
3 from datetime
import datetime
4 from random
import choice
5 from itertools
import chain
7 from mygpo
.core
.models
import Podcast
8 from mygpo
.decorators
import query_if_required
9 from mygpo
.core
.proxy
import proxy_object
10 from mygpo
.directory
.models
import Category
11 from mygpo
.db
.couchdb
.podcast
import podcasts_for_tag
12 from mygpo
.db
.couchdb
.directory
import top_categories
, category_for_tag
17 def __init__(self
, tag
):
21 def get_podcasts(self
):
22 """ Returns the podcasts with the current tag.
24 Some podcasts might be returned twice """
26 return podcasts_for_tag(self
.tag
)
30 TagCloudEntry
= namedtuple('TagCloudEntry', 'label weight')
35 def __init__(self
, total
=100, num_cat
=10, podcasts_per_cat
=10):
37 self
.num_cat
= num_cat
38 self
.podcasts_per_cat
= podcasts_per_cat
39 self
._categories
= None
43 def _needs_query(self
):
44 return self
._categories
is None
50 self
._categories
= top_categories(0, self
.num_cat
, True)
53 if self
.total
-self
.num_cat
> 0:
54 self
._tagcloud
= top_categories(self
.num_cat
, self
.total
-self
.num_cat
, False)
60 self
._tagcloud
.sort(key
= lambda x
: x
.label
.lower())
66 return max([e
.get_weight() for e
in self
.tagcloud
] + [0])
70 return min([e
.get_weight() for e
in self
.tagcloud
])
76 return self
._categories
79 def _prepare_category(self
, resp
):
80 category
= Category
.wrap(resp
['doc'])
81 category
= proxy_object(category
)
82 category
.podcasts
= category
.get_podcasts(0, self
.podcasts_per_cat
)
87 def update_category(podcast
):
88 all_tags
= list(chain
.from_iterable(s
for s
in podcast
.tags
.values()))
93 random_tag
= choice(all_tags
)
95 category
= category_for_tag(random_tag
)
97 category
= Category(label
=random_tag
)
99 category
.updated
= datetime
.utcnow()
101 category
.podcasts
= category
.podcasts
[:999]
103 # we don't need to CategoryEntry wrapper anymore
104 if any(isinstance(x
, dict) for x
in category
.podcasts
):
105 category
.podcasts
= filter(lambda x
: isinstance(x
, dict), category
.podcasts
)
106 category
.podcasts
= [e
['podcast'] for e
in category
.podcasts
]
108 if podcast
.get_id() in category
.podcasts
:
109 category
.podcasts
.remove(podcast
.get_id())
111 category
.podcasts
.insert(0, podcast
.get_id())
112 category
.label
= category
.label
.strip()