"Virtual" feed method
I've been working on a recipe (Reader's Digest) with a big comprehensive RSS feed in it. I was getting annoyed that there were lots of recipe articles (the food recipes, not the Calibre ones) mixed into the main feed along with the "real" stories.
It took a while to get my head around the Feed and Article types in BasicRecipe, but I was able to write a PARSE_FEEDS that scans thru the existing feeds and then creats a separate TOC section for any article that contains the word "Recipe". Here's an example of what it does after normal feed retrieval:
As-is looks like:
Main Feed
Article a
Recipe a
Recipe b
Article b
New looks like:
Main Feed
Article a
Article b
Recipes
Recipe a
Recipe b
I have posted the code below. It's certainly not revolutionary, but could be useful for future recipes. Please, no giggling at my lack of Python skills!
BTW: Is there a sticky anywhere for posting reusable recipe code fragments? I'd definitely be interested in seeing what others have already done (without scanning thru all of the existing recipes).
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
|