-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSFloat-Auto-Trade.py
More file actions
332 lines (281 loc) · 16.4 KB
/
CSFloat-Auto-Trade.py
File metadata and controls
332 lines (281 loc) · 16.4 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import asyncio
import json
import aiohttp
from pathlib import Path
from aiosteampy import SteamClient, AppContext
from aiosteampy.utils import get_jsonable_cookies
from aiosteampy.helpers import restore_from_cookies
from aiosteampy.mixins.guard import SteamGuardMixin # Импортируем SteamGuard для подтверждения трейдов
from aiosteampy.mixins.web_api import SteamWebApiMixin # Импортируем WebApiMixin для работы с Web API
# Продолжительность ожидания между проверками (в минутах)
CHECK_INTERVAL_MINUTES = 5
# Constants for API endpoints
API_USER_INFO = "https://csfloat.com/api/v1/me"
API_TRADES = "https://csfloat.com/api/v1/me/trades?state=queued,pending&limit=500"
API_ACCEPT_TRADE = "https://csfloat.com/api/v1/trades/{trade_id}/accept" # Define the accept trade endpoint
# Path to a file to save cookies, will be created at end of a script run if do not exist
COOKIE_FILE = Path("cookies.json")
USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0"
# Path to store processed trade IDs
PROCESSED_TRADES_FILE = Path("processed_trades.json")
def load_steam_config(config_path='steam.json'):
with open(config_path, 'r') as file:
return json.load(file)
def load_processed_trades():
if PROCESSED_TRADES_FILE.is_file():
with PROCESSED_TRADES_FILE.open("r") as f:
try:
return set(json.load(f)) # trade_id остаются строками
except json.JSONDecodeError:
return set()
return set()
def save_processed_trades(processed_trades):
with PROCESSED_TRADES_FILE.open("w") as f:
json.dump(list(processed_trades), f, indent=2)
async def get_user_info(session, csfloat_api_key):
headers = {'Authorization': csfloat_api_key}
try:
async with session.get(API_USER_INFO, headers=headers) as response:
response.raise_for_status()
return await response.json()
except aiohttp.ClientResponseError as http_err:
print(f"HTTP error occurred while fetching user info: {http_err}")
except Exception as err:
print(f"Other error occurred while fetching user info: {err}")
return None
async def get_trades(session, csfloat_api_key):
headers = {'Authorization': csfloat_api_key}
try:
async with session.get(API_TRADES, headers=headers) as response:
response.raise_for_status()
trades_data = await response.json()
return trades_data
except aiohttp.ClientResponseError as http_err:
print(f"HTTP error occurred while fetching trades: {http_err}")
except Exception as err:
print(f"Other error occurred while fetching trades: {err}")
return None
async def accept_trade(session, csfloat_api_key, trade_id, trade_token):
url = API_ACCEPT_TRADE.format(trade_id=trade_id)
headers = {
'Authorization': csfloat_api_key,
'Content-Type': 'application/json'
}
payload = {
'trade_token': trade_token # Передача trade_token в тело запроса, если требуется API
}
try:
async with session.post(url, headers=headers, json=payload) as response:
if response.status != 200:
# Логирование подробностей ошибки
error_detail = await response.text()
print(f"Failed to accept trade {trade_id}. Status: {response.status}, Detail: {error_detail}")
return False
result = await response.json()
return True
except aiohttp.ClientResponseError as http_err:
print(f"HTTP error occurred while accepting trade {trade_id}: {http_err}")
except Exception as err:
print(f"Other error occurred while accepting trade {trade_id}: {err}")
return False
async def send_steam_trade(client: SteamClient, trade_id, buyer_steam_id=None, trade_url=None, asset_id=None, trade_token=None):
try:
# Определение контекста игры, например, CS2
game_context = AppContext.CS2 # Убедитесь, что это правильный контекст для вашего предмета
# Получение вашего инвентаря
my_inv, _, _ = await client.get_inventory(game_context)
# Проверка структуры предметов в инвентаре
if not my_inv:
print("Ваш инвентарь пуст или не удалось его загрузить.")
return False
# Попытка найти предмет по asset_id
try:
asset_id_int = int(asset_id)
item_to_give = next((item for item in my_inv if item.asset_id == asset_id_int), None)
except ValueError:
item_to_give = next((item for item in my_inv if item.asset_id == asset_id), None)
if not item_to_give:
print(f"Предмет с asset_id {asset_id} не найден в инвентаре.")
return False
# Вызов make_trade_offer с использованием Steam ID или Trade URL
if trade_url:
# Отправка через трейд-ссылку
offer_id = await client.make_trade_offer(
trade_url, # Трейд-ссылка как первый позиционный аргумент
to_give=[item_to_give],
to_receive=[],
message=""
)
elif buyer_steam_id:
# Отправка через Steam ID партнёра
if trade_token:
offer_id = await client.make_trade_offer(
buyer_steam_id, # Steam ID партнёра как первый позиционный аргумент
to_give=[item_to_give],
to_receive=[],
message="",
token=trade_token # Передача trade_token, если требуется
)
else:
offer_id = await client.make_trade_offer(
buyer_steam_id, # Steam ID партнёра как первый позиционный аргумент
to_give=[item_to_give],
to_receive=[],
message=""
)
else:
print("Необходимо указать либо buyer_steam_id, либо trade_url.")
return False
if offer_id:
print(f"Торговое предложение {trade_id} отправлено!")
return offer_id # Возвращаем offer_id для дальнейшей обработки
else:
print("Не удалось отправить торговое предложение.")
return False
except aiohttp.ClientResponseError as http_err:
print(f"HTTP error occurred while sending trade offer: {http_err}")
except Exception as e:
print(f"An error occurred while sending trade offer: {e}")
return False
# Функция подтверждения трейдов, если требуется
async def confirm_trade(client: SteamGuardMixin):
try:
confirmations = await client.get_confirmations()
if not confirmations:
print("No pending confirmations.")
return
for confirmation in confirmations:
confirmation_key, timestamp = await client._gen_confirmation_key(tag="conf")
# Подтверждение трейда
result = await client.confirm_confirmation(confirmation, confirmation_key, timestamp)
if result:
print(f"Successfully confirmed trade offer {confirmation.offer_id}")
else:
print(f"Failed to confirm trade offer {confirmation.offer_id}")
except Exception as e:
print(f"An error occurred while confirming trades: {e}")
async def check_actionable_trades(session, csfloat_api_key, client: SteamGuardMixin, shared_secret, identity_secret, processed_trades, check_interval_minutes):
user_info = await get_user_info(session, csfloat_api_key)
if user_info and user_info.get('actionable_trades', 0) > 0:
print("Actionable trades found, fetching trade details...")
trades_info = await get_trades(session, csfloat_api_key)
if isinstance(trades_info, dict):
trades_list = trades_info.get('trades', [])
if isinstance(trades_list, list):
for trade in trades_list:
if isinstance(trade, dict):
trade_id = trade.get('id')
# Проверка, был ли уже обработан этот trade_id
if str(trade_id) in processed_trades:
print(f"Trade {trade_id} has already been processed. Skipping.")
continue # Пропустить уже обработанные трейды
seller_id = trade.get('seller_id') # ID отправителя
buyer_id = trade.get('buyer_id') # ID получателя
asset_id = trade.get('contract', {}).get('item', {}).get('asset_id')
trade_token = trade.get('trade_token') # Получаем trade_token
trade_url = trade.get('trade_url') # Получаем trade_url
accepted_at = trade.get('accepted_at') # Получаем время принятия, если есть
trade_state = trade.get('state') # Получаем состояние трейда
if trade_state == "verified":
# Если трейд уже подтвержден, добавляем его в обработанные и пропускаем
processed_trades.add(str(trade_id))
continue
if trade_id and seller_id and buyer_id and asset_id:
if accepted_at:
# Предложение уже принято, отправляем торговое предложение
print(f"Trade {trade_id} уже принято. Переходим к отправке торгового предложения.")
offer_id = await send_steam_trade(
client,
trade_id=str(trade_id), # Передаём trade_id как строку
buyer_steam_id=int(buyer_id), # Передаём buyer_id как целое число
asset_id=int(asset_id),
trade_token=trade_token,
trade_url=trade_url
)
if offer_id:
# Автоматически подтверждаем трейды
await confirm_trade(client)
else:
print(f"Failed to send trade for {trade_id}")
else:
# Предложение ещё не принято, принимаем его
print(f"Accepting trade {trade_id}...")
accept_result = await accept_trade(session, csfloat_api_key, trade_id=str(trade_id), trade_token=trade_token)
if accept_result:
print(f"Sending item to buyer for trade {trade_id}...")
offer_id = await send_steam_trade(
client,
trade_id=str(trade_id), # Передаём trade_id как строку
buyer_steam_id=int(buyer_id), # Передаём buyer_id как целое число
asset_id=int(asset_id),
trade_token=trade_token,
trade_url=trade_url
)
if offer_id:
# Автоматически подтверждаем трейды
await confirm_trade(client)
else:
print(f"Failed to send trade for {trade_id}")
else:
print(f"Failed to accept trade {trade_id}")
# Добавляем trade_id в processed_trades независимо от результата
processed_trades.add(str(trade_id))
else:
print(f"Unexpected trades list format: {type(trades_list)}")
else:
print(f"Unexpected trades data format: {type(trades_info)}")
else:
print(f"No actionable trades at the moment. Waiting for {check_interval_minutes} minutes before next check.")
async def main():
config = load_steam_config() # Загрузка конфигурации
csfloat_api_key = config['csfloat_api_key']
steam_api_key = config['steam_api_key']
steam_id = int(config['steam_id64']) # Убедитесь, что это целое число
steam_login = config['steam_login']
steam_password = config['steam_password']
shared_secret = config['shared_secret']
identity_secret = config['identity_secret']
# Определение продолжительности ожидания (в минутах)
CHECK_INTERVAL_MINUTES = 5 # Вы можете легко изменить это значение
# Инициализация SteamClient с необходимыми аргументами
class MySteamClient(SteamClient, SteamWebApiMixin, SteamGuardMixin):
pass
client = MySteamClient(
steam_id=steam_id, # Steam ID64 как целое число
username=steam_login,
password=steam_password,
shared_secret=shared_secret,
identity_secret=identity_secret,
api_key=steam_api_key, # Передача API ключа
user_agent=USER_AGENT,
)
# Восстановление cookies, если они существуют
if COOKIE_FILE.is_file():
with COOKIE_FILE.open("r") as f:
cookies = json.load(f)
await restore_from_cookies(cookies, client)
else:
await client.login()
# Загрузка обработанных трейдов
processed_trades = load_processed_trades()
async with aiohttp.ClientSession() as session:
try:
while True:
await check_actionable_trades(
session,
csfloat_api_key,
client,
shared_secret,
identity_secret,
processed_trades, # Передача набора обработанных трейдов
CHECK_INTERVAL_MINUTES # Передача продолжительности ожидания
)
save_processed_trades(processed_trades) # Сохранение после каждой проверки
await asyncio.sleep(CHECK_INTERVAL_MINUTES * 60) # Ожидание заданное количество минут
finally:
# Сохранение cookies
with COOKIE_FILE.open("w") as f:
json.dump(get_jsonable_cookies(client.session), f, indent=2)
await client.session.close()
if __name__ == "__main__":
asyncio.run(main())