Jump to content

How can I get a text from a website to LUA


N3xT

Recommended Posts

6 hours ago, L3KZ!K4 said:

fetchRemote

Due the fact this an website, you'll need to filter the information you want. Of course to get a text remotely you should use an API, to get only the information you need.

 

I don't know a lot about websites so, How I can filter the results? can I get the text without API?.

Link to comment
30 minutes ago, Dealman said:

Well, if it's using XHTML you can use the regular XML functions to parse through it and find what you need. Using PHP should work for you as well but I haven't really touched any PHP yet so I'm rather inexperienced with that.

 

Yes, I'm using XHTML, but my question how can I do it? I know about " regular XML functions ", but how to search for it after connect?

Link to comment

Use fetchRemote to save it as text, you then pass this onto the client to read through it using the XML functions(unless you need to do it on the server). To see how things are structured, right-click your own page and save as.

I was working on a resource before which would fetch internet radios from internet-radio.com and parse through them for easy streaming. Here's a snippet of the code to see how I parsed through it;

 

for nodeIndex, theNode in ipairs(stationNodes) do
	if(xmlNodeGetName(theNode) == "tr") then
		if(nodeIndex <= #stationNodes-1) then
			local stationInfo = xmlNodeGetChildren(theNode);
			for nodeIndex, theNode in ipairs(stationInfo) do	
				if(xmlNodeGetName(theNode) == "td") then
					if(nodeIndex == 1) then -- Parse URL
						local urlNode = xmlFindChild(theNode, "a", 0);
						local onclickAttribute = xmlNodeGetAttribute(urlNode, "onclick")
						local findURL = string.match(onclickAttribute, "http?://[%w-_%.%?%.:/%+=&]+");
						--outputChatBox("Debug URL: ["..tostring(findURL).."]");
						table.insert(g_RadioStations_URL, #g_RadioStations_URL, findURL);
					elseif(nodeIndex == 2) then -- Parse Name
						local nameNode = (xmlFindChild(theNode, "span", 0) or xmlFindChild(theNode, "a", 0));
						if(theName ~= false) then
							local theName = xmlNodeGetValue(nameNode);
							local theNameFix = string.gsub(theName, "[\n+]", "");
							local theNameFix = string.gsub(theName, "&amp;", "");
							--outputChatBox("Debug Name: ["..tostring(theNameFix).."]");
							table.insert(g_RadioStations_Name, #g_RadioStations_Name, theNameFix);
						end
					elseif(nodeIndex == 3) then -- Parse Bitrate
						local bitRateRaw = xmlNodeGetValue(theNode);
						local bitRateFix = string.gsub(bitRateRaw, "[\n+]", "");
						--outputChatBox("Debug Bitrate: ["..tostring(bitRateFix).."]");
						table.insert(g_RadioStations_Bitrate, #g_RadioStations_Bitrate, bitRateFix);
					end
				end
			end
		end
	end
end

 

  • Like 1
Link to comment

Nothing happen.

<body>
	<td class="sourceName">Tn6eL</td>
</body>

addCommandHandler("getSourceName",
function (p)
	local fileName = "sourceCode.xml"
	local xmlFile = xmlLoadFile(fileName)
	if xmlFile then
		local songName = xmlNodeGetChildren(xmlFile);
		for i,node in ipairs(songName) do
			local nameNode = xmlFindChild(node, "sourceName", 0))
			outputChatBox(xmlNodeGetValue(nameNode),p)
        end
		xmlUnloadFile(xmlFile) 
	end
end	)

 

Link to comment

You should read up on XML a bit, you're confusing the child, attribute and value. In this particular scenario;
<td class="sourceName">Tn6e</td>

Red is the child, green is an attribute and blue is the value. So in order to get the value, you'd have to use this code;

addCommandHandler("getSourceName",
function (p)
	local fileName = "sourceCode.xml"
	local xmlFile = xmlLoadFile(fileName)
	if xmlFile then
		local findNode = xmlFindChild(xmlFile, "td", 0)
		if(findNode) then
			outputChatBox(tostring(xmlNodeGetValue(findNode, "class")), p)
		end
		xmlUnloadFile(xmlFile) 
	end
end)

 

Edited by Dealman
Link to comment
1 hour ago, Dealman said:

You should read up on XML a bit, you're confusing the child, attribute and value. In this particular scenario;
<td class="sourceName">Tn6e</td>

Red is the child, green is an attribute and blue is the value. So in order to get the value, you'd have to use this code;


addCommandHandler("getSourceName",
function (p)
	local fileName = "sourceCode.xml"
	local xmlFile = xmlLoadFile(fileName)
	if xmlFile then
		local findNode = xmlFindChild(xmlFile, "td", 0)
		if(findNode) then
			outputChatBox(tostring(xmlNodeGetValue(findNode, "class")), p)
		end
		xmlUnloadFile(xmlFile) 
	end
end)

 

 

Doesn't work, can't find child

Link to comment
4 minutes ago, Dealman said:

It should work if your XML file looks like you showed above;


<body>
	<td class="sourceName">Tn6eL</td>
</body>

If not, the error is elsewhere in your code - use debugscript.

 
 

No errors, my XML file have a lot of lines else that I reply, maybe that is the problem?

Link to comment
9 minutes ago, Dealman said:

Yes, use xmlNodeGetChildren to loop through them. Use xmlNodeGetAttribute to check if the attribute equals "sourceName". If true, use xmlNodeGetValue to get the value.

 
addCommandHandler("getSourceName",
function (p)
	local fileName = "sourceCode.xml"
	local xmlFile = xmlLoadFile(fileName)
	if xmlFile then
		local songName = xmlNodeGetChildren(xmlFile)
		for i,node in ipairs(songName) do
			if ( xmlNodeGetName(node) == "td" ) then
				local nodeName = xmlNodeGetAttribute(node, "sourceName") 
				if nodeName == false then
					outputChatBox(xmlNodeGetValue(nodeName),p)
				end
			end
        end
		xmlUnloadFile(xmlFile) 
	end
end	)

Doesn't work again xd.

Link to comment

Kind of hard to help you when I don't know how the file's structured. You'll just have to do a bit of trial and error, use outputChatBox to debug, see what values you are getting - use that to try and pin-point where in the file you are. Remember that children can have children and so on, XML is like navigating in a spider-web or finding a needle in a haystack sometimes :P

  • Like 1
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...