Add-on:Parsedom for xbmc plugins: Difference between revisions
>TobiasTheCommie No edit summary |
|||
Line 136: | Line 136: | ||
Returns string | Returns string | ||
clean_string = common.replaceHtmlCodes("&"…><' | clean_string = common.replaceHtmlCodes("&amp;&quot;&hellip;&gt;&lt;&#39; | ||
print clean_string # Prints &"...><' | print clean_string # Prints &"...><' | ||
Revision as of 12:08, 7 December 2011
Parsedom for xbmc plugins. | ||||||||||
|
Add-ons | Parsedom for xbmc plugins. |
Testing/Status
Integration and unittests are run continously by Jenkins
http://tc.tobiasussing.dk/jenkins/view/Common%20Functions/
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
params = common.getParameters(sys.argv[2]) # sys.argv[2] would be something like "?path=/root/favorites&login=true" print repr(params) # Prints '{ "path": "/root/favorites", "login": "true" }'
replaceHtmlCodes(self, str)
WARNING: This will be replaced with replaceHTMLCodes
Replaces html codes with ascii.
Returns string
clean_string = common.replaceHtmlCodes("&"…><' print clean_string # Prints &"...><'
stripTags(self, str)
Removes all DOM elements.
Returns string
clean_string = common.stripTags("I want this text <img src= alt='without this'>") print clean_string # Prints "I want this text"
makeAscii(self, str)
Removes non ascii characters
Returns string