From dce7d56c516c9e8b4b86d7360b762e19870ef3fa Mon Sep 17 00:00:00 2001 From: Swarnaditya <69480361+DemonKingSwarn@users.noreply.github.com> Date: Mon, 13 Jun 2022 16:43:05 +0530 Subject: [PATCH 1/2] Chore: made it faster --- hart-cli.py | 106 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 86 insertions(+), 20 deletions(-) diff --git a/hart-cli.py b/hart-cli.py index 2ce9349..7f9e252 100755 --- a/hart-cli.py +++ b/hart-cli.py @@ -1,20 +1,34 @@ #!/usr/bin/env python3 -import requests -from bs4 import BeautifulSoup +import httpx +from bs4 import BeautifulSoup as bs +import pyperclip as clip + import os import subprocess -import pyperclip as clip -os.system('clear') -download_path = os.environ['HOME']+"/Pictures/hart-cli" +from os.path import expanduser + +headers = { + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64; rv:101.0) Gecko/20100101 Firefox/101.0" +} + +client = httpx.Client(headers=headers, follow_redirects=True) + +home = expanduser("~") +download_path = f"{home}/pix/hart-cli" +os.system(f"mkdir -p {download_path}") + item = 0 page_num = 1 -URL = "https://yande.re/post?page="+str(page_num) -page = requests.get(URL) + +url = f"https://yande.re/post?page={page_num}" +page = client.get(url) + links_arr_full = [] links_arr_preview = [] + def get_new_urls(): - global URL + global url global page global page_num global soup @@ -23,16 +37,17 @@ def get_new_urls(): global links_arr_full global links_preview global links_arr_preview + links_arr_full.clear - links_arr_full.clear - URL = "https://yande.re/post?page="+str(page_num) - page = requests.get(URL) - soup = BeautifulSoup(page.content, "html.parser") + links_arr_preview.clear + + soup = bs(page.content, "html.parser") main_content = soup.find(id="post-list-posts") main_content = str(main_content) - main_content = main_content.replace("smallimg","largeimg") - main_content = BeautifulSoup(main_content, features="lxml") + main_content = main_content.replace("smallimg", "largeimg") + main_content = bs(main_content, features="lxml") main_content = main_content.find(id="post-list-posts") + links_full = main_content.find_all_next("a", class_="directlink largeimg") links_arr_full = [] links_preview = main_content.find_all_next("img", class_="preview") @@ -47,19 +62,70 @@ def get_new_urls(): def next(): global item global page_num + if item != len(links_arr_preview)-1: - item+=1 - os.system('clear') + item += 1 else: - page_num+=1 - item=1 + page_num += 1 + item = 1 get_new_urls() - os.system('clear') -def previus(): +def previous(): global item global page_num global links_arr_preview + + if item != 1: + item -= 1 + else: + page_num -= 1 + get_new_urls() + item = len(links_arr_preview)-1 + +def download(): + global item + global links_arr_full + global download_path + + command = 'echo ' + links_arr_full[item] + ' | cut -d "%" -f 2 |cut -b 3-8' + name = subprocess.check_output(command, shell=True, text=True, encoding='utf_8') + name = name.strip('\n') + name = str(name)+".jpg" + command = "curl -s -o " + download_path + "/" + name + " " + links_arr_full[item] + os.system(command) + +get_new_urls() + +while True: + command = "curl -s -o /tmp/hart-preview.jpg " + links_arr_preview[item] + os.system(command) + command = "convert /tmp/hart-preview.jpg -resize 500x500 /tmp/hart-preview.jpg" + os.system(command) + command = "kitty +icat --place 100x100@0x0 /tmp/hart-preview.jpg" + os.system(command) + print("next:\t\tn") + print("previous:\tp") + print("download:\td") + print("copy URL:\tc") + print("quit:\t\tq") + choice= input() + if choice == "n": + next() + elif choice == "p": + previus() + elif choice == "d": + download() + elif choice == "c": + clip.copy(links_arr_full[item]) + os.system('clear') + elif choice == "q": + os.system('clear') + exit() + else: + print("invaled awnser") + exit(0) + + if item != 1: item-=1 os.system('clear') From fe25308d532b8e8fc6b1225a9251a517cefb6fc2 Mon Sep 17 00:00:00 2001 From: Swarnaditya <69480361+DemonKingSwarn@users.noreply.github.com> Date: Mon, 13 Jun 2022 17:17:26 +0530 Subject: [PATCH 2/2] Update hart-cli.py --- hart-cli.py | 58 ++++++----------------------------------------------- 1 file changed, 6 insertions(+), 52 deletions(-) diff --git a/hart-cli.py b/hart-cli.py index 7f9e252..4f04b04 100755 --- a/hart-cli.py +++ b/hart-cli.py @@ -38,6 +38,8 @@ def get_new_urls(): global links_preview global links_arr_preview + os.system("clear") + links_arr_full.clear links_arr_preview.clear @@ -63,6 +65,7 @@ def next(): global item global page_num + os.system("clear") if item != len(links_arr_preview)-1: item += 1 else: @@ -75,6 +78,7 @@ def previous(): global page_num global links_arr_preview + os.system("clear") if item != 1: item -= 1 else: @@ -93,6 +97,7 @@ def download(): name = str(name)+".jpg" command = "curl -s -o " + download_path + "/" + name + " " + links_arr_full[item] os.system(command) + os.system("clear") get_new_urls() @@ -112,7 +117,7 @@ while True: if choice == "n": next() elif choice == "p": - previus() + previous() elif choice == "d": download() elif choice == "c": @@ -125,54 +130,3 @@ while True: print("invaled awnser") exit(0) - - if item != 1: - item-=1 - os.system('clear') - else: - page_num-=1 - get_new_urls() - item= len(links_arr_preview)-1 - os.system('clear') - -def download(): - global item - global links_arr_full - global download_path - command = 'echo ' + links_arr_full[item] + ' | cut -d "%" -f 2 |cut -b 3-8' - name = subprocess.check_output(command, shell=True, text=True, encoding='utf_8') - name = name.strip('\n') - name = str(name)+".jpg" - command = "curl -s -o " + download_path + "/" + name + " " + links_arr_full[item] - os.system(command) - os.system('clear') - -get_new_urls() - -while True: - command = "curl -s -o /tmp/hart-preview.jpg " + links_arr_preview[item] - os.system(command) - command = "convert /tmp/hart-preview.jpg -resize 500x500 /tmp/hart-preview.jpg" - os.system(command) - command = "kitty +icat --place 100x100@0x0 /tmp/hart-preview.jpg" - os.system(command) - print("next:\t\tn") - print("previous:\tp") - print("download:\td") - print("copy URL:\tc") - print("quit:\t\tq") - choice= input() - if choice == "n": - next() - elif choice == "p": - previus() - elif choice == "d": - download() - elif choice == "c": - clip.copy(links_arr_full[item]) - os.system('clear') - elif choice == "q": - os.system('clear') - exit() - else: - print("invaled awnser")