From 5bf8fd6543f4714b787c4464a8bf1eb8b35b55cb Mon Sep 17 00:00:00 2001 From: Anatoly Kopyl Date: Tue, 6 Apr 2021 15:18:40 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20Made=20a=20pretty=20ui?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 57 +++++++++++++++++++++++++++++++------------ utils/authenticate.py | 1 + utils/savePhoto.py | 8 ++++++ 3 files changed, 50 insertions(+), 16 deletions(-) create mode 100644 utils/savePhoto.py diff --git a/main.py b/main.py index 36c6dbe..1cadcf4 100644 --- a/main.py +++ b/main.py @@ -1,18 +1,22 @@ import configparser import ast +import sys from time import sleep from rich import print#, inspect from rich.console import Console from utils.authenticate import authenticate from utils.detect import detect +from utils.savePhoto import save_photo config = configparser.ConfigParser() config.read('config.ini') terms = ast.literal_eval(config['SEARCH']['terms']) -interests = ast.literal_eval(config['SEARCH']['interests']) +if config['SEARCH']['interests'] != "all": + interests = ast.literal_eval(config['SEARCH']['interests']) c = Console() +c.print('[b]Dochunt[/b] starting...', style='yellow') vk = authenticate() queries = [] @@ -24,22 +28,43 @@ for i in range(len(terms)): photos_processed = 0 photos_saved = 0 + +c.rule() +c.print('Watching documents :eyes:') +c.print(f'> Documents scanned {photos_processed}') +c.print(f'> Documents saved {photos_saved}') while True: - for query in queries: - response = vk.docs.search(q=query['string'], count=1) - image_url = response.popitem()[1][0]['url'] # WTF not readable - image_url_clean = image_url.split('?')[0] # Get url without params + try: + for query in queries: + response = vk.docs.search(q=query['string'], count=1) + image_url = response.popitem()[1][0]['url'] # WTF not readable + image_url_clean = image_url.split('?')[0] # Get url without params - # If the image we are getting is new do stuff - if image_url_clean != query['last_url']: - photos_processed += 1 - query['last_url'] = image_url_clean + # If the image we are getting is new do stuff + if image_url_clean != query['last_url']: + photos_processed += 1 + query['last_url'] = image_url_clean - text = detect(image_url+query['string']) - for interest in interests: - if interest in text: - photos_saved += 1 - c.print('Found an interesting photo!', style="green") - c.print(image_url) + text = detect(image_url+query['string']) + if interests == "all": + if not text.isspace(): + photos_saved += 1 + save_photo(image_url) + else: + for interest in interests: + if interest in text: + photos_saved += 1 + save_photo(image_url) + + sys.stdout.write("\033[F") + sys.stdout.write("\033[K") + sys.stdout.write("\033[F") + sys.stdout.write("\033[K") + c.print(f'> Documents scanned {photos_processed}') + c.print(f'> Documents saved {photos_saved}') - sleep(1) + sleep(1) + + except KeyboardInterrupt: + c.print('Goodbye!', style='blue') + sys.exit() \ No newline at end of file diff --git a/utils/authenticate.py b/utils/authenticate.py index cc4d9e4..d7b3325 100644 --- a/utils/authenticate.py +++ b/utils/authenticate.py @@ -15,5 +15,6 @@ def _2fa_handler(): def authenticate(): vk_session = vk_api.VkApi(login=login, password=password, auth_handler=_2fa_handler) vk_session.auth() + c.print('Authentication success!', style='blue') return vk_session.get_api() \ No newline at end of file diff --git a/utils/savePhoto.py b/utils/savePhoto.py new file mode 100644 index 0000000..3ce0022 --- /dev/null +++ b/utils/savePhoto.py @@ -0,0 +1,8 @@ +from rich.console import Console +c = Console() + +def save_photo(url): + f = open("output.txt", "a", encoding='utf-8') + f.write(url) + f.close() + \ No newline at end of file