-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebuild_cache.py
159 lines (140 loc) · 5.04 KB
/
rebuild_cache.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
"""
Script for generating/updating the directory listing
of the mongo database.
Depending on the size of your gallery listing, this might
take a few minutes to set up the first time.
"""
import os
import glob
import datetime
import yaml
import pymongo
from pymongo import MongoClient
from src.gallery import Gallery
from src import settings
def build_cache(database, now):
"""
Rebuilds the mongo cache of ftimg
"""
tree = dict()
# look for all our valid gallery files
files = list(glob.iglob(os.path.join(settings.ROOT_DIRECTORY, '**', '*.jpg')))
files.extend(list(glob.iglob(os.path.join(settings.ROOT_DIRECTORY, '**', '*.png'))))
files.extend(list(glob.iglob(os.path.join(settings.ROOT_DIRECTORY, '**', 'gallery.yml'))))
for filename in files:
mod_date = os.path.getmtime(filename)
path = filename[len(settings.ROOT_DIRECTORY):]
gallery = os.path.dirname(filename)[len(settings.ROOT_DIRECTORY):]
# establish or update gallery metadata
if filename.endswith('gallery.yml'):
result = database.galleries.find_one_and_update({
'_id': gallery
}, {
'$set': {'lastChecked': now}
})
if result is None or \
(result and result.get('modified', None) is None) or \
(result and result['modified'] < mod_date):
with open(filename, 'r') as gyml:
meta = yaml.load(gyml)['gallery']
database.galleries.update_one(
{
'_id': gallery,
},
{
'$setOnInsert': {
'_id': gallery,
'files': [],
'lastChecked': now,
},
'$set': {
'modified': mod_date,
**meta
}
},
upsert=True
)
# update or add pages into our database
else:
result = database.pages.find_one_and_update({
'_id': path
}, {
'$set': {'lastChecked': now}
})
if result is None or \
(result and result.get('modified', None) is None) or \
(result and result['modified'] < mod_date):
result = database.pages.update_one(
{
'_id': path
},
{
'$setOnInsert': {
'_id': path,
'gallery': gallery,
'lastChecked': now,
},
'$set': {
'modified': mod_date,
'thumbnail': Gallery.generate_thumbnail(filename)
},
},
upsert=True
)
# update the gallery this page belongs to to be aware of the file
if result.upserted_id:
database.galleries.update_one(
{
'_id': gallery,
},
{
'$setOnInsert': {
'modified': None,
'title': os.path.basename(os.path.dirname(filename)),
}
},
upsert=True
)
# for all galleries, generate a thumbnail
for gallery in database.galleries.find():
thumb = gallery.get('thumbnail', "")
if thumb.endswith("jpg") or thumb.endswith("png"):
path = os.path.join(settings.ROOT_DIRECTORY, gallery['_id'], thumb)
thumb = Gallery.generate_thumbnail(path)
else:
thumb = database.pages.find(
{
'gallery': gallery['_id'],
}
).sort("_id", pymongo.ASCENDING)[0]['thumbnail']
database.galleries.update_one(
{
'_id': gallery['_id'],
},
{
'$set': {
'thumbnail': thumb
}
}
)
def clean_cache(database, now):
"""
Remove files from gallery cache if they no longer exist.
This is determined by if the file was checked when rebuilding the cache
@param now: time at which the cache was updated.
"""
database.pages.delete_many({
'lastChecked': {'$lt': now}
})
def run():
"""
Execute the process of rebuilding and cleaning the mongo cache
"""
# current time of checking
now = datetime.datetime.now()
client = MongoClient(settings.MONGO_HOST)
database = client.ftimg
build_cache(database, now)
clean_cache(database, now)
if __name__ == '__main__':
run()