Difference between revisions of "Update bookmarks from file"

From MythTV Official Wiki
Jump to: navigation, search
m (add script info and versioning)
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
This is some very basic [http://www.python.org Python] code to simply remove all the existing websites and replace them with ones from your bookmarks file.
+
{{Script info
 +
|author=Raymond Wagner
 +
|short=mythbrowser bookmark import
 +
|long=Imports a list of bookmarks from the HTML files generated by Firefox's export utility.
 +
|file=bookmarks.py
 +
|S24=yes}}
  
I'm not sure where to send this to make it available, hopefully this page will get spotted and shuffled off into the right place.
+
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'.
  
{{Box File|bookmarks.py|
+
{{Python|bookmarks.py|
 
<pre>
 
<pre>
#Assumes you're using MySQL
+
#!/usr/bin/env python
import MySQLdb
+
# -*- coding: UTF-8 -*-
 +
#----------------------
  
#------ Settings ---------
+
import os
bookmarksFile = "~/bookmarks.html"
+
import sys
db = MySQLdb.connect(host="mythdbserver", user="mythtv", passwd="mythtv", db="mythconverg")
+
from HTMLParser import HTMLParser
#-------------------------
+
try:
 +
    from MythTV import MythDB
 +
    from MythTV.database import DBDataWrite
 +
except:
 +
    print 'ERROR: The python bindings are not installed'
 +
    sys.exit(-1)
  
#Remove all existing website entries
+
class Bookmark( DBDataWrite ):
deleter = db.cursor()
+
    _table = 'websites'
deleter.execute("DELETE FROM websites")
+
    _defaults = {'id':None, 'category':u'', 'name':u'', 'url':u''}
 +
    _schema_value = 'BrowserDBSchemaVer'
 +
    _schema_local = 1002
 +
    _schema_name = 'MythBrowser'
  
bookmarks = open(bookmarksFile,"r+t")
+
class Parser( HTMLParser ):
cursor = db.cursor()
+
    def __init__(self):
urls=[]
+
        HTMLParser.__init__(self)
currentGroupName = "Auto"
+
        self.stack = []
for line in bookmarks:
+
    def handle_starttag(self, tag, attrs):
urlStart=line.find("http")
+
        if tag == 'h3':
if urlStart !=-1:
+
            # new category, push to stack
urlEnd= line.find('"',urlStart)
+
            self.stack.append('h3')
url = line[urlStart:urlEnd]
+
        elif not len(self.stack):
if not url in urls:
+
            # no categories, refuse links
descStart=line.find('>',urlEnd)
+
            return
descEnd = line.find('<',descStart)
+
        elif tag == 'a':
dsc=line[descStart+1:descEnd]
+
            # new bookmark, push to stack
cursor.execute("INSERT INTO websites (grp, dsc, url) VALUES ('" + currentGroupName + "','" + dsc + "','" + url + "')")
+
            d = dict(attrs)
urls.append(url)
+
            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()
  
groupNameFound = line.startswith("    <DT><H3")
+
# select file
if groupNameFound:
+
bookmark_file = '~/bookmarks.html'
groupNameStarts=line.find(">",11)
+
if len(sys.argv) == 2:
groupNameEnds=line.find("<",groupNameStarts)
+
    bookmark_file = sys.argv[1]
currentGroupName=line[groupNameStarts+1:groupNameEnds]
+
 
 +
# 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)
 
</pre>
 
</pre>
 
}}
 
}}
 +
 +
[[Category:Python Scripts]]

Latest revision as of 02:14, 12 November 2010


Author Raymond Wagner
Description Imports a list of bookmarks from the HTML files generated by Firefox's export utility.
Supports Version24.png  


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'.


PythonIcon.png bookmarks.py

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#----------------------

import os
import sys
from HTMLParser import HTMLParser
try:
    from MythTV import MythDB
    from MythTV.database import DBDataWrite
except:
    print 'ERROR: The python bindings are not installed'
    sys.exit(-1)

class Bookmark( DBDataWrite ):
    _table = 'websites'
    _defaults = {'id':None, 'category':u'', 'name':u'', 'url':u''}
    _schema_value = 'BrowserDBSchemaVer'
    _schema_local = 1002
    _schema_name = 'MythBrowser'

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)