-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjob.py
executable file
·215 lines (179 loc) · 6.83 KB
/
job.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
#
# Copyright (c) 2013 Bhautik J Joshi ([email protected])
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import findLibs
from imagedb import imagedb
from imagedb import jobColumns, flickrColumns
from paths import imageBase
from flickrInterface import flickrObject
from facebookInterface import facebookObject
from tumblrInterface import tumblrObject
from twitterInterface import twitterObject
import web
from web import form
import os,sys
import json
import threading
import datetime
import time
def postPendingJobs():
jobIdList = map(lambda x: x[0], imagedb.getJobsToDo())
for jobId in jobIdList:
postJob(jobId)
def postJob(jobId):
jobData = imagedb.getJob(jobId, jobColumns)[0]
jobDict = {}
#TODO: imageid == jobid may not always be true
jobDict["imageId"] = jobId
for idx, val in enumerate(jobColumns):
data = str(jobData[idx])
if data == "None":
data = None
jobDict[val] = data
#cleanup
jobDict["fileurl"] = os.path.join(imageBase, jobDict["dburl"])
jobDict["width"] = int(jobDict["width"])
jobDict["height"] = int(jobDict["height"])
jobDict["jobDict"] = json.loads(jobDict["jobDict"])
if jobDict["tags"] != None:
jobDict["tags"] = jobDict["tags"].split(",")
else:
jobDict["tags"] = []
if jobDict["flickrSets"] != None:
jobDict["flickrSets"] = jobDict["flickrSets"].split(",")
if jobDict["flickrGroups"] != None:
jobDict["flickrGroups"] = jobDict["flickrGroups"].split(",")
jobDict["geoCode"] = int(jobDict["geoCode"])
jobDict["latitude"] = float(jobDict["latitude"])
jobDict["longitude"] = float(jobDict["longitude"])
serviceDict = jobDict["jobDict"]
if "jobStatus" not in serviceDict.keys():
jobStatus = {"flickr":False, "facebook": False, "tumblr": False, "twitter": False}
serviceDict["jobStatus"] = jobStatus
print "GOING TO POST JOB: " + str(jobId)
print serviceDict
if serviceDict["flickr"] != True:
raise Exception("need at least flickr to work!")
else:
if flickrObject.authorised == False:
raise Exception("need at least flickr to work!")
if serviceDict["jobStatus"]["flickr"] == False:
try:
postFlickrImage(jobDict)
serviceDict["jobStatus"]["flickr"] = True
imagedb.updateJobWorking(jobId, "queued", json.dumps(serviceDict))
except:
print "Flickr error:", sys.exc_info()[0]
return
flickrData = imagedb.getJob(jobId, flickrColumns)[0]
jobDict["flickrurl"] = flickrData["url"]
jobDict["flickrshorturl"] = flickrData["shorturl"]
jobDict["flickrImageThumbUrl"] = flickrData["imageThumbUrl"]
jobDict["flickrImageLargeUrl"] = flickrData["imageLargeUrl"]
if serviceDict["facebook"]:
if facebookObject.authorised:
if serviceDict["jobStatus"]["facebook"] == False:
try:
postFacebookPost(jobDict)
serviceDict["jobStatus"]["facebook"] = True
imagedb.updateJobWorking(jobId, "queued", json.dumps(serviceDict))
except:
print "Facebook error:", sys.exc_info()[0]
return
if serviceDict["tumblr"]:
if tumblrObject.authorised:
if serviceDict["jobStatus"]["tumblr"] == False:
try:
postTumblrPost(jobDict)
serviceDict["jobStatus"]["tumblr"] = True
imagedb.updateJobWorking(jobId, "queued", json.dumps(serviceDict))
except:
print "Tumblr error:", sys.exc_info()[0]
return
if serviceDict["twitter"]:
if twitterObject.authorised:
if serviceDict["jobStatus"]["twitter"] == False:
try:
postTwitterPost(jobDict)
serviceDict["jobStatus"]["twitter"] = True
imagedb.updateJobWorking(jobId, "queued", json.dumps(serviceDict))
except:
print "Twitter error:", sys.exc_info()[0]
return
imagedb.updateJobWorking(jobId, "done", json.dumps(serviceDict))
print "job complete! id:" + str(jobId)
def postTwitterPost(jobDict):
twitterObject.postImagePost(jobDict)
def postTumblrPost(jobDict):
tumblrObject.postImagePost(jobDict)
def postFacebookPost(jobDict):
facebookObject.postImagePost(jobDict)
def postFlickrImage(jobDict):
#do it!
try:
photoid = flickrObject.postImage(jobDict)
except:
raise Exception("photo upload error, giving up")
if jobDict["geoCode"] != 0:
try:
flickrObject.setLocation(photoid, jobDict["latitude"], jobDict["longitude"])
except:
print "ERROR IN SETTING LOCATION - you may need to set pref at http://www.flickr.com/account/geo/privacy/"
try:
flickrObject.sendToGroups(photoid, jobDict["flickrGroups"])
except:
print "ERROR IN SENDING TO FLICKR GROUPS"
try:
flickrObject.sendToSets(photoid, jobDict["flickrSets"])
except:
print "ERROR IN SENDING TO FLICKR SETS"
try:
sizes = flickrObject.getPhotoImageSizes(photoid)
except:
raise Exception("photo uploaded but can't get sizes, giving up")
imagethumburl = ""
imagelargeurl = ""
for size in sizes:
if size["label"] == "Large Square":
imagethumburl = size["source"]
elif size["label"] == "Large":
imagelargeurl = size["source"]
infoDict = flickrObject.getPhotoInfo(photoid)
url = infoDict["urls"]["url"][0]["_content"]
shorturl = flickrObject.getShortURL(photoid)
jobDict["flickrurl"] = url
jobDict["flickrshorturl"] = shorturl
jobDict["flickrImageThumbUrl"] = imagethumburl
jobDict["flickrImageLargeUrl"] = imagelargeurl
imagedb.updateFlickrPostUpload(jobDict["imageId"], photoid, url, shorturl, imagethumburl, imagelargeurl)
## Run the job posting thread
minuteSeconds = 60
interval = 10 #runs every interval minutes
class RunJobs(threading.Thread):
def run(self):
while(True):
now = datetime.datetime.now()
print "Running pending jobs at time: %s" % (now)
postPendingJobs()
time.sleep(interval*minuteSeconds)
runJobsThread = RunJobs()
runJobsThread.daemon = True
runJobsThread.start()