def handle(self, *args, **options): docs = set() for username in options.get('users', []): user = User.get_user(username) # User docs.add(user._id) # Suggestions suggestions = Suggestions.for_user(user) docs.add(suggestions._id) # Podcast States for p_state in PodcastUserState.for_user(user): docs.add(p_state._id) # Categories for tag in p_state.tags: c = Category.for_tag(tag) if c: docs.add(c._id) # Podcast podcast = Podcast.get(p_state.podcast) docs.add(podcast._id) # Categories for s in podcast.tags: for tag in podcast.tags[s]: c = Category.for_tag(tag) if c: docs.add(c._id) # Episodes for episode in podcast.get_episodes(): docs.add(episode._id) # Episode States e_state = episode.get_user_state(user) if e_state._id: docs.add(e_state._id) db = get_main_database() docs = sorted(docs) self.dump(docs, db)
def __iter__(self): lists = PodcastList.by_rating(endkey=self.min_list_rating) lists = islice(lists, 0, self.num_lists) lists = map(self._prepare_list, lists) categories = Category.top_categories(self.num_categories) categories = map(self._prepare_category, categories) return chain(lists, categories)
def tag_podcasts(request, tag, count): count = parse_range(count, 1, 100, 100) category = Category.for_tag(tag) if not category: return JsonResponse([]) domain = RequestSite(request).domain query = category.get_podcasts(0, count) resp = map(lambda p: podcast_data(p, domain), query) return JsonResponse(resp)
def update_category(podcast): all_tags = list(chain.from_iterable(s for s in podcast.tags.values())) if not all_tags: return random_tag = choice(all_tags) category = category_for_tag_uncached(random_tag) if not category: category = Category(label=random_tag) category.updated = datetime.utcnow() category.podcasts = category.podcasts[:999] # we don't need to CategoryEntry wrapper anymore if any(isinstance(x, dict) for x in category.podcasts): category.podcasts = filter(lambda x: isinstance(x, dict), category.podcasts) category.podcasts = [e['podcast'] for e in category.podcasts] if podcast.get_id() in category.podcasts: category.podcasts.remove(podcast.get_id()) category.podcasts.insert(0, podcast.get_id()) category.label = category.label.strip() save_category(category)
def handle(self, *args, **options): if len(args) < 2: print """ Merges multiple categories into one by listing them as alternative spellings Usage: ./manage.py category-merge-spellings <category> <spelling1> [<spelling2> ...] """ return start_time = datetime.utcnow() cat_name = args[0] spellings = args[1:] print "Adding new spellings for %s ..." % cat_name category = category_for_tag(cat_name) if not category: print " creating new category %s" % cat_name category = Category() category.label = cat_name for spelling in spellings: new_cat = category_for_tag(spelling) if spelling == cat_name or (spelling in category.spellings): print " skipped %s: already in category" % spelling continue if not new_cat: #merged category doesn't yet exist category.spellings.append(spelling) elif new_cat and category._id == new_cat._id: print " set %s as new label" % cat_name category.spellings = list(set([x for x in category.spellings + [category.label] if x != cat_name])) category.label = cat_name else: print " add spelling %s" % spelling category.spellings = list(set(category.spellings + [new_cat.label] + new_cat.spellings)) category.merge_podcasts(new_cat.podcasts) new_cat.delete() category.updated = start_time save_category(category)
def category(request, category, page_size=20): category = Category.for_tag(category) if not category: return HttpResponseNotFound() # Make sure page request is an int. If not, deliver first page. try: page = int(request.GET.get("page", "1")) except ValueError: page = 1 entries = category.get_podcasts((page - 1) * page_size, page * page_size) podcasts = filter(None, entries) num_pages = len(category.podcasts) / page_size page_list = utils.get_page_list(1, num_pages, page, 15) return render(request, "category.html", {"entries": podcasts, "category": category.label, "page_list": page_list})
def handle(self, *args, **options): # couchdbkit doesn't preserve microseconds start_time = datetime.utcnow().replace(microsecond=0) excluded_tags = settings.DIRECTORY_EXCLUDED_TAGS tags = args or Tag.all() for n, tag in enumerate(tags): if not isinstance(tag, basestring): tag = str(tag) label = utils.remove_control_chars(tag.strip()) if not label: continue tag_obj = Tag(tag) podcast_ids, weights = utils.unzip(list(tag_obj.get_podcasts())) podcast_objs = Podcast.get_multi(podcast_ids) podcasts = [] for podcast, weight in zip(podcast_objs, weights): e = CategoryEntry() e.podcast = podcast.get_id() e.weight = float(weight * podcast.subscriber_count()) podcasts.append(e) category = Category.for_tag(label) if not category: if not label or label in excluded_tags: continue category = Category() category.label = label category.spellings = [] # delete if it has been excluded after it has been created if label in excluded_tags: category.delete() continue # we overwrite previous data if category.updated != start_time: category.podcasts = [] category.merge_podcasts(podcasts) category.updated = start_time if 'weight' in category: del category['weight'] category.save() try: utils.progress(n % 1000, 1000, category.label.encode('utf-8')) except: pass
def _category_wrapper(r): c = Category() c.label = r['value'][0] c._weight = r['value'][1] return c
def _prepare_category(self, resp): category = Category.wrap(resp['doc']) category = proxy_object(category) category.podcasts = category.get_podcasts(0, self.podcasts_per_cat) return category