-
Notifications
You must be signed in to change notification settings - Fork 1
/
shotwell2blog.py
232 lines (193 loc) · 6.69 KB
/
shotwell2blog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!python
# Copyright 2014-2017 by Luc Saffre.
# License: BSD, see LICENSE for more details.
"""
Thanks to Maarek for first inspiration
http://forum.ubuntu-fr.org/viewtopic.php?id=415396
Change history:
- 2015-09-19 accept non-ascii tags
- 2015-12-04 new command-line options `before` and `after`
"""
from __future__ import print_function
from __future__ import unicode_literals
import os
from os.path import expanduser
import time
from time import mktime
from datetime import datetime
from dateutil import parser
import shutil
import codecs
import sqlite3
from argh import dispatch_command, arg, CommandError
"""
sqlite_master : ['type', 'name', 'tbl_name', 'rootpage', 'sql']
- TagTable:
- name : name of the tag (e.g. "blog")
- photo_id_list : comma-separated list of photo ids
CREATE TABLE PhotoTable (
id INTEGER PRIMARY KEY,
filename TEXT UNIQUE NOT NULL,
width INTEGER,
height INTEGER,
filesize INTEGER,
timestamp INTEGER,
exposure_time INTEGER,
orientation INTEGER,
original_orientation INTEGER,
import_id INTEGER,
event_id INTEGER,
transformations TEXT,
md5 TEXT,
thumbnail_md5 TEXT,
exif_md5 TEXT,
time_created INTEGER,
flags INTEGER DEFAULT 0,
rating INTEGER DEFAULT 0,
file_format INTEGER DEFAULT 0,
title TEXT,
backlinks TEXT,
time_reimported INTEGER,
editable_id INTEGER DEFAULT -1,
metadata_dirty INTEGER DEFAULT 0,
developer TEXT,
develop_shotwell_id INTEGER DEFAULT -1,
develop_camera_id INTEGER DEFAULT -1,
develop_embedded_id INTEGER DEFAULT -1,
comment TEXT)
CREATE TABLE VideoTable (id INTEGER PRIMARY KEY, filename TEXT UNIQUE
NOT NULL, width INTEGER, height INTEGER, clip_duration REAL,
is_interpretable INTEGER, filesize INTEGER, timestamp INTEGER,
exposure_time INTEGER, import_id INTEGER, event_id INTEGER, md5 TEXT,
time_created INTEGER, rating INTEGER DEFAULT 0, title TEXT, backlinks
TEXT, time_reimported INTEGER, flags INTEGER DEFAULT 0 , comment TEXT)
"""
def chop(s, prefix):
if s.startswith(prefix):
return s[len(prefix):]
return s
def schema(conn, tableName):
cursor = conn.cursor()
sql = "SELECT sql FROM sqlite_master WHERE type='table' AND name='%s'"
sql = sql % tableName
print(sql)
cursor.execute(sql)
row = cursor.fetchone()
return row['sql']
def show_tables(conn):
cursor = conn.cursor()
sql = "SELECT name FROM sqlite_master WHERE type='table'"
print(sql)
cursor.execute(sql)
return [row['name'] for row in cursor.fetchall()]
def get_photos(conn, tags):
"""Yield a tuple (filename, """
encoding = "utf-8"
cursor = conn.cursor()
li = ','.join(["'{0}'".format(t.decode(encoding)) for t in tags])
where = "name in ({0})".format(li)
sql = "SELECT photo_id_list FROM TagTable WHERE {0}".format(where)
# sql = "SELECT photo_id_list FROM TagTable WHERE name = '%s'" % tagname
photo_ids = set()
video_ids = set()
cursor.execute(sql)
for row in cursor:
for photo_id in row[str("photo_id_list")].split(","):
if photo_id:
if photo_id.startswith('thumb'):
photo_id = int(photo_id[5:], 16)
photo_ids.add(photo_id)
elif photo_id.startswith('video-'):
photo_id = int(photo_id[6:], 16)
video_ids.add(photo_id)
else:
raise Exception("Invalid photo_id %r" % photo_id)
if len(photo_ids):
li = ','.join([str(i) for i in photo_ids])
sql = "SELECT id, filename, time_created " \
"FROM PhotoTable WHERE id IN ({0})"
sql = sql.format(li)
sql += " ORDER BY exposure_time"
cursor.execute(sql)
for row in cursor:
yield row[1], row[2]
RSTLINE = """.. sigal_image:: {0}\n"""
@dispatch_command
@arg('tags', help="One ore more Shotwell tags. "
"The listed pictures must have *at least* one of them.")
@arg('-t', '--target_root',
help='Where to export tagged pictures and videos.')
@arg('-l', '--shotwell_lib',
help='Your Shotwell library directory')
@arg('-s', '--sigal_image',
help='Output as sigal_image directives')
@arg('-b', '--before',
help='Select photos taken before that time')
@arg('-a', '--after',
help='Select photos taken after that time')
@arg('-d', '--shotwell_db',
help='Your Shotwell database file')
def main(target_root=None,
shotwell_lib=expanduser("~/Pictures/"),
shotwell_db=expanduser("~/.local/share/shotwell/data/photo.db"),
sigal_image=False,
before=None, after=None,
*tags):
"""Export tagged photos and videos from a Shotwell photo database to a
target filesystem or list them in different ways.
See <https://github.com/lsaffre/shotwell2blog>.
"""
if target_root:
if not os.path.exists(target_root):
raise CommandError(
"Target directory %s does not exist!" % target_root)
conn = sqlite3.connect(shotwell_db)
conn.row_factory = sqlite3.Row
if False: # I used this to discover the database structure.
yield show_tables(conn)
yield schema(conn, 'TagTable')
yield schema(conn, 'PhotoTable')
yield schema(conn, 'VideoTable')
return
if len(tags) == 0:
raise CommandError("Must specify at least one tag!")
tags = [' '.join(tags)]
# multiple tags are not supported.
if before is not None:
before = parser.parse(before)
if after is not None:
after = parser.parse(after)
targets = []
for orig, time_created in get_photos(conn, tags):
target = chop(orig, shotwell_lib)
target = target.lower()
target = target.replace(" ", "_")
targets.append(target)
if target_root:
target = os.path.join(target_root, target)
if os.path.exists(target):
yield "{0} exists".format(target)
else:
dn = os.path.dirname(target)
if not os.path.exists(dn):
yield "{0} created".format(dn)
os.makedirs(dn)
yield "{0} copied".format(target)
shutil.copyfile(orig, target)
else:
if sigal_image:
yield RSTLINE.format(target)
else:
t = time.localtime(time_created)
t = datetime.fromtimestamp(mktime(t))
if before is None or t <= before:
if after is None or t >= after:
# yield "{0}|{1}".format(t, orig)
yield orig
conn.close()
if target_root:
fn = os.path.join(target_root, 'index.rst')
rstfile = codecs.open(fn, "w", encoding="utf-8")
for t in targets:
rstfile.write(RSTLINE.format(t))
rstfile.close()