|
View Poll Results: Would an allrecipes.com plugin interest you? | |||
Yes | 7 | 77.78% | |
No | 2 | 22.22% | |
Voters: 9. You may not vote on this poll |
|
Thread Tools | Search this Thread |
12-27-2010, 10:16 PM | #1 |
Junior Member
Posts: 4
Karma: 10
Join Date: Dec 2010
Device: Kindle 3
|
Looking to make a plugin, need some guidance getting started
Hello all, I am looking to write a Calibre Plugin to allow users to input a URL from allrecipes.com, and automatically convert the recipe from the url to .mobi format. I have already written the hard part, which takes the URL as a cli arg and outputs only the relevant bits as a valid HTML doc. All I need to do now is integrate my current code into the plugin. Unfortunately, I have only used Python for CLI programs in the past, so I have no idea how to create the GUI. Anyone have any pointers to help me get started?
As a recap: I want to build a very basic GUI Plugin and need some guidance, the backend is pretty much ready. |
12-27-2010, 10:59 PM | #2 |
Calibre Plugins Developer
Posts: 4,673
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
I've written a bunch of GUI plugins which you can download the source code of in this thread
If you read the first post in the thread it also has some links to some useful help topics etc. I'm happy to give some help if you need it, either send me a PM or post on here for others to also assist. I hadn't written any Python at all before starting to dabble with Calibre and got enough to get by pretty quickly. |
12-28-2010, 01:32 PM | #3 |
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Your other option is to use the Calibre recipe (code) system to grab recipes (food) from allrecipes.com. Although the recipe (code) system of Calibre is not designed for simple user input when a recipe (code) runs, you can load the URL (or a portion thereof) via the username password field, via modifying the recipe to add a list of links, or through a Python coded interface.
|
12-28-2010, 01:39 PM | #4 | |
Junior Member
Posts: 4
Karma: 10
Join Date: Dec 2010
Device: Kindle 3
|
Quote:
I have written the following methods to input a URL (from allrecipes.com), and return a concise HTML document containing the recipe: *Note: This script depends on BeautifulSoup. Code:
import urllib2, sys, string from BeautifulSoup import BeautifulSoup def buildHTMLOutput(title, author, ingredientList, directionList): output = "" titleAuthor = "%s by %s" % (string.capwords(title), string.capwords(author)) header = "<html><head><title>%s</title></head><body><h2>%s</h2>" % (titleAuthor, titleAuthor) ingredients = "<h2>Ingredients:</h2>" for ingredient in ingredientList: if ingredient != "": ingredients += "<li>%s</li>" % (ingredient) directions = "<h2>Directions:</h2>" for direction in directionList: if direction != "": directions += "<li>%s</li>" % (direction) ingredientSection = "<ul>%s</ul>" % (ingredients) directionSection = "<ol>%s</ol>" % (directions) footer = "</body></html>" output = "%s\n%s\n%s\n%s" % (header, ingredientSection, directionSection, footer) return output def parseRecipeFromLink(url): try: ingredientList = [] directionList = [] req = urllib2.Request(url) response = urllib2.urlopen(req) pageText = response.read() htmlParser = BeautifulSoup(''.join(pageText)) ingredientDiv = BeautifulSoup(htmlParser.find('div', "ingredients").prettify()) ingredients = ingredientDiv.findAll('li') for ingredient in ingredients: for i in ingredient.findAll(text=True): ingredientList.append(i.replace('\n', '').strip()) directionDiv = BeautifulSoup(htmlParser.find('div', "directions").prettify()) directions = directionDiv.findAll('li') for step in directions: for i in step.findAll(text=True): directionList.append(i.replace('\n', '').strip()) authorDiv = BeautifulSoup(htmlParser.find('div', "author-name").prettify()) spans = authorDiv.findAll('span') for span in spans: author = span.findAll(text=True)[0].replace('\n', '').strip() titleArea = BeautifulSoup(htmlParser.find('h1', {"id" : "itemTitle"}).prettify()) title = titleArea.findAll(text=True)[1].replace('\n', '').strip() output = buildHTMLOutput(title, author, ingredientList, directionList) soup = BeautifulSoup(output) return soup.prettify() except Exception, detail: print "Error: ", detail if __name__ == "__main__": if (len(sys.argv) == 1): print "No URL was specified" quit = 1 else: if "http://allrecipes.com/" in sys.argv[1]: url = sys.argv[1] else: print "The URL must be from allrecipes.com" quit = 1 if quit != 1: print parseRecipeFromLink(url)
So first thing's first: Have I missed any crucial steps or is there anything I need to consider before I proceed? |
|
12-28-2010, 03:17 PM | #5 |
Calibre Plugins Developer
Posts: 4,673
Karma: 2162246
Join Date: Oct 2010
Location: Australia
Device: Kindle Oasis
|
I would think the first thing would be to consider Starson17's suggestion of whether a "recipe" approach is more suitable than a UI plugin. I know absolutely nothing about recipes having never used them which is why I did not mention the option myself. However if there is indeed a way to incorporate a way of the URL being supplied by the user into that which Starson17 indicates some options for then that sounds like a much better approach given it does most of what you need?
|
12-29-2010, 08:58 PM | #6 | ||
Junior Member
Posts: 4
Karma: 10
Join Date: Dec 2010
Device: Kindle 3
|
Quote:
Quote:
|
||
12-30-2010, 07:54 AM | #7 | ||
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Quote:
Quote:
|
||
12-30-2010, 10:58 AM | #8 |
Junior Member
Posts: 4
Karma: 10
Join Date: Dec 2010
Device: Kindle 3
|
As someone who has little experience with Python, I have never done any type of GUI in normal Python (I have used IronPython, C#, or Java for GUI stuff). I am not exactly sure how to get the input from the user, but a link to the thread you were talking about would be very helpful (I was unable to find it on the forum) Thanks for all of the help!
|
12-30-2010, 12:31 PM | #9 | |
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Quote:
|
|
Tags |
help needed, plugin, plugin development |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
New Plugin Type Idea: Library Plugin | cgranade | Plugins | 3 | 09-15-2010 12:11 PM |
Ebook advice and guidance | Radioteacher | Workshop | 2 | 01-17-2010 04:43 PM |
New to Linux, need some guidance! | KindleMan | Sony Reader Dev Corner | 6 | 03-10-2008 11:53 AM |
Guidance needed | Tom Storer | Which one should I buy? | 7 | 12-03-2007 02:09 AM |