def update_feed(source):
	'''
	Updates the feeds from source if it has not been updated for UPDATE_INTERVAL hours
	'''
	logger.info("Checking if %s needs to be updated" % str(source))
	#Get the last update time
	if source:
		last_update = source.last_update
	updated_articles = []
	logger.debug("%s was last updated: %s" % (str(source), format_datetime(last_update)))

	#Update only if UPDATE_INTERVAL time has elapse since the last update
	if not last_update or timedelta(datetime.now(tzlocal()), last_update) >= timedelta(hours=UPDATE_INTERVAL):
		logger.info("Updating source %s" % str(source))
		feed = fp.parse(source.link)
		
		for entry in feed.get('entries'):
			#Try parsing the date and if it thows an exception, default to the current date
			try:
				pub_date = date.parse(entry.get('published'), default=datetime.now(tzlocal()))
			except:
				pub_date = datetime.now(tzlocal())

			link = entry.get('link')
			heading = entry.get('title')
			summary = entry.get('summary_detail').get('value')
			logger.debug("Parsed article: %s" % u''.join(heading).encode('utf-8').strip())

			#Check if article has already been added.
			article = get_object_or_none(Article, link=link)
			if not article:
				try:
					logger.debug("Article not found in database. Adding to database")
					article = Article.objects.create(pub_date=pub_date, link=link, source=source, heading=heading, summary=summary)
					article.tag_self()
					updated_articles.append(article)
				except Exception, e:
					logger.exception(e)

		#Update the last update time of this source
		source.last_update = datetime.now(tzlocal())
		source.save()
		logger.info("No of articles updated: %d" % len(updated_articles))
Example #2
0
def get_article(request, tab_id, article_no):
	logger.debug("Ajax request received.(Tab_id: %s, Article_no: %s)" % (tab_id, article_no))
	article = None
	success = False
	
	if request.user.is_authenticated():
		logger.info("Retreiving articles")
		
		articles = get_content(request.user, tab_id)
		
		logger.info("No of articles retreived: %d" % len(articles))
		
		if articles and len(articles) >= article_no:
			article = articles[article_no - 1]
			success = True

			logger.info("Article no %s found : %s" % (article_no, article.get_heading()))

			article = {
				'id': article.id,
				'title': article.get_heading(),
				'mlink': article.link,
				'link': reverse('newsreader:article', kwargs={'article_url': generate_seo_link(article.get_heading()), 'article_no': article.id}), 
				'pubDate': format_datetime(article.pub_date), 
				'summary': article.get_summary(), 
				'tags':[tag.name for tag in article.tags.all()]
			}
		else:
			article = {
				'no_articles': True
			}
	
	logger.debug("Sending response(%s)" % str(article))
	response = {'success': success, 'article': article}
	
	return simplejson.dumps(response)