-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.py
85 lines (68 loc) · 2.31 KB
/
utilities.py
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
"""This python will handle some extra functions."""
import sys
from os.path import exists
import ddddocr
import yaml
from yaml import SafeLoader
def config_file_generator():
"""Generate the template of config file"""
with open('config.yml', 'w', encoding="utf8") as f:
f.write("""# ++--------------------------------++
# | FCU-AutoClass |
# | Made by LD (MIT License) |
# ++--------------------------------++
# FCU Account
username: ''
password: ''
# Class to join
# If you have more than one class to join, please separate them with space.
# Example: class_id: '0050 0051'
# The less class_id you have, the more rate you can get the class you want.
class_id: ''
# Headless mode
# If you want to run this script in headless mode, please set this to true.
headless: false
"""
)
sys.exit()
def read_config():
"""Read config file.
Check if config file exists, if not, create one.
if exists, read config file and return config with dict type.
:rtype: dict
"""
if not exists('./config.yml'):
print("Config file not found, create one by default.\nPlease finish filling config.yml")
with open('config.yml', 'w', encoding="utf8"):
config_file_generator()
try:
with open('config.yml', 'r', encoding="utf8") as f:
data = yaml.load(f, Loader=SafeLoader)
class_ids = get_class_ids(data['class_id'])
config = {
'username': data['username'],
'password': data['password'],
'class_ids': class_ids,
'headless': data['headless']
}
return config
except (KeyError, TypeError):
print(
"An error occurred while reading config.yml, please check if the file is corrected filled.\n"
"If the problem can't be solved, consider delete config.yml and restart the program.\n")
sys.exit()
def get_class_ids(class_id):
"""Read class_id from config file.
:rtype: list
"""
class_ids = class_id.split(" ")
return class_ids
def get_ocr_answer(ocr_image_path):
"""Get the answer of ocr.
:rtype: str
"""
ocr = ddddocr.DdddOcr()
with open(ocr_image_path, 'rb') as f:
image = f.read()
answer = ocr.classification(image)
return answer