forked from PathOfBuildingCommunity/PathOfBuilding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuildSiteTools.lua
More file actions
132 lines (125 loc) · 5.96 KB
/
Copy pathBuildSiteTools.lua
File metadata and controls
132 lines (125 loc) · 5.96 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
-- Path of Building
--
-- Module: Build Site Tools
-- Functions used to import and export PoB build codes from external websites
--
buildSites = { }
-- Import/Export websites list used in dropdowns
-- label: What a user sees in the export dropdown and when the import box recognizes the website
-- id: Protocol handler id used to load builds with the pob: URL scheme e.g. pob://Maxroll/siteSpecificBuildId
-- matchURL: A pattern to match URLs belonging to this website to show a valid url message in ImportTab
-- regexURL: Copied link from website to extract the build ID and pass to downloadURL to get the raw build XML
-- downloadURL: The URL to download the raw build code
-- codeOut: Gets prepended to returned code from postUrl. Needed to enable export in ImportTab
-- postUrl: The URL to upload a build code. Needed to enable export in ImportTab
-- postFields: The POST fields prepended to base64-encoded XML. Needed to enable export in ImportTab
-- linkURL: The URL pattern to link to the provided build code (Unused currently)
buildSites.websiteList = {
{
label = "Maxroll", id = "Maxroll", matchURL = "^https://maxroll%.gg/poe/pob/.*", regexURL = "maxroll%.gg/poe/pob/(.+)%s*$", downloadURL = "maxroll%.gg/poe/api/pob/%1",
codeOut = "https://maxroll.gg/poe/pob/", postUrl = "https://maxroll.gg/poe/api/pob", postFields = "pobCode=", linkURL = "maxroll%.gg/poe/pob/%1"
},
{
label = "pobb.in", id = "POBBin", matchURL = "^https://pobb%.in/.+", regexURL = "pobb%.in/(.+)%s*$", downloadURL = "pobb.in/pob/%1",
codeOut = "https://pobb.in/", postUrl = "https://pobb.in/pob/", postFields = "", linkURL = "pobb.in/%1"
},
{
label = "PoeNinja", id = "PoeNinja", matchURL = "^https://poe%.ninja/?p?o?e?1?/pob/%w+", regexURL = "poe%.ninja/?p?o?e?1?/pob/(%w+)%s*$", downloadURL = "poe.ninja/poe1/pob/raw/%1",
codeOut = "", postUrl = "https://poe.ninja/poe1/pob/api/upload", postFields = "code=", linkURL="poe.ninja/poe1/pob/%1"
},
{
label = "Pastebin.com", id = "pastebin", matchURL = "^https://pastebin%.com/%w+", regexURL = "pastebin%.com/(%w+)%s*$", downloadURL = "pastebin.com/raw/%1", linkURL = "pastebin.com/%1"
},
{ label = "PastebinP.com", id = "pastebinProxy", matchURL = "^https://pastebinp%.com/%w+", regexURL = "pastebinp%.com/(%w+)%s*$", downloadURL = "pastebinp.com/raw/%1", linkURL = "pastebin.com/%1" },
{ label = "Rentry.co", id = "rentry", matchURL = "^https://rentry%.co/%w+", regexURL = "rentry%.co/(%w+)%s*$", downloadURL = "rentry.co/paste/%1/raw", linkURL = "rentry.co/%1" },
{ label = "poedb.tw", id = "PoEDB", matchURL = "^https://poedb%.tw/.+", regexURL = "poedb%.tw/pob/(.+)%s*$", downloadURL = "poedb.tw/pob/%1/raw", codeOut = "", postUrl = "https://poedb.tw/pob/api/gen", postFields = "", linkURL = "poedb.tw/pob/%1" },
}
--- Uploads a PoB build code to a website
--- @param websiteInfo table Contains the postUrl, any postParams, and a prefix to add to the response
--- @param buildCode string The build code that will be uploaded
function buildSites.UploadBuild(buildCode, websiteInfo)
local response
if websiteInfo then
response = LaunchSubScript([[
local code, connectionProtocol, proxyURL = ...
local curl = require("lcurl.safe")
local page = ""
local easy = curl.easy()
easy:setopt_url(']]..websiteInfo.postUrl..[[')
easy:setopt(curl.OPT_POST, true)
easy:setopt(curl.OPT_USERAGENT, "Path of Building/]]..launch.versionNumber..[[")
easy:setopt(curl.OPT_POSTFIELDS, ']]..websiteInfo.postFields..[['..code)
easy:setopt(curl.OPT_ACCEPT_ENCODING, "")
easy:setopt(curl.OPT_FOLLOWLOCATION, 1)
if connectionProtocol then
easy:setopt(curl.OPT_IPRESOLVE, connectionProtocol)
end
if proxyURL then
easy:setopt(curl.OPT_PROXY, proxyURL)
end
easy:setopt_writefunction(function(data)
page = page..data
return true
end)
easy:perform()
local res = easy:getinfo_response_code()
easy:close()
if (res == 200) then
return page
else
return nil, page
end
]], "", "", buildCode, launch.connectionProtocol, launch.proxyURL)
end
return response
end
--- Downloads a PoB build code from a website
--- @param link string A link to the site that contains the link to the raw build code
--- @param websiteInfo table? Contains the downloadUrl
--- @param callback function The function to call when the download is complete
function buildSites.DownloadBuild(link, websiteInfo, callback)
local siteCodeURL
-- Only called on program start via protocol handler
if not websiteInfo then
-- Inline build code: pob://code/<base64url-build-code> — no HTTP fetch.
-- "code" is a reserved id; do not add a websiteList entry with id = "code".
local inlineCode = link:match("^pob:[/\\]*code[/\\]+([%w_%-]+=?=?)$")
if inlineCode then
callback(true, inlineCode, link)
return
end
for _, siteInfo in ipairs(buildSites.websiteList) do
if link:match("^pob:[/\\]*" .. siteInfo.id:lower() .. "[/\\]+(.+)") then
siteCodeURL = link:gsub("^pob:[/\\]*" .. siteInfo.id:lower() .. "[/\\]+(.+)", "https://" .. siteInfo.downloadURL)
websiteInfo = siteInfo
break
end
end
else -- called via the ImportTab
siteCodeURL = link:gsub(websiteInfo.regexURL, websiteInfo.downloadURL)
end
if websiteInfo then
launch:DownloadPage(siteCodeURL, function(response, errMsg)
if errMsg then
callback(false, errMsg, siteCodeURL)
else
callback(true, response.body, siteCodeURL)
end
end)
else
callback(false, "Download information not found", siteCodeURL)
end
end
-- Parses and converts URI's to import links. Currently only supports protocol handler URI's, extend as needed.
-- @param uri string Example: pob://pobbin/<id> or pob://poeninja/<id>
function buildSites.ParseImportLinkFromURI(uri)
local importLink = nil
-- Check if it's an URI from protocol handler
for _, siteInfo in ipairs(buildSites.websiteList) do
if uri:match("^pob:[/\\]*" .. siteInfo.id:lower() .. "[/\\]+(.+)") then
importLink = uri:gsub("^pob:[/\\]*" .. siteInfo.id:lower() .. "[/\\]+(.+)", "https://" .. siteInfo.linkURL)
break
end
end
return importLink
end