def add(request): context = {} context['page_title'] = 'Add Podcast' id = request.GET.get('id', '').strip() if id: podcast = get_object_or_404(Podcast, id=id) if not podcast.image and podcast.image_url: podcast.download_image() if not Episode.objects.filter(podcast=podcast).exists(): download_episodes(podcast) url = reverse('podcasttime:index') + '#ids={}'.format(podcast.id) return redirect(url) search = request.GET.get('search', '').strip() context['search'] = search if search: podcasts = [] matches = itunes_search(search, attribute='titleTerm') for result in matches['results']: pod = { 'image_url': result['artworkUrl600'], 'itunes_url': result['collectionViewUrl'], 'artist_name': result['artistName'], 'tags': result['genres'], 'name': result['collectionName'], # 'feed_url': result['feedUrl'], } try: podcast = Podcast.objects.get( url=result['feedUrl'], name=result['collectionName'] ) except Podcast.DoesNotExist: podcast = Podcast.objects.create( name=result['collectionName'], url=result['feedUrl'], itunes_lookup=result, image_url=result['artworkUrl600'], ) # episodes will be created and downloaded by the cron job redownload_podcast_image.delay(podcast.id) pod['id'] = podcast.id pod['url'] = reverse( 'podcasttime:podcast_slug', args=(podcast.id, podcast.get_or_create_slug()) ) podcasts.append(pod) context['found'] = matches['resultCount'] context['podcasts'] = podcasts return render(request, 'podcasttime/add.html', context)
def download_episodes_task(podcast_id, verbose=True): try: podcast = Podcast.objects.get(id=podcast_id) except Podcast.DoesNotExist: print("Warning! Podcast with id {} does not exist".format(podcast_id)) return try: download_episodes(podcast, verbose=verbose) except NotFound as exception: PodcastError.create(podcast) if isinstance(exception, bytes): podcast.error = exception.decode("utf-8") else: podcast.error = str(exception) podcast.save()
def _handle(self, *args, **kwargs): podcasts = Podcast.objects.filter(error__isnull=False) self.out(podcasts.count(), "podcasts with errors") acceptable_exceptions = ( TooManyRedirects, ConnectionError, BadPodcastEntry, NotFound, BadEpisodeDurationError, ) for podcast in podcasts.order_by("?")[:10]: print((podcast.name, podcast.id)) print("ERROR (before)", repr(podcast.error[:100])) try: download_episodes(podcast, timeout=20) podcast.refresh_from_db() print("ERROR (after)", bool(podcast.error)) print() except acceptable_exceptions as exception: print("Fetch error ({!r}): {}".format(podcast, exception))
def download_episodes_task(podcast_id, verbose=True): podcast = Podcast.objects.get(id=podcast_id) download_episodes(podcast, verbose=verbose)