AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Socket help (https://forums.alliedmods.net/showthread.php?t=312909)

LondoN 12-20-2018 05:14

Socket help
 
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?

JocAnis 12-20-2018 07:55

Re: Socket help
 
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

Bugsy 12-20-2018 07:56

Re: Socket help
 
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.

LondoN 12-20-2018 09:18

Re: Socket help
 
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.

Bugsy 12-20-2018 10:21

Re: Socket help
 
You need to give the http server time to respond. Like I said, use the http include.

E1_531G 12-20-2018 11:54

Re: Socket help
 
/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.

JocAnis 12-20-2018 13:41

Re: Socket help
 
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

Bugsy 12-20-2018 18:15

Re: Socket help
 
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 );



LondoN 12-21-2018 11:54

Re: Socket help
 
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

Bugsy 12-21-2018 17:34

Re: Socket help
 
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 )


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

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