Difference between revisions of "Update bookmarks from file"
From MythTV Official Wiki
m |
(from scratch rewrite to use the Python bindings) |
||
Line 1: | Line 1: | ||
− | This is | + | This script is part of a number of example scripts, intended to serve as a guide to using the new [[Python bindings]] rewritten for MythTV 0.23. |
− | + | This script will flush the existing list of websites from the MythBrowser, and supplement it with websites from an HTML file, as generated by Firefox's export utility. It accepts a filename on the command line, otherwise using '~/bookmarks.html'. | |
− | {{Python| | + | |
+ | {{Python|myth_video_scan.py| | ||
<pre> | <pre> | ||
− | # | + | #!/usr/bin/env python |
− | + | # -*- coding: UTF-8 -*- | |
+ | #---------------------- | ||
+ | |||
+ | import os | ||
+ | import sys | ||
+ | from HTMLParser import HTMLParser | ||
+ | try: | ||
+ | from MythTV import MythDB, DBDataWrite | ||
+ | except: | ||
+ | print 'ERROR: The python bindings are not installed' | ||
+ | sys.exit(-1) | ||
+ | |||
+ | class Bookmark( DBDataWrite ): | ||
+ | table = 'websites' | ||
+ | where = 'id=%s' | ||
+ | setwheredat = 'self.id,' | ||
+ | defaults = {'id':None, 'category':'Bookmark', 'name':u'', 'url':u''} | ||
+ | |||
+ | def create(self, data=None): | ||
+ | DBDataWrite.create(self, data) | ||
+ | self.wheredat = (self.id,) | ||
+ | self._pull() | ||
+ | return self | ||
+ | |||
+ | class Parser( HTMLParser ): | ||
+ | def __init__(self): | ||
+ | HTMLParser.__init__(self) | ||
+ | self.stack = [] | ||
+ | def handle_starttag(self, tag, attrs): | ||
+ | if tag == 'h3': | ||
+ | # new category, push to stack | ||
+ | self.stack.append('h3') | ||
+ | elif not len(self.stack): | ||
+ | # no categories, refuse links | ||
+ | return | ||
+ | elif tag == 'a': | ||
+ | # new bookmark, push to stack | ||
+ | d = dict(attrs) | ||
+ | self.stack.append(d['href']) | ||
+ | self.stack.append('a') | ||
+ | def handle_endtag(self, tag): | ||
+ | if not len(self.stack): | ||
+ | # empty stack, do nothing | ||
+ | return | ||
+ | elif tag == 'dl': | ||
+ | # drop out of category, pop from stack | ||
+ | self.stack.pop() | ||
+ | def handle_data(self, data): | ||
+ | if not len(self.stack): | ||
+ | # empty stack, do nothing | ||
+ | return | ||
+ | elif self.stack[-1] == 'h3': | ||
+ | # new category name, swap on stack | ||
+ | self.stack[-1] = data | ||
+ | elif self.stack[-1] == 'a': | ||
+ | # new link name, swap on stack | ||
+ | self.stack[-1] = data | ||
+ | # create new bookmark in database | ||
+ | b = Bookmark(db=db) | ||
+ | b.name = self.stack.pop() | ||
+ | b.url = self.stack.pop() | ||
+ | b.category = self.stack[-1] | ||
+ | b.create() | ||
+ | |||
+ | # select file | ||
+ | bookmark_file = '~/bookmarks.html' | ||
+ | if len(sys.argv) == 2: | ||
+ | bookmark_file = sys.argv[1] | ||
+ | |||
+ | # check if file exists | ||
+ | if not os.access(os.path.expanduser(bookmark_file), os.F_OK): | ||
+ | print 'ERROR: bookmark file could not be found' | ||
+ | sys.exit(-1) | ||
− | # | + | # connect to database |
− | + | try: | |
− | db = | + | db = MythDB() |
− | + | except: | |
+ | print 'ERROR: database could not be accessed' | ||
+ | sys.exit(-1) | ||
− | # | + | # flush old entries and reset counter |
− | + | c = db.cursor() | |
− | + | c.execute("""TRUNCATE TABLE websites""") | |
+ | c.close() | ||
− | + | # read in file | |
− | + | fp = open(os.path.expanduser(bookmark_file)) | |
− | + | data = ''.join(fp.readlines()) | |
− | + | fp.close() | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | # parse file for bookmarks | |
− | + | parser = Parser() | |
− | + | parser.feed(data) | |
− | |||
− | |||
</pre> | </pre> | ||
}} | }} | ||
− | + | [[Category:Scripts]] | |
[[Category:Python Scripts]] | [[Category:Python Scripts]] |
Revision as of 08:31, 30 March 2010
This script is part of a number of example scripts, intended to serve as a guide to using the new Python bindings rewritten for MythTV 0.23.
This script will flush the existing list of websites from the MythBrowser, and supplement it with websites from an HTML file, as generated by Firefox's export utility. It accepts a filename on the command line, otherwise using '~/bookmarks.html'.
#!/usr/bin/env python # -*- coding: UTF-8 -*- #---------------------- import os import sys from HTMLParser import HTMLParser try: from MythTV import MythDB, DBDataWrite except: print 'ERROR: The python bindings are not installed' sys.exit(-1) class Bookmark( DBDataWrite ): table = 'websites' where = 'id=%s' setwheredat = 'self.id,' defaults = {'id':None, 'category':'Bookmark', 'name':u'', 'url':u''} def create(self, data=None): DBDataWrite.create(self, data) self.wheredat = (self.id,) self._pull() return self class Parser( HTMLParser ): def __init__(self): HTMLParser.__init__(self) self.stack = [] def handle_starttag(self, tag, attrs): if tag == 'h3': # new category, push to stack self.stack.append('h3') elif not len(self.stack): # no categories, refuse links return elif tag == 'a': # new bookmark, push to stack d = dict(attrs) self.stack.append(d['href']) self.stack.append('a') def handle_endtag(self, tag): if not len(self.stack): # empty stack, do nothing return elif tag == 'dl': # drop out of category, pop from stack self.stack.pop() def handle_data(self, data): if not len(self.stack): # empty stack, do nothing return elif self.stack[-1] == 'h3': # new category name, swap on stack self.stack[-1] = data elif self.stack[-1] == 'a': # new link name, swap on stack self.stack[-1] = data # create new bookmark in database b = Bookmark(db=db) b.name = self.stack.pop() b.url = self.stack.pop() b.category = self.stack[-1] b.create() # select file bookmark_file = '~/bookmarks.html' if len(sys.argv) == 2: bookmark_file = sys.argv[1] # check if file exists if not os.access(os.path.expanduser(bookmark_file), os.F_OK): print 'ERROR: bookmark file could not be found' sys.exit(-1) # connect to database try: db = MythDB() except: print 'ERROR: database could not be accessed' sys.exit(-1) # flush old entries and reset counter c = db.cursor() c.execute("""TRUNCATE TABLE websites""") c.close() # read in file fp = open(os.path.expanduser(bookmark_file)) data = ''.join(fp.readlines()) fp.close() # parse file for bookmarks parser = Parser() parser.feed(data)