Google chrome bookmarks to html and then to database


/ Published in: Python
Save to your folder(s)

Display the google chrome bookmarks to html and then save to database.


Copy this code and paste it in your HTML
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. # Chrome Bookmark Exporter, tested with OS X Chrome 7.0.517.41
  5. # Jan Eden <software (a) janeden net>
  6. # based on a Groovy script by Dan Fraser [http://www.capybara.org/~dfraser/archives/355]
  7. # This file is public domain. Python 2.6 or newer required
  8.  
  9. import json
  10. import datetime
  11. import cgi
  12. import os
  13. import sys
  14. import codecs
  15. from datetime import datetime
  16. import datetime
  17. import time
  18. import psycopg2
  19.  
  20. hostname = 'xyz'
  21. username = 'abc'
  22. password = '****'
  23. database = 'database_name'
  24.  
  25.  
  26. user = os.getlogin()
  27.  
  28.  
  29. def doQuery( conn, name, url, ct ) :
  30. cur = conn.cursor()
  31. ct = int(ct)
  32. cur.execute( "INSERT INTO personal_abstracthuman (id,user_id,slug,profile_pic) VALUES(ct, 2, name, null)" );
  33. cur.execute( "INSERT INTO personal_importantlinks (abstracthuman_ptr_id,title,description,related_language,link) VALUES(ct, name, name,null,link)" );
  34. conn.commit()
  35.  
  36. input_filename = "/home/%s/.config/google-chrome/Default/Bookmarks" % user
  37. #input_filename = "/Users/%s/Library/Application Support/Google/Chrome/Default/Bookmarks" % user
  38. # modify if necessary
  39. output_filename = "/home/%s/Documents/chrome-bookmarks.html" % user
  40.  
  41. input_file = codecs.open(input_filename, encoding='utf-8')
  42. bookmark_data = json.load(input_file)
  43. output_file = codecs.open(output_filename, 'w', encoding='utf-8')
  44.  
  45. def print_bookmarks(bookmarks):
  46. for entry in bookmarks:
  47. if entry['type'] == 'folder':
  48. if not len(entry['children']) == 0:
  49. output_file.write(u'<DT><H3 FOLDED ADD_DATE="{0}">{1}</H3>'.format(entry['date_added'], entry['name']))
  50. next_folder = entry['children']
  51. output_file.write(u'<DL><p>')
  52. print_bookmarks(next_folder)
  53. output_file.write(u'</DL><p>')
  54. else:
  55. output_file.write(u'<DT><A DATE_ADDED="{0}" HREF="{1}">{2}</A>'.format(entry['date_added'], cgi.escape(entry['url']), entry['name']))
  56.  
  57.  
  58. output_file.write(u'<!DOCTYPE NETSCAPE-Bookmark-file-1>\n<Title>Bookmarks</Title>\n<H1>Bookmarks</H1><DL>\n')
  59.  
  60. roots = bookmark_data['roots']
  61.  
  62. #print roots
  63. s = 1
  64. for entry in roots:
  65. try:
  66. print s
  67. #print len(roots[entry]['children'])
  68. ct = 283
  69. #ids = 4
  70. for i in roots[entry]['children']:
  71. #print int(i['date_added'])
  72. s, ms = divmod(int(i['date_added']), 1000)
  73. date_created = '%s.%03d' % (time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
  74. print i['name']
  75. print i['url']
  76. #print i['url']
  77. output_file.write(u'<DT><H3 FOLDED><a href="{1}" >{0} on date <strong style="color:red;">{2}</strong></a></H3>'.format(i['name'], i['url'], date_created))
  78. output_file.write(u'</DL><p>')
  79. #myConnection = psycopg2.connect( host=hostname, user=username, password=password, dbname=database )
  80. #doQuery( myConnection, i['name'], i['url'], ct, ids )
  81. #myConnection.close()
  82. ct = ct+1
  83. #ids = ids+1
  84.  
  85. s = s+1
  86. #print roots[entry]['date_added']
  87. #output_file.write(u'<DT><H3 FOLDED ADD_DATE={0}>{1}</H3>'.format(roots[entry]['date_added'], entry))
  88. #print_bookmarks(roots[entry]['children'])
  89. #output_file.write(u'</DL><p>')
  90. except:
  91. pass
  92.  
  93. output_file.write(u'</DL>')

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.