From 2c26febb2a79db833ecd19c9b5e4e65b845d9c79 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Wed, 15 Mar 2017 20:43:36 +0100 Subject: [PATCH] Handle videos properly, move downloading function to httptools --- filler.py | 15 ++++----------- httptools/__init__.py | 12 +++++++++++- mediatools/__init__.py | 7 +++++++ 3 files changed, 22 insertions(+), 12 deletions(-) create mode 100644 mediatools/__init__.py diff --git a/filler.py b/filler.py index 877e350..bfe86e5 100755 --- a/filler.py +++ b/filler.py @@ -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 diff --git a/httptools/__init__.py b/httptools/__init__.py index 50dbfa7..3a1300e 100644 --- a/httptools/__init__.py +++ b/httptools/__init__.py @@ -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 diff --git a/mediatools/__init__.py b/mediatools/__init__.py new file mode 100644 index 0000000..3bad3a9 --- /dev/null +++ b/mediatools/__init__.py @@ -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])