File size: 2,069 Bytes
c87f53a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from googleapiclient.http import MediaFileUpload, MediaIoBaseUpload
import json
import os
from io import BytesIO
import time
import io
from comparator.report import checkRespectiveFolder
from comparator.compare_model.compare import compare
def get_compare_value(report):
    return report["compare"]

def compareText(service, summary):
    try:
        fake_folder_id = checkRespectiveFolder(service)
        result_folder_id = checkRespectiveFolder(
            service, path=fake_folder_id, Folder_Name="Result"
        )
        response = (
            service.files()
            .list(
                q="name contains 'data-'",
                spaces="drive",
                fields="files(id, name)",
            )
            .execute()
        )
        list_all_files = response.get("files", [])
        all_reports = []

        for list_files in list_all_files:
            if list_files["name"].startswith("data-"):
                latest_file_id = str(list_files["id"])

                existing_data = (
                    service.files().get_media(fileId=latest_file_id).execute()
                )
                existing_details = json.loads(existing_data.decode("utf-8"))
                file_name = list_files["name"]

                for json_data in existing_details:
                    print(summary)
                    value = compare(summary, json_data["summary"])
                    all_reports.append(
                        {
                            "id": json_data["id"],
                            "year": json_data["year"],
                            "drive": json_data["drive"],
                            "summary": json_data["summary"],
                            "category": json_data["category"],
                            "title": "",
                            "compare": value,
                        }
                    )
        sorted_reports = sorted(all_reports, key=get_compare_value, reverse=True)
        return sorted_reports

    except Exception as e:
        print(f"An error occurred: {str(e)}")