Log in

View Full Version : Reading off a web page:



Mister Snow
07-24-2012, 04:44 AM
I would like to have the bot access something like a CSV on a webpage, read the strings, and use as vars in a script.

The page could be any type, and the only data there could be the vars.

Possible to do this? If so, how? Any chance someone can maybe link me to a guide on exploding CSV as well?

Any help is appreciated, thank you

Brandon
07-24-2012, 04:59 AM
At least give an example page. It's hard to imagine what you want here as it isn't clear to me how you want to use this data as vars :S

Silent
07-24-2012, 05:19 AM
I would like to have the bot access something like a CSV on a webpage, read the strings, and use as vars in a script.

The page could be any type, and the only data there could be the vars.

Possible to do this? If so, how? Any chance someone can maybe link me to a guide on exploding CSV as well?

Any help is appreciated, thank you

Have a look at this:

http://docs.villavu.com/simba/scriptref/web.html

Frement
07-24-2012, 07:08 AM
Wrote this for you, hope it helps:
program CSVSnippet;

type TCSVObject = record
Header: TStringArray;
Items: Array of TStringArray;
end;

const Delimeter = ';';
LF = #10;

function CSVParse(Data: String; WithHeaders: Boolean): TCSVObject;
var ExplodedData, ExplodedSubData, HeaderData: TStringArray;
I, J, K: Integer;
begin
ExplodedData := Explode(LF, Data);
if (WithHeaders) then begin
HeaderData := Explode(Delimeter, ExplodedData[0]);
for I := 0 to High(HeaderData) do begin
HeaderData[I] := Replace(HeaderData[I], '"', '', [rfReplaceAll]);
end;
Result.Header := HeaderData;
K := 1;
end else begin
K := 0;
end;
SetLength(Result.Items, High(ExplodedData));
for I := K to High(ExplodedData) do begin
ExplodedSubData := Explode(Delimeter, ExplodedData[I]);
for J := 0 to High(ExplodedSubData) do begin
ExplodedSubData[J] := Replace(ExplodedSubData[J], '"', '', [rfReplaceAll]);
end;
Result.Items[I - 1] := ExplodedSubData;
end;
end;

function CSVGetHeaderIndex(CSVObject: TCSVObject; Needle: String): Integer;
var I: Integer;
begin
for I := 0 to High(CSVObject.Header) do begin
if (CSVObject.Header[I] = Needle) then begin
Result := I;
Exit;
end;
end;
end;

function CSVFind(CSVObject: TCSVObject; Needle: String): Integer;
var I, J: Integer;
begin
for I := 0 to High(CSVObject.Items) do begin
for J := 0 to High(CSVObject.Items[I]) do begin
if (CSVObject.Items[I][J] = Needle) then
Result := I;
Exit;
end;
end;
end;

function CSVGet(CSVObject: TCSVObject; Header: String): TStringArray;
var I, Index: Integer;
begin
Index := CSVGetHeaderIndex(CSVObject, Header);
SetLength(Result, High(CSVObject.Items) + 1);
for I := 0 to High(CSVObject.Items) do begin
Result[I] := CSVObject.Items[I][Index];
end;
end;

var HTTPClient: Integer;
Data: String;
CSVObject: TCSVObject;

begin
// Initialize our HTTP client.
HTTPClient := InitializeHTTPClient(False);

// Get the CSV file as string.
Data := GetHTTPPage(HTTPClient, 'http://static.frement.net/srl/example.csv');

// Pass the retrieved CSV data to our function.
// (Note: the example has headers, so WithHeaders has to be True,
// else it will treat headers as actual item)
CSVObject := CSVParse(Data, True);

// Output the whole object.
Writeln(ToStr(CSVObject));

// Find a specific item with any value in the item. (Note: will return the first found)
Writeln(ToStr(CSVObject.Items[CSVFind(CSVObject, '00001')]));

// Get all values from the object by header.
Writeln(ToStr(CSVGet(CSVObject, 'Addresse')));

// Free our HTTP client.
FreeHTTPClient(HTTPClient);
end.

Mister Snow
07-24-2012, 03:40 PM
At least give an example page. It's hard to imagine what you want here as it isn't clear to me how you want to use this data as vars :S

Hey, sorry I was not clear enough... it was kind of meant that way. There simply is no page yet. I would make the page custom, so the sky is the limit there. The page could be in whatever way to make it easiest and most compatible with Simba.




Wrote this for you, hope it helps:
program CSVSnippet;

type TCSVObject = record
Header: TStringArray;
Items: Array of TStringArray;
end;

const Delimeter = ';';
LF = #10;

function CSVParse(Data: String; WithHeaders: Boolean): TCSVObject;
var ExplodedData, ExplodedSubData, HeaderData: TStringArray;
I, J, K: Integer;
begin
ExplodedData := Explode(LF, Data);
if (WithHeaders) then begin
HeaderData := Explode(Delimeter, ExplodedData[0]);
for I := 0 to High(HeaderData) do begin
HeaderData[I] := Replace(HeaderData[I], '"', '', [rfReplaceAll]);
end;
Result.Header := HeaderData;
K := 1;
end else begin
K := 0;
end;
SetLength(Result.Items, High(ExplodedData));
for I := K to High(ExplodedData) do begin
ExplodedSubData := Explode(Delimeter, ExplodedData[I]);
for J := 0 to High(ExplodedSubData) do begin
ExplodedSubData[J] := Replace(ExplodedSubData[J], '"', '', [rfReplaceAll]);
end;
Result.Items[I - 1] := ExplodedSubData;
end;
end;

function CSVGetHeaderIndex(CSVObject: TCSVObject; Needle: String): Integer;
var I: Integer;
begin
for I := 0 to High(CSVObject.Header) do begin
if (CSVObject.Header[I] = Needle) then begin
Result := I;
Exit;
end;
end;
end;

function CSVFind(CSVObject: TCSVObject; Needle: String): Integer;
var I, J: Integer;
begin
for I := 0 to High(CSVObject.Items) do begin
for J := 0 to High(CSVObject.Items[I]) do begin
if (CSVObject.Items[I][J] = Needle) then
Result := I;
Exit;
end;
end;
end;

function CSVGet(CSVObject: TCSVObject; Header: String): TStringArray;
var I, Index: Integer;
begin
Index := CSVGetHeaderIndex(CSVObject, Header);
SetLength(Result, High(CSVObject.Items) + 1);
for I := 0 to High(CSVObject.Items) do begin
Result[I] := CSVObject.Items[I][Index];
end;
end;

var HTTPClient: Integer;
Data: String;
CSVObject: TCSVObject;

begin
// Initialize our HTTP client.
HTTPClient := InitializeHTTPClient(False);

// Get the CSV file as string.
Data := GetHTTPPage(HTTPClient, 'http://static.frement.net/srl/example.csv');

// Pass the retrieved CSV data to our function.
// (Note: the example has headers, so WithHeaders has to be True,
// else it will treat headers as actual item)
CSVObject := CSVParse(Data, True);

// Output the whole object.
Writeln(ToStr(CSVObject));

// Find a specific item with any value in the item. (Note: will return the first found)
Writeln(ToStr(CSVObject.Items[CSVFind(CSVObject, '00001')]));

// Get all values from the object by header.
Writeln(ToStr(CSVGet(CSVObject, 'Addresse')));

// Free our HTTP client.
FreeHTTPClient(HTTPClient);
end.

Awesome! Thank you so much +rep.

Looks to my eyes like it just might work. Time to mess around with it.

Kyle Undefined
07-24-2012, 04:11 PM
Why use a CSV? They can get corrupted easily, use a database :)

activecamo
07-25-2012, 12:27 PM
Get cygwin if you are on windows, or install a Linux VM.
http://www.cygwin.com/

If you are on linux or a mac do this in a terminal.

-Learn basic awk and regex's (Regular Expressions)

-Use lynx dump to get the page and then use awk to parse the code.



#!/usr/bin/bash

lynx -dump -width 200 http://www.villavu.com/ | sed 's/\[[0-9]*\]//g' | awk "/has been released/"'{print}'



That code will fetch the friendly message of what version of Simba has been released.

If you want to loop it so that it it fetches automaticaly use a loop like this...



while true
do

lynx -dump -width 200 http://www.villavu.com/ | awk "/has been released/"'{print}'

sleep 15; clear; done


If you need regex help let me know. These tools are limitless and far cleaner than Pascal for what i think you are trying to do.

Bam Bam
07-25-2012, 03:55 PM
Get cygwin if you are on windows, or install a Linux VM.
http://www.cygwin.com/

If you are on linux or a mac do this in a terminal.

-Learn basic awk and regex's (Regular Expressions)

-Use lynx dump to get the page and then use awk to parse the code.



That code will fetch the friendly message of what version of Simba has been released.

If you want to loop it so that it it fetches automaticaly use a loop like this...



If you need regex help let me know. These tools are limitless and far cleaner than Pascal for what i think you are trying to do.

I'm a big fan of linux and open source myself, but this hardly seems to be the simplest solution available.

activecamo
07-25-2012, 07:20 PM
I'm a big fan of linux and open source myself, but this hardly seems to be the simplest solution available.

Give me a database and I will sed and awk the hell out of it for you. Simplicity could also based on the familiarity of the language. Since I actually work mostly in BASH its very easy for me.

Frement
07-25-2012, 08:43 PM
Give me a database and I will sed and awk the hell out of it for you. Simplicity could also based on the familiarity of the language. Since I actually work mostly in BASH its very easy for me.

Given that a user must request help on this matter suggests that he/she might not want that solution, it is not the best nor the easiest way to do it. And as we basically work around Simba, it's highly unlikely that using other languages/methods would be acceptable.

While there are other solutions, I'm sure he'll be fine with what he has now.

But don't take this the wrong way, helping is always appreciated :)