2017-03-15 19:43:36 +00:00
|
|
|
import setuptools, pyshorteners, requests, twitools
|
|
|
|
|
|
|
|
def downloadMedia(url, tid, mid, two = twitools.twoHelper()):
|
|
|
|
remote = two.auth.oauth.get(url, auth=two.api.auth.apply_auth())
|
|
|
|
filename = "media/%s_%i.%s" % (str(tid), int(mid), url.split(".")[-1])
|
|
|
|
|
|
|
|
with open(filename, 'wb') as outfile:
|
|
|
|
for chunk in remote.iter_content(chunk_size=1024):
|
|
|
|
if chunk:
|
|
|
|
outfile.write(chunk)
|
|
|
|
outfile.flush()
|
2017-03-13 21:55:49 +00:00
|
|
|
|
|
|
|
def longURL(url):
|
2017-03-13 22:31:05 +00:00
|
|
|
return requests.get(url, allow_redirects=True).url
|
2017-03-13 18:59:25 +00:00
|
|
|
|
|
|
|
def shortURL(url):
|
|
|
|
try:
|
2017-03-13 21:55:49 +00:00
|
|
|
return pyshorteners.Shortener('Google', api_key=setuptools.getSetting("Google", "googl")).short(longURL(url))
|
2017-03-13 18:59:25 +00:00
|
|
|
except pyshorteners.exceptions.ShorteningErrorException as e:
|
|
|
|
print("Error handling %s" % url)
|
2017-03-13 21:55:49 +00:00
|
|
|
print(e)
|
|
|
|
return False
|