-
Notifications
You must be signed in to change notification settings - Fork 0
/
uowmdbcollection.py
73 lines (63 loc) · 2.14 KB
/
uowmdbcollection.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rethinkdb as r
import random
from uowmlib import WPConfiguration, WPLog, is_image
import mimetypes
import json
#TODO:
# Multiple tags and
# Multiple tags or
# Display new ones( without a last display timestamp)
class WPDBColletion(object):
def __init__(self, tags, wpconf, tag_match="AND"):
def match_or(wp):
for tag in wp['tags']:
if r.expr(tags).contains(tag):
return True
return False
#TODO: Reimplement cycle_dirs option as cycle_tags
self.log = WPLog(wpconf)
self.conf = wpconf
self.file_list = []
_conn = r.connect(db='uowm')
t = r.table('wallpapers')
f = r.row['tags']
if tag_match == 'OR':
cur = []
for tag in tags:
cur += [x for x in t.filter(f.contains(tag)).run(_conn)]
else: #AND
cur = t.filter(f.contains(*tags)).run(_conn)
for wallpaper in cur:
fullpath = wallpaper['fullpath']
if is_image(fullpath):
self.file_list.append(fullpath)
def draw(self):
chosen = None
while chosen is None:
print len(self.file_list)
candidate = random.choice(self.file_list)
if len(self.file_list) > self.conf.no_repeat:
if not self.log.in_last(self.conf.no_repeat, candidate):
chosen = candidate
else:
print "This collection has less items than the no_repeat \
parameter. We cannot guarantee this."
chosen = candidate
self.log.add(chosen)
return chosen
def simple_draw():
_conn = r.connect(db='uowm')
cur = r.table('wallpapers').\
filter(r.row['tags'].contains("earthporn")).run(_conn)
collection = []
for cu in cur:
collection.append(cu)
print random.choice(collection)
if __name__ == '__main__':
conf = WPConfiguration()
a = WPDBColletion(['earthporn', 'comics'], conf, tag_match="OR")
b = WPDBColletion(['earthporn'], conf, tag_match="AND")
print a.draw()
print b.draw()