Skip to content
Snippets Groups Projects
Commit 6b1b2be0 authored by Mathieu Loiseau's avatar Mathieu Loiseau
Browse files

marche presque

parent 8d96145d
No related branches found
No related tags found
No related merge requests found
...@@ -6,11 +6,12 @@ import os, sys, os.path ...@@ -6,11 +6,12 @@ import os, sys, os.path
import getpass import getpass
import sqlite3 import sqlite3
import json import json
import bisect
import re #regular expression import re #regular expression
parser = argparse.ArgumentParser(description="Extraire les notes d'une liseuse Vivlio") parser = argparse.ArgumentParser(description="Extraire les notes d'une liseuse Vivlio")
parser.add_argument("-s", "--source", help="le dossier de la liseuse", type=str, default=None) parser.add_argument("-s", "--source", help="le dossier de la liseuse", type=str, default=None)
parser.add_argument("-f","--format", help="le format d'export (JSON ou wiki)", type=str, default=None) parser.add_argument("-f","--format", help="le format d'export (JSON ou wiki)", type=str, default="json")
parser.add_argument("-d", "--destination", default=".", help="le chemin et nom du dossier d'export (un fichier par livre)", type=str) parser.add_argument("-d", "--destination", default=".", help="le chemin et nom du dossier d'export (un fichier par livre)", type=str)
args = parser.parse_args() args = parser.parse_args()
...@@ -21,25 +22,26 @@ def getPage(anchor): ...@@ -21,25 +22,26 @@ def getPage(anchor):
output = output.group(1) output = output.group(1)
return int(output) return int(output)
class Annotation: class Note:
BM = "bookmark" #bookmark BM = 1 #bookmark
QUOTE = "citation" #citation QUOTE = 2 #citation
NOTE = "note" #note NOTE = 3 #note
def __init__(self, dbString): def __init__(self, dbString):
tmp = json.loads(dbString) tmp = json.loads(dbString)
self.text = None self.text = None
self.endP = None self.endP = None
self.note = None
if len(tmp)==3: if len(tmp)==3:
self.type = Annotation.NOTE self.type = Note.NOTE
tmp[0].update(tmp[1]) tmp[0].update(tmp[1])
tmp[0]["note"] = tmp[2]["text"].replace("\n","") tmp[0]["note"] = tmp[2]["text"].replace("\n","")
self.note = tmp[0]["note"] self.note = tmp[0]["note"]
elif len(tmp[1]) == 1: elif len(tmp[1]) == 1:
self.type = Annotation.BM self.type = Note.BM
else: else:
tmp[0].update(tmp[1]) tmp[0].update(tmp[1])
self.type = Annotation.QUOTE self.type = Note.QUOTE
#TODO mistakes Crayon for a quote, quoting "crayon" #TODO mistakes Crayon for a quote, quoting "crayon"
self.raw = tmp[0] self.raw = tmp[0]
self.p = getPage(tmp[0]["anchor"]) self.p = getPage(tmp[0]["anchor"])
...@@ -50,14 +52,109 @@ class Annotation: ...@@ -50,14 +52,109 @@ class Annotation:
if "text" in tmp[0] : if "text" in tmp[0] :
self.text = tmp[0]["text"].replace("\n","") self.text = tmp[0]["text"].replace("\n","")
def __lt__(self, note2):
if self.p > note2.p:
res = -1
elif self.p < note2.p:
res = 1
elif self.endP == note2.endP:
if self.type > note2.type:
res = -1
elif self.type < note2.type:
res = 1
else:
res = 0
elif self.endP == None:
res = 1
elif note2.endP == None:
res = -1
elif self.endP > note2.endP:
res = -1
else:
res = 1
return res
def toJSON(self): def toJSON(self):
json_note = {"raw":self.raw,"type":self.type,"p":self.p} json_note = {"type":self.type,"p":self.p}
if self.text != None :
json_note["text"] = self.text
if self.endP != None : if self.endP != None :
json_note["endP"] = self.endP json_note["endP"] = self.endP
if self.text != None :
json_note["text"] = self.text
json_note["raw"] = self.raw
return json_note return json_note
def toWiki(self):
wiki_note = ""
if self.type != Note.BM:
wiki_note = "{{p|"+str(self.p)
if self.endP != None and self.p!=self.endP:
wiki_note += ""+str(self.endP)
wiki_note+="}}\n"
if self.text != None:
wiki_note += "«"+self.text+"»\n"
if self.note != None:
wiki_note += "{{commentaire|"+self.note+"}}\n"
return wiki_note
class Book:
def __init__(self, author=None, title=None):
self.author = author
self.title = title
self.data = []
def equals(self, author, title):
return self.author == author and self.title == title
def is_empty(self):
return len(self.data)==0
def empty(self):
self.data = []
def addNote(self, note):
bisect.insort_left(self.data,note)
def fileName(self, directory="."):
res = False
if self.author == None:
if self.title == None:
raise ValueError("Can't export title and author-less book")
else:
res = self.title
elif self.title == None:
res = self.author
else:
res = self.author+"_"+self.title
return directory+"/"+res
def store(self, directory=".", format="json"):
if format == "json":
self.JSON_store(self.fileName(directory)+".json")
elif format == "wiki":
self.wiki_store(self.fileName(directory)+".wiki")
else:
raise ValueError("Unexpected format: "+str(format))
def JSON_store(self, fileName):
content = {"author":self.author, "title":self.title, "data":[]}
for note in self.data:
content["data"].append(note.toJSON())
out_file = open(fileName, "w")
json.dump(content, out_file, indent = 2, ensure_ascii=False)
out_file.close()
def wiki_store(self, fileName):
out_file = open(fileName, "w")
out_file.write("="+self.author+""+self.title+"=\n")
for note in self.data:
out_file.write(note.toWiki())
out_file.write("\n")
out_file.close()
#####
# Main functions
#####
def getSource(): def getSource():
print("Warning, this only works for linux systems") print("Warning, this only works for linux systems")
basepath = "/media/"+getpass.getuser() basepath = "/media/"+getpass.getuser()
...@@ -81,7 +178,7 @@ def connect(sourceDir): ...@@ -81,7 +178,7 @@ def connect(sourceDir):
path = sourceDir+'/system/config/books.db' path = sourceDir+'/system/config/books.db'
conn = sqlite3.connect("file://"+path+'?mode=ro', uri=True) conn = sqlite3.connect("file://"+path+'?mode=ro', uri=True)
print("Connected to '"+path+"'.") print("Connected to '"+path+"'.")
except (ValueError,sqlite3.Error) as e: except (sqlite3.Error) as e:
print (str(e)) print (str(e))
conn = None conn = None
return conn return conn
...@@ -93,25 +190,17 @@ def retrieveData(conn): ...@@ -93,25 +190,17 @@ def retrieveData(conn):
return cur.fetchall() return cur.fetchall()
#while conn.fetchone() #while conn.fetchone()
def dataToJSON(data, directory): def export_data(data, directory, format="json"):
curfile = None curbook = Book()
for comment in data: for comment in data:
fileName = directory+"/"+comment[1]+"_"+comment[0]+".json" if not curbook.equals(comment[1], comment[0]):
if curfile != fileName: if not curbook.is_empty():
if curfile != None: curbook.store(directory, format)
JSON_store(curdata, curfile) curbook.empty()
curfile = fileName curbook = Book(comment[1],comment[0])
curdata = {"title":comment[0],"author":comment[1],"data":[]} curbook.addNote(Note(comment[2]))
jj = Annotation(comment[2]) if curbook.is_empty():
curdata["data"].append(jj.toJSON()) curbook.store(directory, format)
fileName = None
if curfile != None:
JSON_store(curdata, curfile)
def JSON_store(data, filename):
out_file = open(filename, "w")
json.dump(data, out_file, indent = 2, ensure_ascii=False)
out_file.close()
#main #main
if __name__ == "__main__": if __name__ == "__main__":
...@@ -125,4 +214,7 @@ if __name__ == "__main__": ...@@ -125,4 +214,7 @@ if __name__ == "__main__":
data = retrieveData(conn) data = retrieveData(conn)
conn.close() conn.close()
if data != None: if data != None:
dataToJSON(data, args.destination) try:
export_data(data, args.destination, args.format)
except ValueError as e:
print(e)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment