-
Notifications
You must be signed in to change notification settings - Fork 0
/
appID_finder.py
83 lines (66 loc) · 2.99 KB
/
appID_finder.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
import os
import sqlite3
from curl_cffi import requests
def get_steam_data(output_dir='assets'):
os.makedirs(output_dir, exist_ok=True)
db_file = os.path.join(output_dir, 'steam_data.db')
conn = sqlite3.connect(db_file)
cursor = conn.cursor()
cursor.execute('''CREATE TABLE IF NOT EXISTS apps (appid INTEGER PRIMARY KEY, name TEXT)''')
cursor.execute('SELECT COUNT(*) FROM apps')
if cursor.fetchone()[0] == 0:
api = "https://api.steampowered.com/ISteamApps/GetAppList/v0002/"
response = requests.get(api, timeout=30)
app_list = response.json()['applist']['apps']
cursor.execute('BEGIN TRANSACTION')
for app in app_list:
cursor.execute('''INSERT OR IGNORE INTO apps (appid, name) VALUES (?, ?)''', (app['appid'], app['name']))
conn.commit()
return conn
def get_steam_app_by_name(app_name):
conn = get_steam_data()
try:
cursor = conn.cursor()
cursor.execute('''SELECT appid, name FROM apps WHERE LOWER(name) = LOWER(?)''', (app_name,))
result = cursor.fetchone()
if result:
return {'appid': result[0], 'name': result[1]}
# If no match, searching
try:
search_url = f"https://steamcommunity.com/actions/SearchApps/{app_name}"
response = requests.get(search_url, timeout=30)
search_results = response.json()
for result in search_results:
if result['name'].lower() == app_name.lower():
cursor.execute('''INSERT OR IGNORE INTO apps (appid, name) VALUES (?, ?)''', (result['appid'], result['name']))
conn.commit()
return {'appid': result['appid'], 'name': result['name']}
except Exception as e:
print(f"Search error: {e}")
return None
finally:
conn.close()
def get_steam_app_by_id(appid):
conn = get_steam_data()
try:
cursor = conn.cursor()
cursor.execute('SELECT name FROM apps WHERE appid = ?', (int(appid),))
result = cursor.fetchone()
if result:
return {'appid': int(appid), 'name': result[0]}
# If not found, try Steam store
try:
store_url = f"https://store.steampowered.com/api/appdetails?appids={appid}"
response = requests.get(store_url, timeout=30)
store_data = response.json()
if str(appid) in store_data and store_data[str(appid)]['success']:
app_details = store_data[str(appid)]['data']
name = app_details.get('name', 'Unknown')
cursor.execute('''INSERT OR IGNORE INTO apps (appid, name) VALUES (?, ?)''', (int(appid), name))
conn.commit()
return {'appid': int(appid), 'name': name}
except Exception as e:
print(f"Search error: {e}")
return None
finally:
conn.close()