mixster
2010-02-21 12:08:45
program GE_Price_Downloader;
type
item = record
name: string;
id, minP, curP, maxP: Integer;
end;
var
items: array of item;
c, p, f: Integer;
n, e: TStringArray;
s: string;
// [url]http://itemdb-rs.runescape.com/viewitem.ws?obj=10392[/url]
// [url]http://itemdb-rs.runescape.com/results.ws?query=&sup=Initial[/url] Letter&cat=A
procedure Writelm(input: array of Variant);
var
i: Integer;
s: string;
begin
for i := 0 to High(input) do
s := s + toStr(input[i]);
Writeln(s);
end;
function ExplodeM(str, del: string): TStringArray;
var
i: Integer;
begin
repeat
SetLength(Result, High(Result) + 2);
i := Pos(del, str);
if i < 1 then
Break;
Result[High(Result)] := Copy(str, 1, i - 1);
Delete(str, 1, i + Length(del) - 1);
until False;
Result[High(Result)] := str;
end;
function ConvertPrice(input: string): Integer;
var
f: Extended;
begin
input := Replace(Lowercase(Trim(input)), ',', '', [rfReplaceAll]);
if (Pos('m', input) > 0) then
f := StrToFloatDef(Copy(input, 1, Pos('m', input) - 1), 0) * 1000000
else if (Pos('k', input) > 0) then
f := StrToFloatDef(Copy(input, 1, Pos('k', input) - 1), 0) * 1000
else
f := StrToFloatDef(Input, 0);
Result := Round(f);
end;
begin
SetLength(n, 27);
for c := 0 to 25 do
n[c] := Chr(Ord('A') + c);
n[26] := 'Other';
for c := 0 to High(n) do
begin
s := GetPage('http://services.runescape.com/m=itemdb_rs/results.ws?query=&sup=Initial Letter&sub=All&cvis=1&vis=0&sortby=name&order=1&cat=' + n[c]);
for p := 1 to StrToIntDef(Between('page=', '&', Copy(s, Pos('Next >', s), 200)), 1) do
begin
s := GetPage('http://services.runescape.com/m=itemdb_rs/results.ws?query=&sup=Initial Letter&sub=All&cvis=1&vis=0&sortby=name&order=1&cat=' + n[c] + '&page=' + IntToStr(p));
Delete(s, 1, Pos('tbody>' + #10 + '<tr', s) + 10);
e := ExplodeM(s, '</tr>' + #10 + '<tr');
e[High(e)] := Copy(e[High(e)], 1, Pos('</tr>', e[High(e)]));
Writelm([n[c], ':', p]);
end;
end;
SetLength(items, High(e) + 1);
for c := 0 to High(e) do
begin
with items[c] do
begin
name := Between('alt="', '"', e[c]);
id := StrToIntDef(Between('obj=', '"', e[c]), -1);
if (id < 0) then
begin
Writelm([c, ': ', name, ' :: ', id, ' :: ', minP, '; ', curP, '; ', maxP, ';']);
Continue;
end;
s := GetPage('http://services.runescape.com/m=itemdb_rs/' + Replace(name, ' ', '_', [rfReplaceAll]) + 'viewitem.ws?obj=' + IntToStr(id));
minP := ConvertPrice(Between('<b>Minimum price:</b> ', #10 + '</span>', s));
curP := ConvertPrice(Between('<b>Market price:</b> ', #10 + '</span>', s));
maxP := ConvertPrice(Between('<b>Maximum price:</b> ', #10 + '</span>', s));
Writelm([c, ': ', name, ' :: ', id, ' :: ', minP, '; ', curP, '; ', maxP, ';']);
end;
end;
f := RewriteFile(AppPath + 'ge_db.csv', False);
if (f = -1) then
begin
Writeln('Error making a file to save the output to, so you basically wasted all this time!');
TerminateScript;
end;
for c := 0 to High(items) do
if not (WriteFileString(f, IntToStr(items[c].id) + ',"' + items[c].name + '",' + IntToStr(items[c].minP) + ',' + IntToStr(items[c].curP) + ',' + IntToStr(items[c].maxP) + #10 + #13)) then
begin
Writeln('Error writing line regarding the following item:');
Writeln(IntToStr(items[c].id) + ',' + items[c].name + ',' + IntToStr(items[c].minP) + ',' + IntToStr(items[c].curP) + ',' + IntToStr(items[c].maxP));
end;
CloseFile(f);
end.Ran it with just 'Other' items and it went fine - currently doing it for everything. When finished, I'll attach a csv of all items.
Edit: Attached the complete list - there's a few items with 0 for prices as my nets failed and page loading did not work. Zip'd as too big as csv/txt file to upload.