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 save_note(user, url, date, tweet_id): soup = get_soup_by_url(url) if not soup: return tag = soup.find("div", {'class': 'highlightText'}) if not tag: return text = ''.join(tag.findAll(text=True)).strip() remark = '' tag = soup.find("div", {'class': 'note'}) if tag: remark = ''.join(tag.findAll(text=True)).replace('Note:', '').replace('@zzrt', '').strip() cover_tag = soup.find('div', {'class': 'cover'}) tag = cover_tag.find("span", {'class': 'title'}) if tag: book = ''.join(tag.findAll(text=True)).strip() if 'Personal Document' in book: book = '' else: book = '' tag = cover_tag.find("span", {'class': 'author'}) if tag: author = ''.join(tag.findAll(text=True)).replace(' by ', '').strip() else: author = '' if ' ' not in text \ and text[0] in string.ascii_letters \ and len(text) <= 64: if Word.objects.filter(word=text).count() == 0: Word.objects.create(user=user, url=url, word=text) else: note = Note(user=user, url=url, text=text) note.uuid = str(uuid.uuid4()) note.added = date or datetime.datetime.now() if remark: note.remark = remark[:128] if book: note.book = book[:128] if author: note.author = author[:128] note.save() # Delete this tweet and tweet it's content user_api = get_twitter_api(user=user) if not user_api: return if len(text) <= 140: status = text if len(status) + len(" #note") <= 140: status += " #note" if remark and len(status) + len(remark) <= 138: status = remark + ": " + status try: user_api.PostUpdates(status) user_api.DestroyStatus(tweet_id) except Exception, e: logger.info("Error: %s tweeted: %s, delete: %s", e, status, tweet_id) else:
note.author = author[:128] note.save() # Delete this tweet and tweet it's content user_api = get_twitter_api(user=user) if not user_api: return if len(text) <= 140: status = text if len(status) + len(" #note") <= 140: status += " #note" if remark and len(status) + len(remark) <= 138: status = remark + ": " + status try: user_api.PostUpdates(status) user_api.DestroyStatus(tweet_id) except Exception, e: logger.info("Error: %s tweeted: %s, delete: %s", e, status, tweet_id) else: # len(u".. #note http://t.co/al28lfq5xx") == 32; 140 - 32 = 108 status = text[:108] + ".. #note http://kindle.io" + note.get_absolute_url() status = shorten_status_urls(status) if len(status) > 140: status = text[:130] + "... #note" try: user_api.PostUpdates(status) user_api.DestroyStatus(tweet_id) except Exception, e: logger.info("Error: %s tweeted: %s, delete: %s", e, status, tweet_id)
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()