-
Notifications
You must be signed in to change notification settings - Fork 0
/
mini_played_games_parser.py
184 lines (131 loc) · 5.29 KB
/
mini_played_games_parser.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
__author__ = 'ipetrash'
import re
from typing import Dict, List
from urllib.parse import urljoin
import requests
# Регулярка вытаскивает выражения вида: 1, 2, 3 или 1-3, или римские цифры: III, IV
PARSE_GAME_NAME_PATTERN = re.compile(
r'(\d+(, *?\d+)+)|(\d+ *?- *?\d+)|([MDCLXVI]+(, ?[MDCLXVI]+)+)',
flags=re.IGNORECASE
)
FINISHED_GAME = 'FINISHED_GAME'
NOT_FINISHED_GAME = 'NOT_FINISHED_GAME'
FINISHED_WATCHED = 'FINISHED_WATCHED'
NOT_FINISHED_WATCHED = 'NOT_FINISHED_WATCHED'
FLAG_BY_CATEGORY = {
' ': FINISHED_GAME,
'- ': NOT_FINISHED_GAME,
' -': NOT_FINISHED_GAME,
' @': FINISHED_WATCHED,
'@ ': FINISHED_WATCHED,
'-@': NOT_FINISHED_WATCHED,
'@-': NOT_FINISHED_WATCHED,
}
def parse_game_name(game_name: str) -> List[str]:
"""
Функция принимает название игры и пытается разобрать его, после возвращает список названий.
У некоторых игр в названии может указываться ее части или диапазон частей, поэтому для правильного
составления списка игр такие случаи нужно обрабатывать.
Пример:
"Resident Evil 4, 5, 6" -> ["Resident Evil 4", "Resident Evil 5", "Resident Evil 6"]
"Resident Evil 1-3" -> ["Resident Evil", "Resident Evil 2", "Resident Evil 3"]
"Resident Evil 4" -> ["Resident Evil 4"]
"""
match = PARSE_GAME_NAME_PATTERN.search(game_name)
if not match:
return [game_name]
seq_str = match.group(0)
# "Resident Evil 4, 5, 6" -> "Resident Evil"
# For not valid "Trollface Quest 1-7-8" -> "Trollface Quest"
index = game_name.index(seq_str)
base_name = game_name[:index].strip()
seq_str = seq_str.replace(' ', '')
if ',' in seq_str:
# '1,2,3' -> ['1', '2', '3']
seq = seq_str.split(',')
elif '-' in seq_str:
seq = seq_str.split('-')
# ['1', '7'] -> [1, 7]
seq = list(map(int, seq))
# [1, 7] -> ['1', '2', '3', '4', '5', '6', '7']
seq = list(map(str, range(seq[0], seq[1] + 1)))
else:
return [game_name]
# Сразу проверяем номер игры в серии и если она первая, то не добавляем в названии ее номер
return [base_name if num == '1' else base_name + " " + num for num in seq]
def parse_played_games(text: str, silence: bool = False) -> Dict[str, Dict[str, List[str]]]:
"""
Функция для парсинга списка игр.
"""
platforms = dict()
platform = None
for line in text.splitlines():
line = line.rstrip()
if not line:
continue
flag = line[:2]
if flag not in FLAG_BY_CATEGORY and line.endswith(':'):
platform_name = line[:-1]
platform = {
FINISHED_GAME: [],
NOT_FINISHED_GAME: [],
FINISHED_WATCHED: [],
NOT_FINISHED_WATCHED: [],
}
platforms[platform_name] = platform
continue
if not platform:
continue
category_name = FLAG_BY_CATEGORY.get(flag)
if not category_name:
if not silence:
print(f'Странный формат строки: "{line}"')
continue
category = platform[category_name]
game_name = line[2:]
for game in parse_game_name(game_name):
if game in category:
if not silence:
print(f'Предотвращено добавление дубликата игры "{game}"')
continue
category.append(game)
return platforms
def get_text_from_url() -> str:
url_gist = 'https://gist.github.com/gil9red/2f80a34fb601cd685353'
rs = requests.get(url_gist)
match = re.search('/gil9red/2f80a34fb601cd685353/raw/.+/gistfile1.txt', rs.text)
if not match:
return ''
url = urljoin(rs.url, match.group())
rs = requests.get(url)
return rs.text
def get_played_games() -> Dict[str, Dict[str, List[str]]]:
text = get_text_from_url()
return parse_played_games(text)
if __name__ == '__main__':
def print_text(text, export_to_file_name):
platforms = parse_played_games(text)
print('Platforms:', len(platforms))
total_games = 0
for categories in platforms.values():
for games in categories.values():
total_games += len(games)
print('Games:', total_games)
print()
print(', '.join(platforms.keys()))
print(platforms)
import json
json.dump(platforms, open(export_to_file_name, mode='w', encoding='utf-8'), ensure_ascii=False, indent=4)
def get_text_from_local():
with open('gistfile1.txt', encoding='utf-8') as f:
return f.read()
print('get_text_from_local')
text = get_text_from_local()
print_text(text, 'games_local.json')
print('\n')
print('get_text_from_url')
text = get_text_from_url()
print_text(text, 'games_url.json')
assert parse_played_games(text) == get_played_games()