From ab212d4be0f22df5ce315fe179fe3aa232e53820 Mon Sep 17 00:00:00 2001 From: Luis Mayo Valbuena Date: Thu, 22 Apr 2021 22:13:06 +0200 Subject: [PATCH] Added the ability to show meda as evidence --- ace-attorney-reddit-bot | 2 +- comment_list_brige.py | 10 ++++++++++ main.py | 16 +++++++++++++++- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/ace-attorney-reddit-bot b/ace-attorney-reddit-bot index f268d7f..277c621 160000 --- a/ace-attorney-reddit-bot +++ b/ace-attorney-reddit-bot @@ -1 +1 @@ -Subproject commit f268d7f5d52965c95e6e6bd2fbd4c2880db8394a +Subproject commit 277c6211e5074f157e097c9c01cac4c22c622725 diff --git a/comment_list_brige.py b/comment_list_brige.py index 091163e..7e40efa 100644 --- a/comment_list_brige.py +++ b/comment_list_brige.py @@ -1,3 +1,5 @@ +import requests + class Comment: def __init__(self, tweet): self.author = Author(tweet.user.name) @@ -5,6 +7,14 @@ def __init__(self, tweet): if (len(self.body) == 0): self.body = '...' self.score = 0 + if (hasattr(tweet,'extended_entities') and tweet.extended_entities is not None + and 'media' in tweet.extended_entities and len(tweet.extended_entities['media']) > 0): + url = tweet.extended_entities['media'][0]['media_url_https'] + '?format=png&name=small' + name = tweet.extended_entities['media'][0]['media_url_https'].split('/')[-1] + '.png' + response = requests.get(url) + with open(name, 'wb') as file: + file.write(response.content) + self.evidence = name class Author: def __init__(self, name): diff --git a/main.py b/main.py index 4e3af4d..92c2e6a 100644 --- a/main.py +++ b/main.py @@ -131,7 +131,7 @@ def process_tweets(): except Exception as second_error: print(second_error) print(e) - os.remove(output_filename) + clean(thread, output_filename) else: try: api.update_status('@' + tweet.author.screen_name + " There should be at least two people in the conversation", in_reply_to_status_id=tweet.id_str) @@ -139,8 +139,22 @@ def process_tweets(): print(e) time.sleep(1) except Exception as e: + clean(thread, output_filename) print(e) + +def clean(thread, output_filename): + try: + os.remove(output_filename) + except Exception as second_e: + print(second_e) + try: + for comment in thread: + if (hasattr(comment, 'evidence')): + os.remove(comment.evidence) + except Exception as second_e: + print(second_e) + def restore_account(): global mention_queue global next_account