From c015769950468ff2a3d4e0a2abffcead71ad1458 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 27 Feb 2017 00:44:50 +0100 Subject: [PATCH] Implement media download in filler --- filler.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/filler.py b/filler.py index 1c69e52..d9ce7e9 100755 --- a/filler.py +++ b/filler.py @@ -4,7 +4,7 @@ import argparse, dbtools, dbtools.fillerfilter, requests, setuptools, time, twit def downloadMedia(url, tid, mid): remote = requests.get(url, stream=True) - filename = "media/%i_%i.%s" % (tid, mid, url.split(".")[-1]) + filename = "media/%i_%i.%s" % (int(tid), int(mid), url.split(".")[-1]) with open(filename, 'wb') as outfile: for chunk in remote.iter_content(chunk_size=1024): @@ -29,7 +29,13 @@ def getTweets(db=dbtools.dbHelper(), user=twitools.twObject().whoami(), two=twit try: db.executeQuery("INSERT INTO tweets(tweet_id,timestamp,text) VALUES(" + str(status.id) + ",'" + timestamp + "','" + text + "')") except: - pass + print("Failed to insert %s into database." % str(status.id)) + + if 'media' in status.entities: + mid = 0 + for m in status.entities['media']: + downloadMedia(m['media_url'], status.id, mid) + mid += 1 last = status.id tw_counter = tw_counter + 1