-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfree_games.py
166 lines (130 loc) · 5.54 KB
/
free_games.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
import re
import aiohttp
from bs4 import BeautifulSoup
async def search_steam_games():
"""
Поиск игр, ставших бесплатными в Steam.
:return: [[Название + Дата выхода,
Дополнение (0,1),
Ссылка,
Описание,
Рейтинг,
До какого момента можно забрать,
Картинка,
Тип магазина
]]
"""
url = 'https://store.steampowered.com/search/?l=russian&maxprice=free&supportedlang=any&specials=1'
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
contents = await resp.text()
soup = BeautifulSoup(contents, 'html.parser')
contents = soup.find_all('a', {'class': 'search_result_row ds_collapse_flag'})
res = []
for game in contents:
async with aiohttp.ClientSession() as session:
async with session.get(game['href'] + '&l=russian') as resp:
game_info = await resp.text()
game_info = BeautifulSoup(game_info, 'html.parser')
try:
date = game_info.find('p', {'class': 'game_purchase_discount_quantity'}).text.split(' ')[0]
except AttributeError:
continue
if game_info.find('div', {'class': 'game_area_bubble game_area_dlc_bubble'}) is not None:
dlc = 1
description = game_info.find('div', {'id': 'game_area_description'}).text[33:1000].strip() + '...'
else:
dlc = 0
description = game_info.find('div', {'id': 'game_area_description'}).text[13:1000].strip() + '...'
try:
rating = (
game.find(
'span', {'class': re.compile('search_review_summary')}
).get('data-tooltip-html').replace('<br>', ': ')
)
except AttributeError:
rating = None
# 'Платформы: ' +
# ', '.join(a['class'][-1] for a in game.find_all('span', {'class': re.compile('platform_img')}))
res += [[
game.find('span', {'class': 'title'}).text + ' (' + # Название
game.find('div', {'class': 'col search_released responsive_secondrow'}).text.strip() + ')', # Дата выхода
dlc, # Дополнение (0,1)?
game['href'], # Ссылка
description, # Описание
rating, # Рейтинг
date, # До какого момента можно забрать
game.find('img').get('srcset').split()[-2], # Картинка
0 # Тип магазина
]]
return res
async def search_epic_games():
"""
Поиск игр, ставших бесплатными в EpicGames.
:return: [[Название + Дата выхода,
Дополнение (0,1),
Ссылка,
Описание,
Рейтинг,
До какого момента можно забрать,
Картинка,
Тип магазина
]]
"""
url = (
'https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions?locale=ru&country=RU&allowCountries=RU'
)
async with aiohttp.ClientSession() as session:
async with session.get(url) as resp:
contents = await resp.json()
contents = contents['data']['Catalog']['searchStore']['elements']
res = []
for game in contents:
if game['title'] == 'Mystery Game' or game['price']['totalPrice']['discountPrice'] != 0:
continue
try:
date = 'Можно забрать до ' + '/'.join(
game['promotions']['promotionalOffers'][0]['promotionalOffers'][0]['endDate'][:10].split('-')[::-1]
) + '.'
except (IndexError, KeyError, TypeError):
continue
try:
url = 'https://store.epicgames.com/ru/p/' + game['catalogNs']['mappings'][0]['pageSlug'], # Ссылка
except (KeyError, IndexError):
url = 'https://store.epicgames.com/ru/p/' + game['productSlug'], # Ссылка
dlc = 0 # if game['offerType'] == 'BASE_GAME' else 1
img = game['keyImages'][0]['url']
res += [[
game['title'], # Название
dlc, # Дополнение?
url[0], # Ссылка
game['description'], # Описание
None, # Рейтинга в эпике нет
date, # Дата
img, # Картинка
1 # Тип магазина
]]
return res
async def search_games():
"""
Поиск игр, ставших бесплатными в Steam и EpicGames.
:return: [[Название + Дата выхода,
Дополнение (0,1),
Ссылка,
Описание,
Рейтинг,
До какого момента можно забрать,
Картинка,
Тип магазина
]]
"""
res = await search_steam_games()
res += await search_epic_games()
return res
if __name__ == '__main__':
import asyncio
loop = asyncio.new_event_loop()
print('EPIC')
print(*loop.run_until_complete(search_epic_games()), sep='\n', end='\n\n')
print('STEAM')
print(*loop.run_until_complete(search_steam_games()), sep='\n', end='\n\n')