From 4755215271bafa09b407a85baf6ab5bc6fe167a1 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Mon, 27 Feb 2017 00:56:37 +0100 Subject: [PATCH] Remove duplicated code in getMessages(), implement media download for messages --- filler.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/filler.py b/filler.py index d9ce7e9..91887d0 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" % (int(tid), int(mid), url.split(".")[-1]) + 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): @@ -50,7 +50,7 @@ def getMessages(db=dbtools.dbHelper(), two=twitools.twObject()): new_messages = two.api.direct_messages(since_id=savepoint, count=200, full_text=True) new_out_messages = two.api.sent_direct_messages(since_id=savepoint, count=200, full_text=True) - for m in new_messages: + for m in (new_messages + new_out_messages): if dbtools.fillerfilter.messageFilter(m, True): try: db.executeQuery("INSERT INTO messages VALUES(%s, '%s', %s, %s, '%s')" % (m.id, setuptools.unescapeText(m.text), m.sender_id, m.recipient_id, m.created_at)) @@ -58,13 +58,11 @@ def getMessages(db=dbtools.dbHelper(), two=twitools.twObject()): except: pass - for m in new_out_messages: - if dbtools.fillerfilter.messageFilter(m, False): - try: - db.executeQuery("INSERT INTO messages VALUES(%s, '%s', %s, %s, '%s')" % (m.id, setuptools.unescapeText(m.text), m.sender_id, m.recipient_id, m.created_at)) - mcount += 1 - except: - pass + if 'media' in m.entities: + mid = 0 + for med in m.entities['media']: + downloadMedia(med['media_url'], status.id, mid) + mid += 1 db.commit()