View Single Post
Old 01-18-2010, 01:25 PM   #2
BrianG
Member
BrianG began at the beginning.
 
Posts: 23
Karma: 22
Join Date: Dec 2009
Device: Kindle DX
Create a "virtual feed"

The PARSE_FEEDS method below is used to create a virtual feed from an RSS feed. in this case, it uses the PARSE_FEEDS method to:

Get all articles associated with the feed via the BasicRecipe methods
Scan thru the articles and find titles that contain the word "recipe"
Delete these articles from their existing feed
Create a new feed section called "recipes" to hold these articles


As-is looks like:
Code:
Main Feed
    Article a
    Recipe a
    Recipe b
    Article b
New looks like:
Code:
Main Feed
    Article a
    Article b
Recipes
    Recipe a
    Recipe b
This can be used to break up large RSS feeds or to create sub-topics, etc.
Spoiler:

Code:
from calibre.web.feeds.recipes import BasicNewsRecipe
from calibre.web.feeds import Feed

    def parse_feeds (self): 

	# Do the "official" parse_feeds first
	feeds = BasicNewsRecipe.parse_feeds(self) 


	# Loop thru the articles in all feeds to find articles with "recipe" in it
	recipeArticles = []
	for curfeed in feeds:
		delList = []
		for a,curarticle in enumerate(curfeed.articles):
			if curarticle.title.upper().find('RECIPE') >= 0:
				recipeArticles.append(curarticle)
				delList.append(curarticle)
		if len(delList)>0:
			for d in delList:
				index = curfeed.articles.index(d)
				curfeed.articles[index:index+1] = []


	# If there are any recipes found, create, append a new Feed object
 	if len(recipeArticles) > 0:
		pfeed = Feed()
		pfeed.title = 'Recipes'
		pfeed.descrition = 'Recipe Feed (Virtual)'
        	             pfeed.image_url  = None
		pfeed.oldest_article = 30
        	             pfeed.id_counter = len(recipeArticles)
		# Create a new Feed, add the recipe articles, and append 
                         # to "official" list of feeds
		pfeed.articles = recipeArticles[:]
		feeds.append(pfeed)

	return feeds

Last edited by Starson17; 02-25-2011 at 10:06 PM.
BrianG is offline   Reply With Quote