-
Notifications
You must be signed in to change notification settings - Fork 19
/
G4U.lua
183 lines (162 loc) · 6.96 KB
/
G4U.lua
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
--1.02
local function checkVersion(str, comparison)
local serverversion = str:sub(3, 6)
return serverversion == comparison
end
local scriptsfolder = client.GetScriptsPath()
local updtheaders = {
["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
local function extractGameName(url)
-- Attempt to find the game name in the pattern "/%d+%-([%a%d%-]+)%-"
local start_index, end_index, game_name = url:find("/%d+%-([%a%d%-]+)%-")
if game_name and game_name ~= "" then
return game_name
else
-- If the URL doesn't match the first pattern, try to extract the game name directly
start_index, end_index, game_name = url:find("/([%a%d%-]+)/?$") -- Also consider a possible trailing slash
if game_name and game_name ~= "" then
return game_name
else
return nil
end
end
end
local version = "1.02"
local githubversion = http.get("https://raw.githubusercontent.com/Y0URD34TH/Project-GLD/main/Scripts/G4U.lua", updtheaders)
local outdated = false
if checkVersion(githubversion, version) then
outdated = false
else
outdated = true
Notifications.push_warning("Script Outdated", "This script is outdated. Please update it!")
end
function extractDomain(url)
local pattern = "^[^:]+://([^/]+)"
local domain = string.match(url, pattern)
return domain
end
local function filterLinksByGameName(links, gameName)
local filteredLinks = {}
for _, link in ipairs(links) do
-- Check if the link contains the game name string
if string.find(link, gameName, 1, true) then
table.insert(filteredLinks, link)
end
end
return filteredLinks
end
local function filterLinks(links)
local filteredLinks = {}
for _, link in ipairs(links) do
-- Check if the link starts with "/en/" followed by a number
if string.match(link, "^/en/%d") then
table.insert(filteredLinks, link)
end
end
return filteredLinks
end
-- Function to filter, complete, and remove duplicates from links
local function filterCompleteAndRemoveDuplicates(links)
local uniqueLinks = {}
local filteredLinks = {}
for _, link in ipairs(links) do
-- Check if the link ends with "/nzb"
local endsWithNzb = string.match(link, "/nzb$")
-- Check if the link contains "ddownload," "katfile," or "steam"
local containsDdownload = string.match(link, "ddownload")
local containsKatfile = string.match(link, "katfile")
local containsSteam = string.match(link, "steam")
-- Complete incomplete links with "https://g4u.to/"
if not link:match("^https://") then
link = "https://g4u.to" .. link
end
-- Add the link to the filtered list if it meets any of the conditions and is not a duplicate
if endsWithNzb or containsSteam then
if not uniqueLinks[link] then
uniqueLinks[link] = true
table.insert(filteredLinks, link)
end
end
end
return filteredLinks
end
local function webScrapeg4u(gameName)
local searchUrl = "https://g4u.to/en/search/?str=" .. gameName
searchUrl = searchUrl:gsub(" ", "+")
local gamenamemod = gameName
gamenamemod = gamenamemod:gsub(" ", "-")
gamenamemod = gamenamemod:gsub(":", "")
gamenamemod = gamenamemod:gsub("'", "")
gamenamemod = string.lower(gamenamemod)
local headers = {
["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36"
}
local responseBody = http.get(searchUrl, headers)
local gameLinks = {}
local gameResultsL = filterLinksByGameName(filterLinks(HtmlWrapper.findAttribute(responseBody, "a", "", "", "href")), gamenamemod)
local gameResults = {}
for _, link in ipairs(gameResultsL) do
table.insert(gameLinks, "https://g4u.to" .. link)
end
for i = 1, #gameLinks do
local gameResponseBody = http.get(gameLinks[i], headers)
if gameResponseBody then
local gameResult = {
name = extractGameName(gameLinks[i]),
links = {},
ScriptName = "g4u"
}
local linksDL = HtmlWrapper.findAttribute(gameResponseBody, "a", "class", "w3-button w3-block w3-orange w3-text-white w3-hover-green w3-padding-small", "href")
local linksDL2 = filterCompleteAndRemoveDuplicates(HtmlWrapper.findAttribute(gameResponseBody, "a", "target", "_blank", "href"))
table.insert(gameResult.links, { name = "View Game Page", link = gameLinks[i], addtodownloadlist = false })
for _, serverLink in ipairs(linksDL) do
-- Insert into gameResult.links
local serverName = "freediscussions"
table.insert(gameResult.links, { name = serverName, link = "https://g4u.to" .. serverLink, addtodownloadlist = false })
end
for _, serverLink2 in ipairs(linksDL2) do
-- Insert into gameResult.links
local serverName = extractDomain(serverLink2)
if string.match(serverName, "g4u") then
table.insert(gameResult.links, { name = "nzb", link = serverLink2, addtodownloadlist = true })
else
table.insert(gameResult.links, { name = serverName, link = serverLink2, addtodownloadlist = false })
end
end
table.insert(gameResults, gameResult)
else
end
end
return gameResults
end
local version = client.GetVersionDouble()
if version < 2.14 then
Notifications.push_error("Lua Script", "Program is outdated. Please update the app to use this script!")
if outdated then
menu.add_button("Update G4U")
local function updatebutton()
Download.DirectDownload("https://raw.githubusercontent.com/Y0URD34TH/Project-GLD/main/Scripts/G4U.lua", scriptsfolder .. "G4U.lua")
client.unload_script("G4U.lua")
client.load_script("G4U.lua")
end
client.add_callback("on_button_Update G4U", updatebutton)
end
else
Notifications.push_success("Lua Script", "G4U script is loaded and working!")
if outdated then
menu.add_button("Update G4U")
local function updatebutton()
Download.DirectDownload("https://raw.githubusercontent.com/Y0URD34TH/Project-GLD/main/Scripts/G4U.lua", scriptsfolder .. "G4U.lua")
client.unload_script("G4U.lua")
client.load_script("G4U.lua")
end
client.add_callback("on_button_Update G4U", updatebutton)
end
local function g4u()
local gamename = game.getgamename()
local results = webScrapeg4u(gamename)
communication.receiveSearchResults(results)
end
client.add_callback("on_scriptselected", g4u)
end