Raised This Month: $51 Target: $400
 12% 

Socket help


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
LondoN
Senior Member
Join Date: Dec 2015
Location: Roman, Romania.
Old 12-20-2018 , 05:14   Socket help
Reply With Quote #1

Hello, i'm tring to retrive a html page by sockets but i don't know how to parse page to retry a number of votes showed in the page.

www.csservers.ro/evidenta/zm.darkland.ro - this is the page

The sockets retrive me full html who is bigger.


Can someone help pe to retrive onli the votes number in a variable?
__________________
LondoN is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 12-20-2018 , 07:55   Re: Socket help
Reply With Quote #2

you mean to get 'zm_snowbase3 - 29.16%' for example?

edit: in that case maybe using https://forums.alliedmods.net/showthread.php?t=63120 + regex
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 12-20-2018 at 08:00.
JocAnis is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-20-2018 , 07:56   Re: Socket help
Reply With Quote #3

Use the http include file to download the full html page. Provide a sample of the content and we can help you parse the data you want.
__________________
Bugsy is offline
LondoN
Senior Member
Join Date: Dec 2015
Location: Roman, Romania.
Old 12-20-2018 , 09:18   Re: Socket help
Reply With Quote #4

Code:
#include < amxmodx >
#include < amxmisc >
#include < sockets >

#define FILE	"addons/amxmodx/configs/page.txt"

new Socket, Error;

public plugin_init ( )	set_task ( 2.0, "SavePage" );
public SavePage ( )
{
	Socket = socket_open ( "www.csservers.ro", 80, SOCKET_TCP, Error );
	
	if ( Socket > 0 )
	{
		new Query [ 512 ], DataRecived [ 2048 ];
		formatex ( Query, charsmax ( Query ), "GET /evidenta/zm.darkland.ro HTTP/1.1^r^nHost:www.csservers.ro^r^nConnection: close^r^n^r^n" );

		if ( !Error )
		{
			socket_send ( Socket, Query, charsmax ( Query ) );
		
			while ( socket_recv ( Socket, DataRecived, charsmax ( DataRecived ) ) )
			{
				if ( socket_change ( Socket, 1 ) )
				{
					socket_recv ( Socket, DataRecived, charsmax ( DataRecived ) );
					write_file ( FILE, DataRecived );
					server_print ( "[Sockets] Page Saved" );
				}
			}

			socket_close ( Socket );

		}
	}
}
With this i get the page content. This is the page: https://pastebin.com/ntfwxvj2

From this page i need:

Code:
<div class="floating ui red label" title="Voturi azi"><a href="http://www.csservers.ro/evidenta/zm.darkland.ro/voturi-azi">13</a></div>
Only number 13 i need to store in a variable.
__________________
LondoN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-20-2018 , 10:21   Re: Socket help
Reply With Quote #5

You need to give the http server time to respond. Like I said, use the http include.
__________________
Bugsy is offline
E1_531G
Senior Member
Join Date: Dec 2017
Old 12-20-2018 , 11:54   Re: Socket help
Reply With Quote #6

/voturi-azi" can be used as a unique key for a search. ( with containi() ).
From the next char (between > and < ) you will find your desired number.
__________________
My English is A0
E1_531G is offline
JocAnis
Veteran Member
Join Date: Jun 2010
Old 12-20-2018 , 13:41   Re: Socket help
Reply With Quote #7

you can try with this, i had similar topic : https://forums.alliedmods.net/showthread.php?t=311173
Code:
#include <httpx>
#include <regex> 
new Buffer[ 1024 ]

HTTPX_Download( "your site", _, "when_it_finish", "progress" ) //see this at HTTP:X

public when_it_finish() client_print( 0, print_chat, "download page finished" )

public progress( id ) 
{
	new RegeX:res

	while ( ( len = HTTPX_GetData( Buffer, charsmax( Buffer ) ) ) ) 
	{
		new error[50], num 
		
		new Regex:res=regex_match(Buffer, "voturi-azi^">(.*?)</a>", num, error, 49)
	
		if(res)
		{ 
			new wanted_text[300]	// create buffer to store wanted_text 
			regex_substr(res, 1, wanted_text, charsmax(wanted_text)); // retrieve wanted_text 
			server_print("zm vote: %s", wanted_text); // wanted_text: I am wanted 
			regex_free(res); // free the regex handle to prevent memory leak 
		}  
    }
    return PLUGIN_CONTINUE
}
i hope it helps
__________________
KZ Public Autocup - PrimeKZ

My blog: http://primekz.xyz (in progress...) - not active (dec 2022)

Last edited by JocAnis; 12-20-2018 at 13:43.
JocAnis is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-20-2018 , 18:15   Re: Socket help
Reply With Quote #8

PHP Code:
new const szData[] = "<div class=^"floating ui red label^" title=^"Voturi azi^"><a href=^"http://www.csservers.ro/evidenta/zm.darkland.ro/voturi-azi^">13</a></div>";

new const szFindMe[] = "<a href=^"http://www.csservers.ro/evidenta/zm.darkland.ro/voturi-azi^">";
new const szFindMe2[] = "</a></div>";

new 
iPos strfindszData szFindMe );
new 
iPos2 strfindszData szFindMe2 iPos );
new 
szResult];

if ( ( 
iPos > -) && ( iPos2 > -) )
{
    
copycszResult charsmaxszResult ) , szDataiPos + ( sizeofszFindMe ) - ) ] , iPos2 - ( sizeofszFindMe ) - ) );
    
    
server_print"val=[%s]" szResult );

__________________

Last edited by Bugsy; 12-20-2018 at 18:20.
Bugsy is offline
LondoN
Senior Member
Join Date: Dec 2015
Location: Roman, Romania.
Old 12-21-2018 , 11:54   Re: Socket help
Reply With Quote #9

Code:
#include < amxmodx >
#include < amxmisc >
#include < sockets >

#define FILE	"addons/amxmodx/configs/page.txt"

new Socket, Error;

new const FindLine [ ] = "<a href=^"http://www.csservers.ro/evidenta/zm.darkland.ro/voturi-azi^">";
new const FindLine2 [ ] = "</a></div>";

public plugin_init ( )	set_task ( 2.0, "SavePage" );
public SavePage ( )
{
	Socket = socket_open ( "www.csservers.ro", 80, SOCKET_TCP, Error );

	new Pos, Pos2;
	new Result [ 3 ];
	
	if ( Socket > 0 )
	{
		new Query [ 512 ], DataRecived [ 2048 ];
		formatex ( Query, charsmax ( Query ), "GET /evidenta/zm.darkland.ro HTTP/1.1^r^nHost:www.csservers.ro^r^nConnection: close^r^n^r^n" );

		if ( !Error )
		{
			socket_send ( Socket, Query, charsmax ( Query ) );
		
			while ( socket_recv ( Socket, DataRecived, charsmax ( DataRecived ) ) )
			{
				if ( socket_change ( Socket, 1 ) )
				{
					socket_recv ( Socket, DataRecived, charsmax ( DataRecived ) );
					
					Pos = strfind ( DataRecived, FindLine );
					Pos2 = strfind ( DataRecived, FindLine2, Pos );
					
					if ( ( Pos > -1 ) && ( Pos2 > -1 ) )
					{
						copyc ( Result, charsmax ( Result ), DataRecived [ Pos + ( sizeof ( FindLine ) -1 ) ], Pos2 - ( sizeof ( FindLine2 ) -1 ) );
						server_print ( "Votes: %d", str_to_num ( Result ) );
					}
					
				}
			}

			socket_close ( Socket );

		}
	}
}
I've tried to strfiind in DataRecived but no server_print in console..that's not good )
With regex hmm.. i will try this
__________________
LondoN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 12-21-2018 , 17:34   Re: Socket help
Reply With Quote #10

Chances are slim but there is a possibility your data is getting cut-off as you read chunks from the socket into your buffer. This would cause an issue if the break is in the middle of the string you are searching for. You should download the file (using an HTTP include), and then scan the file for the data you want. Or, if this file is not huge, make your buffer bigger and read everything into the buffer, concatenating the data with each socket_read().

To see if this is the case, print the data into the console as it comes in to see if it's all in one piece:
Code:
socket_recv ( Socket, DataRecived, charsmax ( DataRecived ) );
server_print( "Data [%s]" , DataRecived )
__________________

Last edited by Bugsy; 12-21-2018 at 17:36.
Bugsy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 18:19.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode