root/hodgestar/PythonCode/xbel2deli/xbel2deli.py

Revision 376, 1.9 kB (checked in by simon, 4 years ago)

Import xbel bookmarks into del.icio.us.

  • Property svn:mime-type set to text/python-source
  • Property svn:eol-style set to native
Line 
1import delicious
2import xml.dom.minidom
3import sys
4import datetime
5import time
6
7dom = xml.dom.minidom
8
9class XbelBookmark(object):
10    def __init__(self,oNode):
11        self.__oBN = oNode
12       
13    def url(self):
14        return self.__oBN.getAttribute('href')
15       
16    def folder(self):
17        oP = self.__oBN.parentNode
18        oT = iter(oP.getElementsByTagName('title')).next()
19        return self.__toText(oT.childNodes)
20       
21    def title(self):
22        oT = iter(self.__oBN.getElementsByTagName('title')).next()
23        return self.__toText(oT.childNodes)
24       
25    def __toText(self,aNodes):
26        return "".join([oN.data for oN in aNodes if oN.nodeType == oN.TEXT_NODE])
27
28    def __str__(self):
29        return self.folder() + ": " + self.url()
30
31class Xbel(object):
32    def __init__(self,sFile):
33        self.__oXbel = dom.parse(sFile)
34       
35    def bookmarks(self):
36        for oElem in self.__oXbel.getElementsByTagName('bookmark'):
37            yield XbelBookmark(oElem)
38           
39def usage(aArgs):
40    print aArgs[0], "<del.icio.us user>", "<password>", "<xbel file>"
41
42def main(aArgs):
43    if len(aArgs) != 4:
44        usage(aArgs)
45        return 1
46
47    sUser = aArgs[1]
48    sPass = aArgs[2]
49    sIn = aArgs[3]
50   
51    oXbel = Xbel(sIn)
52    oDeli = delicious.DeliciousAPI(sUser,sPass)
53   
54    oTS = datetime.datetime.utcnow()
55    oTS = oTS.replace(microsecond=0)
56    sTimestamp = oTS.isoformat() + 'Z'
57   
58    for oB in oXbel.bookmarks():
59        sUrl = oB.url().encode('ascii','replace')
60        sDescription = oB.title().encode('ascii','replace')
61        sTags = oB.folder().encode('ascii','replace')
62        sExtended = "extended"
63
64        oDeli.posts_add(sUrl,sDescription,sExtended,sTags,sTimestamp)
65        time.sleep(2)
66       
67        # print sUrl,sDescription,sExtended,sTags,sTimestamp
68
69    return 0
70   
71if __name__ == '__main__':
72    sys.exit(main(sys.argv))
Note: See TracBrowser for help on using the browser.