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): # 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