SRL Archive - SRL Snippets

GetPrice [RS07][Lape]


CynicRus

2013-10-19 17:47:20

Hey:) I present to you my function for getting the price of RS 07 items. This function get info from zybez.net json api by my json library for lape:)

Working only in Lape:


program rs07price;
{$i json.simba}
const

API_URL = 'http://forums.zybez.net/runescape-2007-prices/api/';

function GetFormattedUrl(const ItemName: string):string;
begin
result:=Replace(ItemName,#32,'+',[rfReplaceAll, rfIgnoreCase]);
end;

function GetPriceJson(const ItemName: string): string;
var
Page: string;
begin
Page:=GetPage(API_URL+GetFormattedURL(ItemName));
result:=page;
end;

function GetPrice(const ItemName: string): integer;
var
json,key,value: string;
temp,Prices,Average: TStringArray;
i,j: integer;
begin
Result:=-1;
json:=GetPriceJson(ItemName);

if JSONDataIsList(json) then
begin
JSON2TSA(json, prices);
for i := 0 to Length(Prices) - 1 do
begin
Json2TSA(Prices[i],temp);
for j:=0 to Length(temp) -1 do
begin
if JSONDataIsKeyValuePair(temp[j]) then
begin
key:=JsonGetKey(temp[j]);
value:=JSONGetValue(temp[j]);
if DeQuote(key) = 'average' then
begin
result:=Trunc(StrToFloat(DeQuote(value)));
break;
end;
end;
end;
end;
end;
end;

begin
cleardebug;
WriteLn(IntToStr(GetPrice('dragon chainbody')));
WriteLn(IntToStr(GetPrice('blahbla')));
WriteLn(IntToStr(GetPrice('Mithril arrow')));
end.

Output:

7494343
-1
35
Successfully executed.

json.simba:(cannot make this file as attachment from Linux, idk why:()



//json helper library by Cynic

function CopyStop(const Str: string; const start: Integer; const Stop: Integer): string;
begin
Result := Copy(Str, Start, Stop - Start + 1);
end;

function DynArrayAppend(var V: TStringArray; const R: String): Integer;
begin
Result := Length(V);
SetLength(V, Result + 1);
V[Result] := R;
end;

function JSONEscapeDecode(const JSONData: string): string;
var
ptr: integer = 1;
moji_hex: string;
moji: string = '';
begin
Result := '';
repeat
while (ptr <= length(JSONData)) do
begin
if ptr > length(JSONData) then
Exit;
case JSONData[ptr] of
'\':
begin
if ptr >= length(JSONData) then
Exit;
inc(ptr);
case JSONData[ptr] of
'u':
begin
Inc(ptr);
Moji_Hex := '';
Moji_Hex := Uppercase(Copy(JSONData, ptr, 4));
Moji := '';
Moji := ('$' + Moji_Hex);
Result := Result + Moji;
Inc(ptr, 3);
end;
else
Result := Result + JSONData[ptr];
end;
end;
else
Result := Result + JSONData[ptr];
end;
Inc(ptr);
end;
until ptr >= length(JSONData);
end;

function JSONDataIsList(const JSONData: string): Boolean;
begin
Result := false;
if Length(Trim(JSONData)) = 0 then
exit;
if Trim(JSONData)[1] in ['{', '[', '('] then
Result := True;
end;

function JSONDataIsObject(const JSONData: string): Boolean;
begin
Result := false;
if Length(Trim(JSONData)) = 0 then
exit;
if Trim(JSONData)[1] in ['{'] then
Result := True;
end;

function JSONDataIsArray(const JSONData: string): Boolean;
begin
Result := false;
if Length(Trim(JSONData)) = 0 then
exit;
if Trim(JSONData)[1] in ['['] then
Result := True;
end;

function JSONDataIsNumber(const JSONData: string): Boolean;
var
IndexPOS: Integer;
begin
Result := false;
if Length(Trim(JSONData)) = 0 then
exit;
Result := True;
for IndexPos := 1 to Length(JSONData) do
if not (JSONData[IndexPos] in [' ', '.', 'e', 'E', '-', '+', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9']) then
begin
Result := False;
Exit;
end;
end;

function JSONDataIsString(const JSONData: string): Boolean;
begin
Result := false;
if Length(Trim(JSONData)) = 0 then
exit;
if Trim(JSONData)[1] in ['"'] then
Result := True;
end;

function JSONDataIsKeyValuePair(const JSONData: string): Boolean;
var
InputPos: Integer = 0;
SeparCount: Integer = 0;
InQuote: Boolean = False;
BraceLevel: Integer = 0;
begin
Result := false;
if Length(Trim(JSONData)) = 0 then
exit;
while (InputPOS < length(JSONData)) do
begin
Inc(InputPos);
case JSONData[InputPos] of
'"':
begin
if InputPos = 1 then
InQuote := not InQuote
else if JSONData[InputPos - 1] <> '\' then
InQuote := not InQuote;
end;
'{': Inc(BraceLevel);
'}': Dec(BraceLevel);
end;
if (not InQuote) and (BraceLevel = 0) then
if JSONData[InputPos] = ':' then
inc(SeparCount);
end;
Result := (SeparCount = 1);
end;

function JSON2TSA(const JSONString: AnsiString; var Decoded: TStringArray): Integer;
const
ListItemSep = ',';

var
Pos: Integer;
NestCount: Integer = 0;
CopyPos: Integer = 1;
TmpStr: string;
InQuote: Boolean = False;
begin
SetLength(Decoded,0);
{ if not JSONDataIsList(JSONString) then
begin
Result := - 1;
Exit;
end; }
for Pos := 1 to Length(JSONString) do
begin
case JSONString[Pos] of
'"':
begin
if pos = 1 then
InQuote := not InQuote
else if JSONString[Pos - 1] <> '\' then
InQuote := not InQuote;
end;
'(', '[', '{':
if not InQuote then
Inc(NestCount);
')', ']', '}':
if not InQuote then
begin
Dec(NestCount);
if NestCount = 0 then
begin
TmpStr := Trim(CopyStop(JSONString, CopyPos + 1, Pos - 1));
// WriteLn(TMPStr);
DynArrayAppend(Decoded,tmpstr);
CopyPos := Pos;
end;
end;
ListItemSep:
if not InQuote then
begin
if NestCount = 1 then
begin
TmpStr := Trim(CopyStop(JSONString, CopyPos + 1, Pos - 1));
// WriteLn(TMPStr);
DynArrayAppend(Decoded,tmpstr);
CopyPos := Pos;
end;
end;
end;
end;
Result := 0;
end;

function JSONGetValueByKey(const JSONList: TStringArray; const Key: string): string;
var
i: integer;
_Key: string;
Len: integer;
DelimiterPos: Integer;
begin
Result := '';
Len := Length(JSONList);
for i := 0 to Len - 1 do
begin
DelimiterPos := Pos(':', JSONList[i]);
if DelimiterPOS > 1 then
begin
_key := copy(JSONList[i], 1, DelimiterPOS - 1);
if _key = key then
begin
Result := CopyStop(JSONList[i], DelimiterPOS + 1, Length(JSONList[i]));
Exit;
end;
end;
end;
end;

function JSONTreeGet(const JSONString: string; const JSONPath: string): string;
var
InputPOS: Integer;
SimpleKeyStartPos: Integer;
ThisKey: string;
JSONList: TSTringArray;
begin
SetLength(JSONList, 1);
if Length(JSONPath) = 0 then
exit;
InputPOS := 0;
SimpleKeyStartPos := 0;
Result := JSONString;
repeat
Inc(InputPOS);
SimpleKeyStartPos := InputPos;
while (InputPOS < length(JSONPath)) and (JSONPath[InputPos] <> ':') do
Inc(InputPos);
if JSONPath[InputPos] <> ':' then
ThisKey := CopyStop(JSONPath, SimpleKeyStartPos, InputPos)
else
ThisKey := CopyStop(JSONPath, SimpleKeyStartPos, InputPos - 1);
if Length(ThisKey) = 0 then
Exit;
JSON2TSA(Result, JSONList);
Result := JSONGetValueByKey(JSONList, ThisKey);
until InputPOS >= Length(JSONPath);
end;

procedure JSONTreeGetMultiple(const JSONString: string; const JSONPath: string; var ValueList: TStringArray);
var
InputPOS: Integer;
SimpleKeyStartPos: Integer;
ThisKey: string;
JSONList: TStringArray;
tmpStr: string;
Found: Boolean;
begin
SetLEngth(JsonList, 1);
if Length(JSONPath) = 0 then
exit;
repeat
Found := false;
InputPOS := 0;
SimpleKeyStartPos := 0;
tmpStr := JSONString;
repeat
Inc(InputPOS);
SimpleKeyStartPos := InputPos;
while (InputPOS < length(JSONPath)) and (JSONPath[InputPos] <> ':') do
Inc(InputPos);
if JSONPath[InputPos] <> ':' then
ThisKey := CopyStop(JSONPath, SimpleKeyStartPos, InputPos)
else
ThisKey := CopyStop(JSONPath, SimpleKeyStartPos, InputPos - 1);
if Length(ThisKey) = 0 then
Exit;
JSON2TSA(tmpStr, JSONList);
tmpStr := JSONGetValueByKey(JSONList, ThisKey);
if (InputPOS = Length(JSONPath)) and (tmpstr <> '') then
begin
DynArrayAppend(ValueList,TmpStr);
tmpStr := '';
Found := True;
end;
until InputPOS >= Length(JSONPath);
until not Found;
end;

function JSONGetKey(const JSONData: string): string;
var
InputPos: integer = 0;
begin
if Length(JSONData) = 0 then
exit;
if JSONData[1] in ['{', '[', '('] then
exit;
while (InputPOS < length(JSONData)) and (JSONData[InputPos] <> ':') do
Inc(InputPos);
case JSONData[InputPos] of
':': Result := Trim(CopyStop(JSONData, 1, InputPos - 1))
else
Result := '';
end;
end;

function JSONGetValue(const JSONData: string): string;
var
InputPos: integer = 0;
begin
Result := 'not set';
if Length(JSONData) = 0 then
exit;
if JSONData[1] in ['{', '[', '('] then
exit;
while (InputPOS < length(JSONData)) and (JSONData[InputPos] <> ':') do
Inc(InputPos);
case JSONData[InputPos] of
':': Result := Trim(CopyStop(JSONData, InputPos + 1, Length(JSONData)))
else
Result := 'notfound';
end;
end;

function DeQuote(const input: string): string;
var
p: integer;
begin
result := input;
p := pos('"', result);
if p > 0 then
result := copy(result, p + 1, length(result) - p);
p := pos('"', result);
if p > 0 then
result := copy(result, 1, p - 1);
end;

function DeBracket(const input: string): string;
var
p: integer;
begin
result := input;
p := pos('[', result);
if p > 0 then
result := copy(result, p + 1, length(result) - p);
p := pos(']', result);
if p > 0 then
result := copy(result, 1, p - 1);
end;

function DeBrace(const input: string): string;
var
p: integer;
begin
result := input;
p := pos('{', result);
if p > 0 then
result := copy(result, p + 1, length(result) - p);
p := pos('}', result);
if p > 0 then
result := copy(result, 1, p - 1);
end;

function DeParen(const input: string): string;
var
p: integer;
begin
result := input;
p := pos('(', result);
if p > 0 then
result := copy(result, p + 1, length(result) - p);
p := pos(')', result);
if p > 0 then
result := copy(result, 1, p - 1);
end;


How to use:
- Place json.simba into your simba include directory
- Run script:)

Cheers,
Cynic.

Le Jingle

2013-10-19 18:40:11

I like the effort, as always, Cynic :>

Cheers,
Lj