def scrape(request, tvdb_id): """ Takes a scrape request, constructs a Scraper object and performs a scrape for the show if it hasn't been scraped before or hasn't been scraped within the last :math:`x` days (where :math:`x` is the number of days specified by RESCRAPE_AFTER). Otherwise if the show exists and has been scraped within the last :math:`x` days redirect to the appropriate show page :param request: A Scrape request object. :param tvdb_id: The id of the tv show to be scraped (or shown) :return: A HttpResponse Object containing the page of the show requested. """ # Determine if the show already exists in the datastore q = TVShow.get_by_key_name(tvdb_id) if users.is_current_user_admin() and 'force' in request.GET and request.GET[ 'force'] == '1': Scraper(tvdb_id, rescrape=True, options=q.options) return HttpResponseRedirect('/show/{0}'.format(q.url_string)) # Check if the show has been scraped before and if that scrape was in the last x days specified by RESCRAPE_AFTER if q and q.last_scraped > datetime.now() - timedelta(days=RESCRAPE_AFTER): url_slug = q.url_string else: # If scraping is switched on then scrape the show if settings.SCRAPING: s = Scraper(tvdb_id) url_slug = s.get_url_slug() else: url_slug = tvdb_id return HttpResponseRedirect('/show/{0}'.format(url_slug))