From 57c2ba5ccc75de2f7cf01ba6949de7a4f4a98948 Mon Sep 17 00:00:00 2001 From: ThaaoBlues Date: Sat, 2 Jul 2022 19:23:50 +0200 Subject: [PATCH] first try of isbn auto lookup --- client.py | 1 - copypasta.py | 2 +- requirements.txt | 1 + templates/index.html | 4 ++-- test.py | 38 ++++++++++++++++++++++++++++++++------ util.py | 30 +++++++++++++++++++++++++++++- 6 files changed, 65 insertions(+), 11 deletions(-) diff --git a/client.py b/client.py index 7fd6ced..d6e5888 100644 --- a/client.py +++ b/client.py @@ -1,5 +1,4 @@ import requests -from ast import literal_eval from util import * def send_text_scan(text): diff --git a/copypasta.py b/copypasta.py index 329003d..e62cf4a 100644 --- a/copypasta.py +++ b/copypasta.py @@ -566,7 +566,7 @@ def upload(): isbn = r - store_to_history({"file_type" : "isbn", "content" : f"{isbn}", "date" :f"{time}"}) + store_to_history({"file_type" : "isbn", "content" : f"{isbn}", "date" :f"{time}","isbn_lookup":identify_product(isbn)}) return jsonify({"upload_status" : "true"}) diff --git a/requirements.txt b/requirements.txt index d82069e..afb1c79 100644 --- a/requirements.txt +++ b/requirements.txt @@ -6,3 +6,4 @@ pyautogui pillow waitress webtest +beautifulsoup4 diff --git a/templates/index.html b/templates/index.html index 77484d1..00fb044 100644 --- a/templates/index.html +++ b/templates/index.html @@ -246,9 +246,9 @@

Last files/data sent :

//barcode type }else if(obj.file_type == "isbn"){ - search_icon = ""; + search_icon = ""; - tab_element = "
isbn:"+obj.content+"
"+search_icon+copy_content_icon+delete_scan_icon+"
"; + tab_element = "
isbn: "+obj.content+" | name: "+obj.isbn_lookup.name+"
"+search_icon+copy_content_icon+delete_scan_icon+"
"; //emails }else if(obj.file_type == "email"){ diff --git a/test.py b/test.py index 510efd6..3f17947 100644 --- a/test.py +++ b/test.py @@ -1,9 +1,35 @@ -from ast import literal_eval -from os import startfile from requests import get +from json import loads +from bs4 import BeautifulSoup +#isbn = "3302740068027" +#isbn = "978209157545" +isbn = "3270220060949" -startfile("C:/") - - -print(literal_eval(get("https://api.github.com/repos/CopyPastaOfficial/CopyPasta/tags").text)[0]['name']) \ No newline at end of file +def identify_product(isbn:str): + + + + # edible product ? + r = get(f"http://world.openfoodfacts.org/api/v0/product/{isbn}") + r = loads(r.text) + if "product" in r.keys(): + r = r["product"] + + return {"name":r["product_name"]+ " - "+r["brands"] if "brands" in r.keys() else r["product_name"] ,"url":f"https://world.openfoodfacts.org/product/{isbn}"} + + + # book ? + r = get(f"https://www.isbnsearcher.com/books/{isbn}",headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"}) + + if r.status_code == 200: + r = BeautifulSoup(r.text,"html.parser") + + return {"name":r.find("h1").get_text(),"url":f"https://www.isbnsearcher.com/books/{isbn}"} + + else: + return {"name":isbn,"url":f"https://www.google.com/search?q={isbn}"} + +#print(identify_product(isbn)) +# amazon lookup +r = get("https://www.amazon.fr/s?k=9782091575452",headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"},allow_redirects=True) diff --git a/util.py b/util.py index fba836a..44b3063 100644 --- a/util.py +++ b/util.py @@ -2,6 +2,7 @@ from getpass import getuser from random import randint from json import * +from bs4 import BeautifulSoup from requests import get from os import mkdir, path, remove from xml.etree import ElementTree @@ -327,4 +328,31 @@ def remove_copypasta_port_redirect(): def is_image(file_type:str): - return file_type in ["jpeg","jpg","png","ico","gif","apng","avif","gif","jfif","pjpeg","pjp","svg","webp"] \ No newline at end of file + return file_type in ["jpeg","jpg","png","ico","gif","apng","avif","gif","jfif","pjpeg","pjp","svg","webp"] + + + +def identify_product(isbn:str): + + + + # edible product ? + r = get(f"http://world.openfoodfacts.org/api/v0/product/{isbn}") + r = loads(r.text) + + if "product" in r.keys(): + r = r["product"] + + return {"name":r["product_name"]+ " - "+r["brands"] if "brands" in r.keys() else r["product_name"] ,"url":f"https://world.openfoodfacts.org/product/{isbn}"} + + + # book ? + r = get(f"https://www.isbnsearcher.com/books/{isbn}",headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37"}) + + if r.status_code == 200: + r = BeautifulSoup(r.text,"html.parser") + + return {"name":r.find("h1").get_text(),"url":f"https://www.isbnsearcher.com/books/{isbn}"} + + else: + return {"name":isbn,"url":f"https://www.google.com/search?q={isbn}"} \ No newline at end of file