Handle videos properly, move downloading function to httptools

This commit is contained in:
Klaus-Uwe Mitterer 2017-03-15 20:43:36 +01:00
parent 3b8dc335a9
commit 2c26febb2a
3 changed files with 22 additions and 12 deletions

View file

@ -2,16 +2,6 @@
import argparse, dbtools, filters.filler, requests, setuptools, time, 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()
def getTweets(db=dbtools.dbHelper(), user=twitools.twObject().whoami(), two=twitools.twObject()):
query = "from:" + user
savepoint = db.getLatestTweet() + 1
@ -34,7 +24,10 @@ def getTweets(db=dbtools.dbHelper(), user=twitools.twObject().whoami(), two=twit
if 'media' in status.entities:
mid = 0
for m in status.entities['media']:
downloadMedia(m['media_url'], status.id, mid)
if "video" in m["expanded_url"]:
mediatools.videoHandler(m["expanded_url"], status.id, mid)
else:
mediatools.photoHandler(m['media_url'], status.id, mid)
mid += 1
last = status.id

View file

@ -1,4 +1,14 @@
import setuptools, pyshorteners, requests
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()
def longURL(url):
return requests.get(url, allow_redirects=True).url

7
mediatools/__init__.py Normal file
View file

@ -0,0 +1,7 @@
import httptools, youtube_dl
def photoHandler(url, tid, mid):
return httptools.downloadMedia(url, tid, mid)
def videoHandler(url, tid, mid):
youtube_dl.YoutubeDL({"outtmpl": "media/%s_%s.%s" % (tid, mid, "%(ext)s")}).download([url])