-
Notifications
You must be signed in to change notification settings - Fork 3
/
common.py
329 lines (241 loc) · 9.45 KB
/
common.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
import pytumblr
import os, sys, traceback
import glob
import re
import time
from datetime import datetime
from bs4 import BeautifulSoup
import requests
import subprocess
import hashlib
from random import randint
from timelapse import getCanonicalUrl
from timelapse import getFolderNameFromUrlOld2
from getConfig import getConfigParameters
globalBlogName = getConfigParameters('globalBlogName')
globalPrefix = getConfigParameters('globalPrefix')
messageSuiteFileName = globalPrefix + 'statusUpdateMessageSuite.txt'
# Authenticate via OAuth
tumblrConsumerKey = getConfigParameters('tumblrConsumerKey')
tumblrConsumerSecret = getConfigParameters('tumblrConsumerSecret')
tumblrAccessToken = getConfigParameters('tumblrAccessToken')
tumblrAccessTokenSecret = getConfigParameters('tumblrAccessTokenSecret')
client = pytumblr.TumblrRestClient(
tumblrConsumerKey,
tumblrConsumerSecret,
tumblrAccessToken,
tumblrAccessTokenSecret
)
'''
input:canonicalURL
'''
def getFormattedTagURL(canonicalURL):
canonicalURL = canonicalURL.strip()
if( len(canonicalURL) > 0 ):
canonicalURL = re.sub(r'\W+', '.', canonicalURL)
return canonicalURL
'''
input:canonicalURL
'''
def getHashString(canonicalURL):
md5hash = ''
canonicalURL = canonicalURL.strip()
if( len(canonicalURL) > 0 ):
# Assumes the default UTF-8; http://www.pythoncentral.io/hashing-strings-with-python/
hash_object = hashlib.md5(canonicalURL.encode())
md5hash = hash_object.hexdigest()
return md5hash
#precondition: first line: What Did http://www.example.com Look Like From beginYear To EndYear?
def extractBeginAndEndYear(firstLineOfUrlsFile):
if( len(firstLineOfUrlsFile) > 0 ):
indexOfFrom = firstLineOfUrlsFile.rfind('From')
#index of "From" + length of "From"
years = firstLineOfUrlsFile[indexOfFrom + 4:]
#remove ?
years = years[:-1]
years = years.split('To')
beginYear = years[0].strip()
endYear = years[1].strip()
return beginYear, endYear
#precondition gif filname has OptDelay
def getGifFilename(folderName):
newfile = ''
if(len(folderName) > 0):
currentFolder = os.getcwd()
os.chdir(folderName)
for file in glob.glob("*.gif"):
indexOfMarker = file.find("Delay")
if(indexOfMarker > -1):
newfile = file[:indexOfMarker] + 'Opt' + file[indexOfMarker:]
break
os.chdir(currentFolder)
return newfile
#precondition: links file is called urlsFile.txt
def getLinks(folderName):
folderName = folderName.strip()
if( len(folderName) > 0 ):
try:
urlsFile = open(globalPrefix + '/' + folderName + '/urlsFile.txt', 'r')
urls = urlsFile.read()
urlsFile.close()
return urls
except:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print((fname, exc_tb.tb_lineno, sys.exc_info() ))
#returns post id, beginYear, endYear
def uploadAnimatedGifToSocialMedia(folderName, URL, queueOrPublish='queue'):
folderName = folderName.strip()
URL = URL.strip()
if( len(folderName) > 0 and len(URL) > 0 and (queueOrPublish == 'queue' or queueOrPublish =='publish') ):
tldURL = getFolderNameFromUrlOld2(URL)
canonicalURL = getCanonicalUrl(URL)
#TAG-MODIFICATION-ZONE
canonicalURL = getFormattedTagURL(canonicalURL)
if( canonicalURL != tldURL ):
tags = canonicalURL + ',' + tldURL
else:
tags = canonicalURL
links = getLinks(folderName)
indexOfNewLineCharacter = links.find('\n')
firstline = links[:indexOfNewLineCharacter]
beginYear, endYear = extractBeginAndEndYear(firstline)
#gifAnimationFilename = globalPrefix + folderName + '/' + getGifFilename(folderName)
mp4Filename = globalPrefix + folderName + '/' + folderName + 'WithAudio.mp4'
uploadFile = ""
if os.path.exists(mp4Filename):
uploadFile = mp4Filename
else:
for item in os.listdir(globalPrefix + folderName + '/'):
if item.endswith(".png"):
uploadFile = globalPrefix + folderName + '/' + item
break
if(len(uploadFile) > 0):
#instagram currently doesn't support posting videos via a web browser
'''
instaScript = os.path.join(os.path.dirname(__file__), globalPrefix+'instagram.js')
username = getConfigParameters('instagramUsername')
password = getConfigParameters('instagramPassword')
nodeSystemPath = getConfigParameters('nodeSystemPath')
print("...uploading to instagram")
res = subprocess.check_output([nodeSystemPath, instaScript, username, password, globalPrefix + folderName + '/' + folderName + '.mp4', links])
instagramLink = res.decode('utf-8')
print(instagramLink)
instagramLink = instagramLink.replace('\n',"")
instagramLink = instagramLink.replace('Instagram Link: ','')
'''
instaScript = os.path.join(os.path.dirname(__file__), globalPrefix+'instagramWithBrowserStack.py')
username = getConfigParameters('instagramUsername')
password = getConfigParameters('instagramPassword')
browserStackUserID = getConfigParameters('browserStackUserID')
browserStackKey = getConfigParameters('browserStackKey')
instaAppPath = glob.glob(globalPrefix+"*.apk")[0]
print("...uploading to Instagram")
pythonVirtualEnvPath = getConfigParameters('pythonVirtualEnv1Path')
instaCaption = links.split("\n")[0] + " #memento"
res = subprocess.check_output([pythonVirtualEnvPath, instaScript, username, password, browserStackUserID, browserStackKey, instaAppPath, uploadFile, instaCaption])
instagramLink = res.decode('utf-8')
instagramLink = instagramLink.replace('\n',"")
instagramLink = instagramLink.split('Instagram Link: ')[-1]
instagramLink = re.sub('^https?:\/\/(www\.)?', '', instagramLink.split('/?')[0])
print(instagramLink)
print("...uploading to tumblr")
if uploadFile.endswith(".mp4"):
postID = client.create_video(globalBlogName, tags=[tags], state=queueOrPublish, caption=[links], data=uploadFile)
else:
postID = client.create_photo(globalBlogName, tags=[tags], state=queueOrPublish, caption=[links], data=uploadFile)
#write this postID to tumblrDataFile.txt
return postID['id'], beginYear, endYear, instagramLink
return -1, '', '', ''
def getPostDateTime(postTag):
postTag = postTag.strip()
postDateTime = ''
if( len(postTag) ):
params = {'tag': postTag, 'limit': 1}
postJson = client.posts(globalBlogName, **params)
postDateTime = postJson['posts'][0]['date']
return postDateTime
def getPostID(postTag):
postTag = postTag.strip()
postID = ''
if( len(postTag) ):
params = {'tag': postTag, 'limit': 1}
postJson = client.posts(globalBlogName, **params)
postID = postJson['posts'][0]['id']
return postID
#http://stackoverflow.com/questions/4770297/python-convert-utc-datetime-string-to-local-datetime
def datetime_from_utc_to_local(utc_datetime):
now_timestamp = time.time()
offset = datetime.fromtimestamp(now_timestamp) - datetime.utcfromtimestamp(now_timestamp)
return utc_datetime + offset
'''
input: canonicalURLHash
return value:
-1 - error
0 - not posted
1 - posted
'''
def isPosted(canonicalURLHash):
returnCode = 0
canonicalURLHash = canonicalURLHash.strip()
if( len(canonicalURLHash) > 0 ):
canonicalURLHash = 'https://'+globalBlogName+'/tagged/'+canonicalURLHash
try:
'''
co = 'curl -I -A "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36" "'+canonicalURLHash+'"'
header = subprocess.getoutput(co)
if(header.find('HTTP/1.1 200 OK') > -1):
returnCode = 1
'''
req = requests.get(canonicalURLHash)
if req.status_code == 200:
returnCode = 1
except:
returnCode = -1
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print((fname, exc_tb.tb_lineno, sys.exc_info() ))
return returnCode
def getRandomStatusUpdateMessage(folderNameUrl, pageTitle, beginYear, endYear, link):
randomMessage = ''
if( len(folderNameUrl) > 0 and len(pageTitle) > 0 and len(link) > 0 and len(beginYear) > 0 and len(endYear) > 0 ):
try:
messageSuiteFile = open(messageSuiteFileName, 'r')
lines = messageSuiteFile.readlines()
messageCount = len(lines)
if( messageCount > 0 ):
randomMessageIndex = randint(0, messageCount-1)
randomMessage = lines[randomMessageIndex].strip()
folderNameUrl = folderNameUrl.strip()
pageTitle = pageTitle.strip()
beginYear = beginYear.strip()
endYear = endYear.strip()
link = link.strip()
randomMessage = randomMessage.replace('folderName', folderNameUrl)
randomMessage = randomMessage.replace('pageTitle', pageTitle)
randomMessage = randomMessage.replace('beginYear', beginYear)
randomMessage = randomMessage.replace('endYear', endYear)
randomMessage = randomMessage.replace('link', link)
#randomMessage = randomMessage + '\n#memento'
except:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print((fname, exc_tb.tb_lineno, sys.exc_info() ))
return randomMessage
def getPageTitle(url):
titleOfPage = ''
if( len(url) > 0 ):
try:
'''
req = urllib.request.Request(url)
req.add_header('User-agent', 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36')
response = urllib.request.urlopen(req)
'''
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
titleOfPage = str(soup.title.string)
#this line added because some titles contain funny characters that generate encoding errors
#titleOfPage = titleOfPage.encode('ascii', 'ignore')
except:
titleOfPage = url + '...'
return titleOfPage.replace("\n", "")