twitools/httptools/__init__.py
2017-12-06 18:20:13 +01:00

22 lines
799 B
Python

import codecs, setuptools, pyshorteners, requests, twitools, sys
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()
def longURL(url):
return requests.get(url, allow_redirects=True).url
def shortURL(url):
try:
return pyshorteners.Shortener('Google', api_key=setuptools.getSetting("Google", "googl")).short(longURL(url))
except pyshorteners.exceptions.ShorteningErrorException as e:
codecs.getwriter("utf8")(sys.stdout.buffer).write("Error handling %s\n" % url)
print(e)
return False