Add-on:Parsedom for xbmc plugins
Parsedom for xbmc plugins. | ||||||||||
|
Add-ons | Parsedom for xbmc plugins. |
Developers
This DOM parser is a fast replacement for Beautiful Soup.
And also a few other useful functions.
Setup
To use the cacheing service add the following to your py file.
import xbmc, xbmcvfs, xbmcaddon import CommonFunctions common = CommonFunctions.CommonFunctions() common.plugin = plugin
Debugging
To enable debugging set the following values in default.py
common.dbg = True # Default common.dbglevel = 3 # Default
Whenever you debug your own code you should also debug in the cache. Otherwise you should remember to DISABLE it.
parseDOM(self, html, name = "", attrs = {}, ret = False)
- html - String to parse.
- name - Element to match ( for instance "span" )
- attrs - Dictionary with attributes you want matched in the elment ( for instance { "id": "span3", "class": "oneclass.*anotherclass", "attribute": "a random tag" } )
- ret - Attribute in element to return value of. If not set(or False), returns content of DOM element.
returns list
_fetchPage(self, dict)
WARNING: This call will be changed from "_fetchPage" to "fetchPage" in the future.
Fetches a page from the internet.
returns tupple of ( dict, int).
An int of 200 indicates success, and 500 error.
The dict returned contains
- content: HTML content
- new_url: Redirect url
- header: Header information
result = common._fetchPage({"url": "http://www.example.com/index.html"}) if result["status"] == 200: print "content: %s" %result["content"] result = common._fetchPage({"url": "http://www.example.com/doesnotexist.html"}) if result["status"] == 500: print "redirect url: %s" %result["new_url"] print "header: %s" %result["header"] print "content: %s" %result["content"]
log(self, str, level = 0)
Sends the string to the xbmc.log function if the level provided is less than the level set in dbglevel.
Returns None
import CommonFunctions common = CommonFunctions.CommonFunctions() common.plugin = "PluginName" common.dbg = True common.dbglevel = 3
def helloWorld(): common.log("Ran this") common.log("Ignored this", 4) common.log("Ran this as well", 2) helloWorld()
Will give the following output in the xbmc.log
[PluginName] helloWorld : 'Ran This' [PluginName] helloWorld : 'Ran This as well'
openFile(self, filepath, options = "w")
Opens a binary or text file handle
Returns filehandle
file = common.openFile("myfile.txt", "wb") file.write("my data") file.close()
This function raises a keyboard for user input
Returns string
search = common.getUserInput("Artist", "") # Will ask the user to write an Artist to search for def_search = common.getUserInput("Artist", "Miley Cyrus") # Will default to Miley Cyrus if the user doesn't enter another artist.
Warning: This will return string in the future.
This function raises a keyboard numpad for user input
Returns int
pin = common.getUserInput("Userpin", "") # Will ask the user to write a pin. def_pin = common.getUserInput("Userpin", "1234") # Will default to 1234 if the user doesn't enter another pin.
getParameters(self, dict)
Converts the request url passed on by xbmc to the plugin into a dict of key-value pairs
returns dict
replaceHtmlCodes(self, str)
Replaces html codes like "…" with "...", Besides for hellip it replaces: "\"&<>'"
Returns string
stripTags(self, str)
Removes all DOM elements.
Returns string
makeAscii(self, str)
Removes non ascii characters
Returns string