-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
149 lines (122 loc) · 5.04 KB
/
main.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
import datetime
import subprocess
import time
import webbrowser
import PySimpleGUI as sg
from pip._vendor import requests
# ありがとう
# https://qiita.com/dario_okazaki/items/656de21cab5c81cabe59
# https://note.com/hideharu092/n/n9dc1c1075a7b
sg.theme('SystemDefault1')
API = URL = RTM = ""
try:
with open('settings', mode='r') as f:
API = f.readline()
URL = f.readline()
RTM = f.readline()
except FileNotFoundError:
pass
# レイアウトの定義
layout = [
[sg.Text('Please Input Steam Data', key="output")],
[sg.Text('API Key', size=(15, 1)), sg.InputText(API.strip(), key="API"), sg.Button(button_text='Start')],
[sg.Text('Custom URL', size=(15, 1)), sg.InputText(URL.strip(), key="URL"), sg.Button(button_text='Get API Key')],
[sg.Text('Required Time (m)', size=(15, 1)), sg.InputText(RTM.strip(),size=(5,1), key="RTM")],
[sg.Text('https://steamcommunity.com/id/XXXXX(Custom URL)')]
]
# ウィンドウの生成
window = sg.Window('SteamSaleInTheLibrary', layout)
# イベントループ
while True:
event, values = window.read()
if event is None:
print('exit')
break
if event == 'Start':
apikey = values["API"]
rtm = values["RTM"]
if not rtm.isdecimal():
rtm = 0
else:
rtm = int(rtm)
with open('settings', mode='w') as f:
f.write(apikey + "\n")
f.write(values["URL"] + "\n")
f.write(str(rtm))
# SteamID の取得(Vanity URL か数字の ID か判定)
if not values["URL"].isdecimal():
idurl = (
"http://api.steampowered.com/ISteamUser/ResolveVanityURL/v0001/"
f"?key={apikey}&vanityurl={values['URL']}"
)
response = requests.get(idurl)
json_data = response.json()
steamID = json_data["response"]["steamid"]
else:
steamID = values["URL"]
# ユーザの所有ゲーム一覧を取得
ownedurl = (
"http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/"
f"?key={apikey}&steamid={steamID}&format=json"
)
response = requests.get(ownedurl)
json_data = response.json()
# 指定プレイ時間以上のゲームの appid を抽出
appList = []
for g in json_data["response"]["games"]:
if g["playtime_forever"] >= rtm:
appList.append(g["appid"])
today = datetime.datetime.today()
filename = (
"SteamSaleInTheLibrary-"
+ today.strftime("%Y%m%d%H%M%S")
)
appLength = len(appList)
itemCount = 0
with open(filename, mode='x', encoding='UTF-8') as f:
for appid in appList:
itemCount += 1
print(f"in process... {itemCount} / {appLength}")
window["output"].update(f"in process... {itemCount} / {appLength}")
try:
# store API による価格・割引情報の取得
# 例: https://store.steampowered.com/api/appdetails?appids=1091500&cc=jp&l=japanese
store_url = (
"https://store.steampowered.com/api/appdetails"
f"?appids={appid}&cc=jp&l=japanese"
)
store_res = requests.get(store_url)
store_json = store_res.json()
if str(appid) not in store_json:
print(f"Invalid response for appid={appid}")
continue
app_info = store_json[str(appid)]
if not app_info.get('success'):
print(f"Failed to get data for appid={appid}")
continue
data = app_info.get('data', {})
appName = data.get('name', '')
# 価格情報
price_info = data.get('price_overview')
if not price_info:
continue
discount = price_info.get('discount_percent', 0)
initial_price = price_info.get('initial_formatted', '')
final_price = price_info.get('final_formatted', '')
if discount > 0:
f.write(
f"{appName} : [-{discount}%] "
f"{initial_price} -> {final_price}\n"
)
print(f"{appName} : [-{discount}%] {initial_price} -> {final_price}")
except Exception as e:
print(f"ERROR - appid={appid} ex:{e}")
continue
# 処理終了後にファイルをメモ帳で開く
subprocess.Popen(['notepad.exe', filename])
print(f"Done! {filename}")
window["output"].update(f"Done! {filename}")
if event == 'Get API Key':
webbrowser.open("https://steamcommunity.com/dev/apikey")
# ウィンドウの破棄と終了
window.close()