-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteam_treemap.py
82 lines (71 loc) · 3.23 KB
/
steam_treemap.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
import re
import sys
import json
import requests
import operator
from bs4 import BeautifulSoup
from sys import exit
playerId = input("Steam id (留空则会尝试读取本目录下的content.html): ")
thresholdInput = input("生成的游戏数量 (默认100): ")
randomOrderInput = input("随机排列? (y/n,默认n): ")
print("\n读取游戏时长...")
if(playerId == ""):
try:
with open('content.html', 'r', encoding='utf-8') as myfile:
page = myfile.read()
except FileNotFoundError:
print("错误:没有在本目录下找到content.html,请将游戏时长页面另存为成content.html并放在本目录")
x = input("按任意键退出")
exit()
else:
if playerId.isnumeric():
url = "http://steamcommunity.com/profiles/" + playerId
else:
url = "http://steamcommunity.com/id/" + playerId
cookies = {'birthtime': '283993201', 'mature_content': '1'}
try:
page = requests.get(url + "/games/?tab=all", cookies=cookies).text
except Exception:
print("访问错误:请尝试关闭反代工具后使用全局魔法,或者手动访问将游戏时长页面,另存为成content.html并放在本目录")
x = input("按任意键退出")
exit()
threshold = 100
if thresholdInput.isnumeric():
threshold = int(thresholdInput)
sortMethod = ".sort((a, b) => Math.random()-0.5)" if randomOrderInput == "y" else ""
try:
want = json.loads(re.search(r"var rgGames = (.*);", page).group(1))
except Exception:
print("读取页面错误:请确认游戏时长为公开")
x = input("按任意键退出")
exit()
gameHourList = {}
gameImageList = {}
gameNameList = {}
count = 0
for eachJson in want:
count = count + 1
if count > threshold:
break
appId = eachJson['appid']
if 'hours_forever' not in eachJson:
continue
hours = float(eachJson['hours_forever'].replace(",", ""))
gameName = eachJson['name']
gameHourList[appId] = hours
gameNameList[appId] = gameName
gameImageList[appId] = "https://steamcdn-a.akamaihd.net/steam/apps/{}/header.jpg".format(appId)
print("\n生成页面...")
content = "<div id='viz'></div><script src='js/d3plus.full.min.js'></script><script>var data = ["
for appId, hours in gameHourList.items():
newline = "{ name: '" + gameNameList[appId].replace("'", "\\'") + "', color: 'black', id: '" + str(appId) + "', value: " + str(hours) + ", image: '" + gameImageList[appId] + "' },"
content += newline
content = content[:-1]
content += " ];new d3plus.Treemap().data(data).groupBy('name','id').duration(1000).shapeConfig({fill: function (d) {return d.color;},backgroundImage: function (d) {return d.image;},label: false,activeOpacity:1,hoverOpacity:0.8}).select('#viz').sum('value').on('click', function(d) {window.open('https://store.steampowered.com/app/' + d.id,'_blank');}).tooltipConfig({title: function(d) {return d['name'];},tbody: [['小时数', function(d) { return d['value'] }]]})" + sortMethod + ".render();</script>"
# .tile(d3.treemapSquarify.ratio(2.13))
filepath = playerId + "_treemap_output.html"
output_file = open(filepath, "w", encoding='utf-8')
output_file.write(content)
output_file.close()
print("\n文件已保存为" + filepath)
x = input("按任意键退出")