File size: 4,389 Bytes
111b72c
 
 
5924ba7
5bf3b30
 
 
 
 
111b72c
 
4c9c4c6
111b72c
 
 
5924ba7
 
 
111b72c
4c9c4c6
 
111b72c
 
 
 
 
4c9c4c6
 
111b72c
4c9c4c6
 
111b72c
4c9c4c6
5924ba7
 
 
 
111b72c
 
6b5f037
5bf3b30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
572ca12
6b5f037
5bf3b30
 
 
 
 
 
572ca12
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import json

from gradio_client import Client
from pgsoft.pgdate.date_utils import beijing
from pgsoft.pgfile import download, upload, list_files
from pgsoft.pghash.md5 import md5
from time import sleep
from huggingface_hub import HfApi
import os


def call_logger(log_info, caller, hf_token) -> None:
    #######################
    # logging
    #######################
    calling_start = beijing()
    print(f"calling logger starts at {beijing()}")
    #################################################
    urls = [
        "https://hubei-hunan-logger.hf.space",
        "https://hubei-hunan-logger2.hf.space",
    ]
    for url in urls:
        try:
            client = Client(
                url,
                hf_token=hf_token,
                verbose=False,
            )
            client.submit(json.dumps(log_info), caller)
            print(f"[logging to {url}] OK")
        except Exception as e:
            print(f"[logging to {url}] error: {e}")
    #################################################
    calling_end = beijing()
    timecost = calling_end.timestamp() - calling_start.timestamp()
    print(f"calling logger ends at {calling_end}, costs {timecost:.2f}s")


dataset_id = "pgsoft/watermelon"
local_dir = "game"
if not os.path.exists(local_dir):
    os.mkdir(local_dir)
hf_api = HfApi()


def file_service(service, arg: str, token: str):
    """download game, upload game, or list games"""
    if service == "download game":
        filepath = arg.strip() + ".json"
        res = download(
            dataset_id,
            filepath,
            repo_type="dataset",
            localdir=local_dir,
            token=token,
        )
        if not res:
            return None
        with open(res, "r") as f:
            outp = json.load(f)
        print(f"[{service}] OK")
        return outp
    elif service == "upload game":
        try:
            game = json.loads(arg)
        except json.JSONDecodeError as e:
            print(f"[{service}] {type(e)}: {e}")
            return None

        if not isinstance(game, dict):
            print(f"[{service}] not a dict")
            return None

        needed_keys = ["game-file", "device-id"]
        for key in needed_keys:
            if key not in game:
                print(f'[{service}] error: missed "{key}"')
                return None
        if not isinstance(game["device-id"], str):
            print(f'[{service}] error: "device-id" is not a str')
            return None
        if not isinstance(game["game-file"], dict):
            print(f'[{service}] error: "game-file" is not a dict')
            return None

        obj = {
            "upload-time": beijing().__str__(),
            "game-file": game["game-file"],
        }

        maxtry = 5
        for retry in range(maxtry):
            md5code = md5(obj)
            if not hf_api.file_exists(
                repo_id=dataset_id,
                filename=md5code + ".json",
                repo_type="dataset",
                token=token,
            ):
                break
            sleep(0.1)
            obj["upload-time"] = beijing().__str__()
            maxtry -= 1
        filename = md5code + ".json"
        if not maxtry and hf_api.file_exists(
            repo_id=dataset_id,
            filename=md5code + ".json",
            repo_type="dataset",
            token=token,
        ):
            print(f"[{service}] error: file exists")
            return None
        localpath = os.sep.join([local_dir, filename])
        content = json.dumps(game, indent=4)
        with open(localpath, "w") as f:
            f.write(content)
        res = upload(
            localpath,
            filename,
            dataset_id,
            "dataset",
            token,
            f"Updated at {beijing()}",
        )
        if not res:
            print(f"[{service}] error: upload failed")
            return None
        print(f"[{service}] OK")
        return md5code
    elif service == "list games":
        games = list_files(
            repo_id=dataset_id,
            repo_type="dataset",
            token=token,
        )
        if games is None:
            return None
        games = {item.split(".")[0]: item for item in games if len(item) >= 32}
        print(f"[{service}] OK")
        return games
    else:
        print(f"[{service}] error: unknown service")
        return None