-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_main.py
More file actions
127 lines (91 loc) · 3.86 KB
/
Copy pathserver_main.py
File metadata and controls
127 lines (91 loc) · 3.86 KB
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
import tornado.ioloop
import tornado.web
import json
import os
import time
import datetime
import glob
import shutil
import subprocess
import random
from tornado import gen
import multiprocessing
from sh import gphoto2 as gp
PATH = os.path.join(os.path.dirname(__file__), "static")
settings = {'debug': True,
'static_path': PATH}
class MainHandler(tornado.web.RequestHandler):
def get(self, *args, **kwargs):
print settings
self.render("main.html")
class SnapHandler(tornado.web.RequestHandler):
### Receives the hash and the JSON object of the search
### checks the DB is the hash is in the column, if yes return cached data response
### else make the DB query and caches the response before sending back to frontend
def get(self):
searchstring = self.get_argument("search")
print searchstring
print "SNAP"
#### just a call to take a snap
triggerCommand = ["--trigger-capture"]
gp(triggerCommand)
self.write("snap:"+searchstring)
self.finish()
class CleanUpHandler(tornado.web.RequestHandler):
def get(self):
print "console hit"
# time.sleep(10)
### copy down the files off the camera
getFilesCommand = ["--get-all-files"]
gp(getFilesCommand)
### make a datetime dir on the flash drive
newSessionDirName = os.path.join(r'/media/pi/HP v125w/kikanina', datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S') )
os.makedirs(newSessionDirName)
newSessionPlayDirName = os.path.join(newSessionDirName, 'play')
os.makedirs(newSessionPlayDirName)
### copy the files to the flash subdir
cwd = r'/media/pi/HP v125w/kikanina'
currentDir = os.getcwd()
for snap in glob.iglob(os.path.join(currentDir, "*.JPG")):
os.remove(snap)
downloadCommand = ["--get-all-files"]
# status = subprocess.Popen("gphoto2 --get-all-files", cwd=newSessionDirName,shell=True)
time.sleep(2)
gp(downloadCommand)
### copy them to the right Dir
for snap in glob.iglob(os.path.join(currentDir, "*.JPG")):
shutil.copy(snap, newSessionDirName)
shutil.copy(snap, newSessionPlayDirName)
### delete the photos off the camera
clearphotos = subprocess.check_output(r"gphoto2 --folder='/store_00020001/DCIM/100CANON' -R --delete-all-files", shell=True)
# # clearCommand = ["--folder=","/store_00020001/DCIM/100CANON" "-R", "--delete-all-files"]
# # gp(clearCommand)
# ### return done (Possibly a gif?)
mogrify = subprocess.check_output('mogrify -resize 1280x960 *.JPG', cwd=newSessionPlayDirName, shell=True)
gifme = subprocess.check_output('convert -delay 110 -loop 0 *.JPG mygif.gif', cwd=newSessionPlayDirName, shell=True)
### process gif on the side
# def target(where):
# mogrify = subprocess.check_output('mogrify -resize 1280x960 *.JPG', cwd=where, shell=True)
# gifme = subprocess.check_output('convert -delay 110 -loop 0 *.JPG mygif.gif', cwd= where, shell=True)
#
# mygiffile = os.path.join(newSessionDirName,'mygif.txt')
# p = multiprocessing.Process(target=target, args=(newSessionPlayDirName,))
# p.start()
# p.join()
# giflink = os.path.join('thegif','mygif.gif')
#
# self.write(json.dumps(giflink))
self.write(json.dumps("cleaned up"))
self.finish()
def make_app():
return tornado.web.Application([
(r"/", MainHandler),
(r"/snap", SnapHandler),
(r"/cleanup", CleanUpHandler)],
**settings)
if __name__ == "__main__":
### define and run a function that kills the gphoto process IF it exists
app = make_app()
port = int(os.environ.get("PORT", 4000))
app.listen(port)
tornado.ioloop.IOLoop.current().start()