def home(request): if request.method == "POST": url = request.POST.get("url") if not url.startswith('http'): url = 'http://' + url if '.' not in url: messages.error(request, "Need a valid URL.") return HttpResponseRedirect("/") try: bf = BriticleFile(url, settings.KINDLE_LIVE_DIR) except Exception, e: if isinstance(e, URLError) or 'timed out' in str(e): messages.error(request, "Open URL Error, Try again later.") return HttpResponseRedirect("/") elif isinstance (e, UnicodeEncodeError): logger.info("UnicodeEncodeError: %s URL: %s" % (e, url)) messages.error(request, "Internal Error when fetch the URL.") return HttpResponseRedirect("/") raise doc = bf.save_to_mobi(sent_by="Kindle.io") if os.path.exists(doc): if not settings.DEBUG: send_to_kindle(request, [doc], subject=bf.title) os.remove(doc) messages.success(request, "The doc has been sent to your kindle successfully!") return HttpResponseRedirect("/") else: messages.error(request, "Error: No file generated for this URL.") return HttpResponseRedirect("/")
def update_news(self, article_list): """ Update HackerNews records """ count_created = count_updated = count_filed = 0 for article in article_list: if self.filter(url=article['url']).exists(): news = self.get(url=article['url']) # Update the points of the article, send the points update signal if news.filed and news.file_path and article['points'] > news.points: pre_points = news.points news.points = article['points'] news.save() signals.points_updated.send(sender=news, pre_points=pre_points) count_updated += 1 # If the article is abort before. Ignore it. # We can check the error log for reasons if news.aborted: continue else: # Create an new one if it does not exist news = self.create(url=article['url'], points=article['points'], title=smart_str(article['title'])) count_created += 1 # Save article whose points is high enough into file if (not news.filed) and article['points'] >= POINTS_LIMIT_TO_SAVE: try: year, week_number, _ = datetime.date.today().isocalendar() dir_hackernews = settings.HACKER_NEWS_DIR if not os.path.exists(dir_hackernews): os.mkdir(dir_hackernews) dir_year = os.path.join(dir_hackernews, str(year)) if not os.path.exists(dir_year): os.mkdir(dir_year) dir_week = os.path.join(dir_year, "%02d" % week_number) if not os.path.exists(dir_week): os.mkdir(dir_week) bf = BriticleFile(news.url, dir_week) except Exception, e: if isinstance(e, URLError) or 'timed out' in str(e): logger.error("URLError or Time out Exception: %s URL: %s" % (e, news.url)) news.aborted = True news.save() continue elif isinstance (e, UnicodeEncodeError): logger.error("UnicodeEncodeError: %s URL: %s" % (e, news.url)) news.aborted = True news.save() continue raise # Abort if there is not content if bf.is_empty(): logger.info("No content found for: %s" % news.url) news.aborted = True news.save() continue try: mobi = bf.save_to_mobi(title=news.title, sent_by="Kindle.io") except Exception, e: logger.error("Failed while calling bf.save_to_mobi(). %s: %s URL: %s" % \ (e.__class__, e, news.url)) news.aborted = True news.save() continue if mobi: news.filed = True news.file_path = mobi news.html = bf.html news.save() signals.file_saved.send(sender=news) count_filed += 1 else: logger.error("Failed to save file. URL: %s" % news.url) news.aborted = True news.save()