1
|
|
|
""" |
2
|
|
|
SQLite plugin object. |
3
|
|
|
This plugin stores metadata about all media in an sqlite database. |
4
|
|
|
|
5
|
|
|
You'll need to create a SQLite database using the schema.sql file and |
6
|
|
|
reference it in your configuration. |
7
|
|
|
|
8
|
|
|
``` |
9
|
|
|
[PluginSQLite] |
10
|
|
|
database_file=/path/to/database.db |
11
|
|
|
|
12
|
|
|
.. moduleauthor:: Jaisen Mathai <[email protected]> |
13
|
|
|
""" |
14
|
|
|
from __future__ import print_function |
15
|
|
|
|
16
|
|
|
import json |
17
|
|
|
import os |
18
|
|
|
import sqlite3 |
19
|
|
|
import time |
20
|
|
|
#import json |
21
|
|
|
|
22
|
|
|
#from google_auth_oauthlib.flow import InstalledAppFlow |
23
|
|
|
#from google.auth.transport.requests import AuthorizedSession |
24
|
|
|
#from google.oauth2.credentials import Credentials |
25
|
|
|
|
26
|
|
|
from elodie.media.photo import Photo |
27
|
|
|
from elodie.media.video import Video |
28
|
|
|
from elodie.plugins.plugins import PluginBase |
29
|
|
|
|
30
|
|
|
class SQLite(PluginBase): |
31
|
|
|
"""A class to execute plugin actions. |
32
|
|
|
|
33
|
|
|
Requires a config file with the following configurations set. |
34
|
|
|
database_file: |
35
|
|
|
The full path to the SQLite database (.db). |
36
|
|
|
|
37
|
|
|
""" |
38
|
|
|
|
39
|
|
|
__name__ = 'SQLite' |
40
|
|
|
|
41
|
|
|
def __init__(self): |
42
|
|
|
super(SQLite, self).__init__() |
43
|
|
|
|
44
|
|
|
self.database_schema = '{}{}{}'.format(os.path.dirname(os.path.realpath(__file__)), os.sep, 'schema.sql') |
45
|
|
|
self.database_file = None |
46
|
|
|
if('database_file' in self.config_for_plugin): |
47
|
|
|
self.database_file = self.config_for_plugin['database_file'] |
48
|
|
|
|
49
|
|
|
self.con = sqlite3.connect(self.database_file) |
50
|
|
|
self.con.row_factory = sqlite3.Row |
51
|
|
|
self.cursor = self.con.cursor() |
52
|
|
|
|
53
|
|
|
def after(self, file_path, destination_folder, final_file_path, metadata): |
54
|
|
|
|
55
|
|
|
# We check if the source path exists in the database already. |
56
|
|
|
# If it does then we assume that this is an update operation. |
57
|
|
|
full_destination_path = '{}{}'.format(destination_folder, final_file_path) |
58
|
|
|
self.cursor.execute("SELECT `path` FROM `metadata` WHERE `path`=:path", {'path': file_path}) |
59
|
|
|
if(self.cursor.fetchone() is None): |
60
|
|
|
self.log(u'SQLite plugin inserting {}'.format(file_path)) |
61
|
|
|
sql_statement, sql_values = self._insert_row_sql(full_destination_path, metadata) |
62
|
|
|
else: |
63
|
|
|
self.log(u'SQLite plugin updating {}'.format(file_path)) |
64
|
|
|
sql_statement, sql_values = self._update_row_sql(file_path, full_destination_path, metadata) |
65
|
|
|
|
66
|
|
|
self.cursor.execute(sql_statement, sql_values) |
67
|
|
|
|
68
|
|
|
def batch(self): |
69
|
|
|
pass |
70
|
|
|
|
71
|
|
|
def before(self, file_path, destination_folder): |
72
|
|
|
pass |
73
|
|
|
|
74
|
|
|
def generate_db(self, hash_db): |
75
|
|
|
pass |
76
|
|
|
|
77
|
|
|
"""def create_schema(self): |
78
|
|
|
with open(self.database_schema, 'r') as fp_schema: |
79
|
|
|
sql_statement = fp_schema.read() |
80
|
|
|
|
81
|
|
|
with self.con: |
82
|
|
|
self.cursor.executescript(sql_statement) |
83
|
|
|
""" |
84
|
|
|
|
85
|
|
|
def run_query(self, sql, values): |
86
|
|
|
self.cursor.execute(sql, values) |
87
|
|
|
return self.cursor.fetchall() |
88
|
|
|
|
89
|
|
|
def _insert_row_sql(self, final_path, metadata): |
90
|
|
|
timestamp = int(time.time()) |
91
|
|
|
return ( |
92
|
|
|
"INSERT INTO `metadata` (`path`, `metadata`, `created`, `modified`) VALUES(:path, :metadata, :created, :modified)", |
93
|
|
|
{'path': final_path, 'metadata': json.dumps(metadata), 'created': timestamp, 'modified': timestamp} |
94
|
|
|
) |
95
|
|
|
|
96
|
|
|
def _update_row_sql(self, current_path, final_path, metadata): |
97
|
|
|
timestamp = int(time.time()) |
98
|
|
|
return ( |
99
|
|
|
"UPDATE `metadata` SET `path`=:path, `metadata`=json(:metadata), `modified`=:modified WHERE `path`=:currentPath", |
100
|
|
|
{'currentPath': current_path, 'path': final_path, 'metadata': json.dumps(metadata), 'modified': timestamp} |
101
|
|
|
) |
102
|
|
|
|