View Single Post
Old 12-08-2011, 01:50 AM   #5
mlt
Junior Member
mlt began at the beginning.
 
Posts: 2
Karma: 10
Join Date: Dec 2011
Device: Kindle DX graphite
Question

I was looking at a sample code. However I still don't get it how to fetch things if I don't have epub among other formats in the first place?

I ended up writing standalone python code that fetches all I want and calls `calibredb add`. I'd appreciate if someone can suggest as how to convert it into a recipe.
Spoiler:
Code:
#!/usr/bin/python
# vim: set fileencoding=utf-8 :

import feedparser, urllib2, tempfile, os
from BeautifulSoup import BeautifulSoup

feed_url = "http://dotu.ru/feed"
feed = feedparser.parse(feed_url)

for item in feed["items"]:
    title = item["title"]
    print """Fetching "%s" """ % title
    summary = urllib2.urlopen(item["link"])
    soup = BeautifulSoup(summary)
    downloadText = soup.find(text=u"Скачать fb2")
    if None != downloadText:
        a = downloadText.findParent(name='a')
        file = tempfile.NamedTemporaryFile(suffix='.fb2')
        url = a["href"]
        try:
            response = urllib2.urlopen(url)
            file.write(response.read())
            file.flush()
            cmd = u"calibredb add %s" % file.name
            cmd2 = cmd.encode('utf-8')
            os.system(cmd2)
            file.close()
        except:
            print "Failed to download %s" % url
mlt is offline   Reply With Quote