File size: 3,952 Bytes
82c2688
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import gradio as gr
import pandas as pd
import logging
import requests
from urllib.parse import quote
import yaml

class Products:
    data = []  # Список, в котором будут словари

    def read_yaml_file(self, filename):
        """Read the data from a YAML file and return a list of dictionaries"""
        with open(filename, 'r') as file:
            self.data = yaml.load(file, Loader=yaml.FullLoader)

    def write_yaml_file(self, filename):
        """Write the data in YAML format to a file"""
        with open(filename, 'w') as file:
            yaml.dump(self.data, file)

    def sortProducts(self, criteria: str, order: bool = False):
        """Sort list of products by given criteria:
        \n price - price of a product
        \n rating - total rating of a seller
        \n sold - the amount of items sold
        \n\n order - asc=0, desc=1
        """
        self.data.sort(key=operator.itemgetter(criteria), reverse=order)

    def parseAPI(self, query):
        """Find all elements on page and store them into the dictionary using plati.ru API"""
        self.data = []
        pagesize = 499
        contents = requests.get(f"https://plati.io/api/search.ashx?query={quote(query)}&pagesize={pagesize}&visibleOnly=true&response=json").json()
        total_pages = int(contents['Totalpages'])
        for entry in contents['items']:
            self.data.append(
                {'name': entry['name'], 'link': entry['url'], 'price': int(entry['price_rur']), 'rating': float(entry['seller_rating']), 'sold': int(entry['numsold'])})
        if total_pages > 1:
            for i in range(2, total_pages + 1):
                contents = requests.get(f"https://plati.io/api/search.ashx?query={quote(query)}&pagesize={pagesize}&pagenum={i}&visibleOnly=true&response=json").json()
                for entry in contents['items']:
                    self.data.append(
                        {'name': entry['name'], 'link': entry['url'], 'price': int(entry['price_rur']), 'rating': float(entry['seller_rating']), 'sold': int(entry['numsold'])})

# Функция поиска, которая будет вызываться из интерфейса Gradio
def search(query):
    logging.info(f"Search started with query: {query}")
    products = Products()
    products.parseAPI(query)
    products.write_yaml_file("cache.yaml")
    products.read_yaml_file("cache.yaml")
    logging.info(f"Search results: {products.data}")

    # Создаем новый DataFrame из списка словарей products.data
    df = pd.DataFrame(products.data)
    return df  # Возвращаем DataFrame

# Добавляем функцию greet для демонстрации
def greet(name):
    return "Hello " + name + "!!"

# Создание Gradio интерфейса
def create_interface():
    with gr.Blocks() as demo:
        gr.Markdown("# Plati.market Parser and Greet Function")
        gr.Markdown("### Greeting Section")
        greet_input = gr.Textbox(label="Enter your name")
        greet_button = gr.Button("Greet")
        greet_output = gr.Textbox(label="Greeting")
        greet_button.click(fn=greet, inputs=greet_input, outputs=greet_output)
        
        gr.Markdown("### Plati.market Search Section")
        gr.Markdown("Input what you like to find in the field below. The results will be displayed in the table.")
        search_input = gr.Textbox(label="Search Query")
        search_button = gr.Button("Search")
        data_table = gr.Dataframe(headers=["name", "link", "price", "rating", "sold"], interactive=True, label="Search Results")
        search_button.click(fn=search, inputs=search_input, outputs=data_table)

    return demo

if __name__ == "__main__":
    # Настройка логирования
    logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    
    demo = create_interface()
    demo.launch()