Created
February 21, 2012 12:06
-
-
Save hamstah/1876145 to your computer and use it in GitHub Desktop.
Generate links for mendeley
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pysqlite2 import dbapi2 as sqlite | |
import sys | |
import os | |
import re | |
def print_headers(handle): | |
handle.write("""<html>\n\t<head>\n\t\t<title>Mendelinks test</title>\n\t\t<style>body{ background-color: #969ba3; font-family: arial; color: #333;} | |
div { background-color: #fff; border: 1px solid #747981; padding: 5px; margin: 5px; } | |
h2, h4 { margin: 4px }\n\t\t</style>\n\t</head>\n\t<body>""") | |
def print_footer(handle): | |
handle.write("\n\t</body>\n</html>\n") | |
def remove_braces(uuid): | |
return re.sub(r"[{}]","",uuid) | |
def print_documents(handle,cursor): | |
handle.write("\n\t\t<div><h2>Documents</h2>") | |
# uuid | |
handle.write("\n\t\t<h4>Links to documents by Uuid</h4>\n\t\t<ul>") | |
cursor.execute("select uuid,title from documents limit 0,10") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/document/%s">%s</a></li>"""%(remove_braces(res[0]), res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# uuid + /pdf | |
handle.write("\n\t\t<h4>Links to documents by Uuid, open first pdf</h4>\n\t\t<ul>") | |
cursor.execute("select uuid,title from documents limit 0,10") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/document/%s/pdf">%s</a></li>"""%(remove_braces(res[0]), res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# uuid + hash | |
handle.write("\n\t\t<h4>Links to documents by Uuid, opening a file</h4>\n\t\t<ul>") | |
cursor.execute("select uuid, title, hash from documents d, documentfiles df where df.documentId = d.id limit 0,10") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/document/%s/files/%s">%s</a></li>"""%(remove_braces(res[0]), res[2],res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# remoteid | |
handle.write("\n\t\t<h4>Links to documents by WebId</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid,title from remotedocuments rf, documents d where rf.documentid = d.id limit 0,10") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/document/%s">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# remoteid | |
handle.write("\n\t\t<h4>Links to documents by WebId, open first pdf</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid,title from remotedocuments rf, documents d where rf.documentid = d.id limit 0,10") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/document/%s/pdf">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# remoteid + hash | |
handle.write("\n\t\t<h4>Links to documents by WebId, opening a file</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid, title, hash from documents d, documentfiles df, remotedocuments rf where df.documentId = d.id and df.documentId = rf.documentId limit 0,10") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/document/%s/files/%s">%s</a></li>"""%(res[0], res[2],res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t</div>") | |
def print_folders(handle,cursor): | |
handle.write("\n\t\t<div><h2>Folders</h2>") | |
# uuid | |
handle.write("\n\t\t<h4>Links to folders by Uuid (top level)</h4>\n\t\t<ul>") | |
cursor.execute("select uuid, name from folders where parentId=-1 limit 0,5") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/folder/%s">%s</a></li>"""%(remove_braces(res[0]), res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t<h4>Links to folders by Uuid (sub folder)</h4>\n\t\t<ul>") | |
cursor.execute("select uuid, name from folders where parentId<>-1 limit 0,5") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/folder/%s">%s</a></li>"""%(remove_braces(res[0]), res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# remoteid | |
handle.write("\n\t\t<h4>Links to folders by WebId (top level)</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid, name from folders f, remotefolders rf where parentId=-1 and f.id = rf.folderid limit 0,5") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/folder/%s">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t<h4>Links to folders by WebId (sub folder)</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid, name from folders f, remotefolders rf where parentId<>-1 and f.id = rf.folderid limit 0,5") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://library/folder/%s">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t</div>") | |
def print_groups(handle,cursor): | |
handle.write("\n\t\t<div><h2>Groups</h2>") | |
# remoteid | |
handle.write("\n\t\t<h4>Links to Groups by WebId</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid, name from groups where remoteid<>0 limit 0,3") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://group/%s">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# remoteid overview | |
handle.write("\n\t\t<h4>Links to Groups by WebId (Overview tab)</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid, name from groups where remoteid<>0 limit 0,3") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://group/%s/overview">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# remoteid documents | |
handle.write("\n\t\t<h4>Links to Groups by WebId (Documents tab)</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid, name from groups where remoteid<>0 limit 0,3") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://group/%s/documents">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
# remoteid members | |
handle.write("\n\t\t<h4>Links to Groups by WebId (Members tab)</h4>\n\t\t<ul>") | |
cursor.execute("select remoteid, name from groups where remoteid<>0 limit 0,3") | |
for res in cursor: | |
handle.write(("""\n\t\t\t<li><a href="mendeley://group/%s/members">%s</a></li>"""%(res[0], res[1])).encode('utf-8')) | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t</div>") | |
def print_filters(handle,cursor): | |
handle.write("\n\t\t<div><h2>Filters</h2>") | |
handle.write("\n\t\t<h4>Filters</h4>\n\t\t<ul>") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/all-documents">All Documents</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/recently-added">Recently added</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/my-publications">My publications</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/quickstart">Quickstart</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/mendeley-suggest">Mendeley Suggest</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/favorites">Favorites</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/favorites">Favourites</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://library/filter/unsorted">Unsorted</a>""") | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t</div>") | |
def print_settings(handle,cursor): | |
handle.write("\n\t\t<div><h2>Settings</h2>\n\t\t<ul>") | |
handle.write("""\n\t\t\t<li><a href="mendeley://settings/general">General</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://settings/account-details">Account details</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://settings/proxy-settings">Proxy settings</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://settings/folder-monitoring">Folder monitoring</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://settings/metadata-fields">Metadata fields</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://settings/zotero-settings">Zotero settings</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://settings/bibtex-settings">Bibtex settings</a>""") | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t</div>") | |
def print_import(handle,cursor): | |
handle.write("\n\t\t<div><h2>Import</h2>\n\t\t<ul>") | |
handle.write("""\n\t\t\t<li><a href="mendeley://import/endnote-xml">Endnote</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://import/pdf">PDF</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://import/any">Any format</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://import/bibtex">Bibtex</a>""") | |
handle.write("""\n\t\t\t<li><a href="mendeley://import/ris">RIS</a>""") | |
handle.write("\n\t\t</ul>") | |
handle.write("\n\t\t</div>") | |
if len(sys.argv) != 2: | |
print "usage: %s database.sqlite"%(sys.argv[0]) | |
exit(1) | |
path = sys.argv[1] | |
try: | |
# will trigger an exception if the file does not exist | |
open(path) | |
handle = open("mendelinks.html","w") | |
connection = sqlite.connect(path) | |
cursor = connection.cursor() | |
print "Generating mendelinks.html" | |
print_headers(handle) | |
print_settings(handle,cursor) | |
print_import(handle,cursor) | |
print_filters(handle,cursor) | |
print_documents(handle,cursor) | |
print_folders(handle,cursor) | |
print_groups(handle,cursor) | |
print_footer(handle) | |
print "done" | |
handle.close() | |
connection.close() | |
except IOError: | |
print "** Error: Invalid database file %s"%(path) | |
exit(2) | |
except sqlite.OperationalError as e: | |
print "** Error: Database format is invalid" | |
print e | |
exit(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment