Remove duplicated code in getMessages(), implement media download for messages

This commit is contained in:
Klaus-Uwe Mitterer 2017-02-27 00:56:37 +01:00
parent c015769950
commit fd1d1d5bac

View file

@ -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()