/
pyuploader.py
124 lines (88 loc) · 2.54 KB
/
pyuploader.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
'''
Modules required:
poster
urllib2
os
'''
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
import os
from datetime import datetime
#PARAMETERS
camera_id = '2f8ef4e4eba722a38c2d' #camera_id from /admin/newcamera
file_end = 'jpg' #file ending. Do not include the .
dir = './' #relative path from python script to image file
#"./" = same folder
#"../"= one level up
#"../images/"=one level up, then into "images" folder - make sure to include the final slash
#etc
#LOCAL VS LIVE
# base_url = 'http://aegissurv.appspot.com'
base_url = 'http://localhost:8096'
def parse_time(filename):
string_time = filename[:-len(file_end)-1]
return string_time
def findfiles():
#uploaded count
upload_count = 0
#find files
for f in os.listdir(dir):
if f[-len(file_end):] == file_end:
print('uploading file:'+dir+f)
#upload file
upload_url = get_upload_url()
t = parse_time(f)
post_image(upload_url,dir+f,t)
#increment
upload_count += 1
#no files found
if upload_count == 0:
print('No files found that end in .'+file_end)
def get_upload_url():
#get the upload url from GAE
#try a maximum of 5 times
url_attempts = 0
while url_attempts < 5:
try:
urlrequest = urllib2.urlopen(base_url+"/upload/geturl")
upload_url = urlrequest.read()
return upload_url
except Exception,e:
print("Error getting upload URL")
print(e)
print("Retrying...")
url_attempts += 1
def post_image(upload_url,filename,time):
# Register the streaming http handlers with urllib2
#try a maximum of 5 times
upload_attempts = 0
while upload_attempts < 5:
try:
register_openers()
datagen, headers = multipart_encode({"file": open(filename),"time":time,"camera_id":camera_id,"token":token})
request = urllib2.Request(upload_url, datagen, headers)
response = urllib2.urlopen(request).read()
print response
if response == '200':
#delete files here
os.remove(filename)
print('RESPONSE OKAY FILE DELETED')
return True
else:
print('SERVER ERROR FILE NOT DELETED')
return False
except Exception,e:
print("Error posting file")
print(e)
print("Retrying...")
upload_attempts += 1
if upload_attempts == 5:
os.remove(filename)
print('Image was deleted after 5 failed attempts to post')
return False
###DO NOT CHANGE###
token = '3g6cz8IoZ2OOhFhkZQVCJgsEtgFZVxMe' #Don't change this - it's hardcoded on both the python and server sides
###DO NOT CHANGE###
#find files and post
findfiles()