Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit uPastebinCom;
- (*
- TPastebinCom class (Based on Pastebin APIs)
- Date : 13.03.2014
- Author: HEX0x29A
- Notes :
- *Requere Delphi XE+
- *)
- interface
- {$M+}
- uses
- Windows, IdHttp, HTTPApp, Classes, SysUtils, RegularExpressions;
- const
- USER_AGENT = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.13) Gecko/20080401';
- PRIVACY_PUBLIC = 0;
- PRIVACY_UNLISTED = 1;
- PRIVAСY_PRIVATE = 2;
- EXPIRY_10MINUTES = '10M';
- EXPIRY_1HOUR = '1H';
- EXPIRY_1DAY = '1D';
- EXPIRY_1WEEK = '1W';
- EXPIRY_2WEEKS = '2W';
- EXPIRY_1MONTH = '1M';
- EXPIRY_NEVER = 'N';
- LANGUAGES: packed array [0..218] of string =
- ('4cs'{4CS}, '6502acme'{6502 ACME Cross Assembler}, '6502kickass'{6502 Kick Assembler}, '6502tasm'{6502 TASM/64TASS},
- 'abap'{ABAP}, 'actionscript'{ActionScript}, 'actionscript3'{ActionScript 3}, 'ada'{Ada}, 'algol68'{ALGOL 68},
- 'apache'{Apache Log}, 'applescript'{AppleScript}, 'apt_sources'{APT Sources}, 'arm'{ARM}, 'asm'{ASM (NASM)},
- 'asp'{ASP}, 'asymptote'{Asymptote}, 'autoconf'{autoconf}, 'autohotkey'{Autohotkey}, 'autoit'{AutoIt},
- 'avisynth'{Avisynth}, 'awk'{Awk}, 'bascomavr'{BASCOM AVR}, 'bash'{Bash}, 'basic4gl'{Basic4GL}, 'bibtex'{BibTeX},
- 'blitzbasic'{Blitz Basic}, 'bnf'{BNF}, 'boo'{BOO}, 'bf'{BrainFuck}, 'c'{C}, 'c_mac'{C for Macs},
- 'cil'{C Intermediate Language}, 'csharp'{C#}, 'cpp'{C++}, 'cpp-qt'{C++ (with QT extensions)},
- 'c_loadrunner'{C: Loadrunner}, 'caddcl'{CAD DCL}, 'cadlisp'{CAD Lisp}, 'cfdg'{CFDG}, 'chaiscript'{ChaiScript},
- 'clojure'{Clojure}, 'klonec'{Clone C}, 'klonecpp'{Clone C++}, 'cmake'{CMake}, 'cobol'{COBOL},
- 'coffeescript'{CoffeeScript}, 'cfm'{ColdFusion}, 'css'{CSS}, 'cuesheet'{Cuesheet}, 'd'{D}, 'dcl'{DCL},
- 'dcpu16'{DCPU-16}, 'dcs'{DCS}, 'delphi'{Delphi}, 'oxygene'{Delphi Prism (Oxygene)}, 'diff'{Diff}, 'div'{DIV},
- 'dos'{DOS}, 'dot'{DOT}, 'e'{E}, 'ecmascript'{ECMAScript}, 'eiffel'{Eiffel}, 'email'{Email}, 'epc'{EPC},
- 'erlang'{Erlang}, 'fsharp'{F#}, 'falcon'{Falcon}, 'fo'{FO Language}, 'f1'{Formula One}, 'fortran'{Fortran},
- 'freebasic'{FreeBasic}, 'freeswitch'{FreeSWITCH}, 'gambas'{GAMBAS}, 'gml'{Game Maker}, 'gdb'{GDB}, 'genero'{Genero},
- 'genie'{Genie}, 'gettext'{GetText}, 'go'{Go}, 'groovy'{Groovy}, 'gwbasic'{GwBasic}, 'haskell'{Haskell}, 'haxe'{Haxe},
- 'hicest'{HicEst}, 'hq9plus'{HQ9 Plus}, 'html4strict'{HTML}, 'html5'{HTML 5}, 'icon'{Icon}, 'idl'{IDL},
- 'ini'{INI file}, 'inno'{Inno Script}, 'intercal'{INTERCAL}, 'io'{IO}, 'j'{J}, 'java'{Java}, 'java5'{Java 5},
- 'javascript'{JavaScript}, 'jquery'{jQuery}, 'kixtart'{KiXtart}, 'latex'{Latex}, 'ldif'{LDIF}, 'lb'{Liberty BASIC},
- 'lsl2'{Linden Scripting}, 'lisp'{Lisp}, 'llvm'{LLVM}, 'locobasic'{Loco Basic}, 'logtalk'{Logtalk},
- 'lolcode'{LOL Code}, 'lotusformulas'{Lotus Formulas}, 'lotusscript'{Lotus Script}, 'lscript'{LScript}, 'lua'{Lua},
- 'm68k'{M68000 Assembler}, 'magiksf'{MagikSF}, 'make'{Make}, 'mapbasic'{MapBasic}, 'matlab'{MatLab}, 'mirc'{mIRC},
- 'mmix'{MIX Assembler}, 'modula2'{Modula 2}, 'modula3'{Modula 3}, '68000devpac'{Motorola 68000 HiSoft Dev}, 'mpasm'{MPASM},
- 'mxml'{MXML}, 'mysql'{MySQL}, 'nagios'{Nagios}, 'newlisp'{newLISP}, 'text'{None}, 'nsis'{NullSoft Installer},
- 'oberon2'{Oberon 2}, 'objeck'{Objeck Programming Langua}, 'objc'{Objective C}, 'ocaml-brief'{OCalm Brief}, 'ocaml'{OCaml},
- 'octave'{Octave}, 'pf'{OpenBSD PACKET FILTER}, 'glsl'{OpenGL Shading}, 'oobas'{Openoffice BASIC}, 'oracle11'{Oracle 11},
- 'oracle8'{Oracle 8}, 'oz'{Oz}, 'parasail'{ParaSail}, 'parigp'{PARI/GP}, 'pascal'{Pascal}, 'pawn'{PAWN}, 'pcre'{PCRE},
- 'per'{Per}, 'perl'{Perl}, 'perl6'{Perl 6}, 'php'{PHP}, 'php-brief'{PHP Brief}, 'pic16'{Pic 16}, 'pike'{Pike},
- 'pixelbender'{Pixel Bender}, 'plsql'{PL/SQL}, 'postgresql'{PostgreSQL}, 'povray'{POV-Ray}, 'powershell'{Power Shell},
- 'powerbuilder'{PowerBuilder}, 'proftpd'{ProFTPd}, 'progress'{Progress}, 'prolog'{Prolog}, 'properties'{Properties},
- 'providex'{ProvideX}, 'purebasic'{PureBasic}, 'pycon'{PyCon}, 'python'{Python}, 'pys60'{Python for S60}, 'q'{q/kdb+},
- 'qbasic'{QBasic}, 'rsplus'{R}, 'rails'{Rails}, 'rebol'{REBOL}, 'reg'{REG}, 'rexx'{Rexx}, 'robots'{Robots},
- 'rpmspec'{RPM Spec}, 'ruby'{Ruby}, 'gnuplot'{Ruby Gnuplot}, 'sas'{SAS}, 'scala'{Scala}, 'scheme'{Scheme}, 'scilab'{Scilab},
- 'sdlbasic'{SdlBasic}, 'smalltalk'{Smalltalk}, 'smarty'{Smarty}, 'spark'{SPARK}, 'sparql'{SPARQL}, 'sql'{SQL},
- 'stonescript'{StoneScript}, 'systemverilog'{SystemVerilog}, 'tsql'{T-SQL}, 'tcl'{TCL}, 'teraterm'{Tera Term},
- 'thinbasic'{thinBasic}, 'typoscript'{TypoScript}, 'unicon'{Unicon}, 'uscript'{UnrealScript}, 'ups'{UPC}, 'urbi'{Urbi},
- 'vala'{Vala}, 'vbnet'{VB.NET}, 'vedit'{Vedit}, 'verilog'{VeriLog}, 'vhdl'{VHDL}, 'vim'{VIM}, 'visualprolog'{Visual Pro Log},
- 'vb'{VisualBasic}, 'visualfoxpro'{VisualFoxPro}, 'whitespace'{WhiteSpace}, 'whois'{WHOIS}, 'winbatch'{Winbatch},
- 'xbasic'{XBasic}, 'xml'{XML}, 'xorg_conf'{Xorg Config}, 'xpp'{XPP}, 'yaml'{YAML}, 'z80'{Z80 Assembler}, 'zxbasic'{ZXBasic});
- type
- TUserInfo = packed record
- Name : string;
- FormatShort: string;
- Expiry : string;
- Avatar : string;
- Privacy : integer;
- WebSite : string;
- Email : string;
- Location : string;
- ProUser : Boolean;
- end;
- TPaste = packed record
- Key : string;
- Date : TDateTime;
- Title : string;
- Size : integer;
- Expiry : TDateTime;
- Privacy : integer;
- FormatLong : string;
- FormatShort: string;
- Url : string;
- Hits : integer;
- end;
- TPasteList = packed array of TPaste;
- TPastebinCom = class
- private
- bAutoUserKey: Boolean;
- DevApiKey : string;
- LocUserKey : string;
- Http : TIdHttp;
- Params : TStringList;
- function GetUserAgent: string;
- function GetProxyHost: string;
- function GetProxyPort: integer;
- function GetApiKey: string;
- procedure SetUserAgent(const Value: string);
- procedure SetProxyHost(const Value: string);
- procedure SetProxyPort(const Value: integer);
- procedure SetApiKey(const Value: string);
- procedure XMLToPasteList(const XML : string;
- var PasteList: TPasteList);
- public
- constructor Create(const szApiKey: string = '');
- destructor Destroy; override;
- function Login(var UserKey : string;
- const Name : string;
- const Password: string): Boolean;
- function GetPasteList(var PasteList: TPasteList;
- const UserKey : string = '';
- const Limit : integer = 50 {1..1000}): Boolean;
- function GetTrendingPasteList(var PasteList: TPasteList): Boolean;
- function SendPaste(var Response: string;
- const Content : string;
- const Title : string = '';
- const Language: string = '';
- const UserKey : string = '';
- const Expiry : string = EXPIRY_NEVER;
- const Privacy : dword = PRIVACY_PUBLIC): Boolean;
- function DeletePaste(const PasteKey: string;
- const UserKey : string = ''): Boolean;
- function GetPasteRAW(const PasteKey: string): string;
- function GetUserInfo(var UserInfo: TUserInfo;
- const UserKey : string = ''): Boolean;
- published
- property ApiKey : string read GetApiKey write SetApiKey;
- property UserAgent : string read GetUserAgent write SetUserAgent;
- property ProxyHost : string read GetProxyHost write SetProxyHost;
- property ProxyPort : integer read GetProxyPort write SetProxyPort;
- property AutoUserKey: Boolean read bAutoUserKey write bAutoUserKey;
- end;
- function UnixDateTimeToDelphiDateTime(UnixDateTime: longint): TDateTime;
- function DelphiDateTimeToUnixDateTime(DateTime: TDateTime): longint;
- implementation
- function DelphiDateTimeToUnixDateTime(DateTime: TDateTime): longint;
- const
- SecsPerDay = 24 * 60 * 60;
- begin
- Result := Trunc((DateTime - EncodeDate(1970, 1, 1)) * SecsPerDay);
- end;
- function UnixDateTimeToDelphiDateTime(UnixDateTime: longint): TDateTime;
- const
- SecsPerDay = 24 * 60 * 60;
- begin
- Result := EncodeDate(1970, 1, 1) + (UnixDateTime / SecsPerDay);
- end;
- { TPastebinCom }
- constructor TPastebinCom.Create(const szApiKey: string = '');
- begin
- Http := TIdHttp.Create(nil);
- Params := TStringList.Create;
- DevApiKey := szApiKey;
- LocUserKey := '';
- bAutoUserKey := True;
- Http.Request.UserAgent := USER_AGENT;
- end;
- function TPastebinCom.DeletePaste(const PasteKey: string;
- const UserKey : string = ''): Boolean;
- const
- BAD_REQUEST = 'Bad API request';
- var
- buffer: string;
- begin
- with Params do
- begin
- Clear;
- Add('api_option=' + 'delete');
- Add('api_paste_key=' + PasteKey);
- if (UserKey = '')and(bAutoUserKey) then
- Add('api_user_key=' + LocUserKey)
- else
- Add('api_user_key=' + UserKey);
- Add('api_dev_key=' + DevApiKey);
- end;
- try
- buffer := Http.Post('http://pastebin.com/api/api_post.php', Params);
- if Trim(buffer) = '' then
- buffer := BAD_REQUEST;
- except
- buffer := BAD_REQUEST;
- end;
- Result := ((Pos(BAD_REQUEST, buffer) <> 1) and (Pos('Paste Removed', buffer) = 1));
- end;
- destructor TPastebinCom.Destroy;
- begin
- Params.Free;
- Http.Free;
- inherited;
- end;
- function TPastebinCom.GetApiKey: string;
- begin
- Result := DevApiKey;
- end;
- function TPastebinCom.GetPasteList(var PasteList: TPasteList;
- const UserKey : string = '';
- const Limit : integer = 50): Boolean;
- const
- BAD_REQUEST = 'Bad API request';
- var
- buffer: string;
- begin
- with Params do
- begin
- Clear;
- Add('api_option=' + 'list');
- Add('api_results_limit=' + IntToStr(Limit));
- if (UserKey = '')and(bAutoUserKey) then
- Add('api_user_key=' + LocUserKey)
- else
- Add('api_user_key=' + UserKey);
- Add('api_dev_key=' + DevApiKey);
- end;
- try
- buffer := Http.Post('http://pastebin.com/api/api_post.php', Params);
- if Trim(buffer) = '' then
- buffer := BAD_REQUEST;
- except
- buffer := BAD_REQUEST;
- end;
- Result := (Pos(BAD_REQUEST, buffer) <> 1);
- SetLength(PasteList, 0);
- if Result then
- begin
- XMLToPasteList(buffer, PasteList);
- end;
- end;
- function TPastebinCom.GetPasteRAW(const PasteKey: string): string;
- begin
- try
- Result := Http.Get('http://pastebin.com/raw.php?i=' + PasteKey);
- except
- Result := '';
- end;
- end;
- function TPastebinCom.GetProxyHost: string;
- begin
- Result := Http.ProxyParams.ProxyServer;
- end;
- function TPastebinCom.GetProxyPort: integer;
- begin
- Result := Http.ProxyParams.ProxyPort;
- end;
- function TPastebinCom.GetTrendingPasteList(var PasteList: TPasteList): Boolean;
- const
- BAD_REQUEST = 'Bad API request';
- var
- buffer: string;
- begin
- with Params do
- begin
- Clear;
- Add('api_option=' + 'trends');
- Add('api_dev_key=' + DevApiKey);
- end;
- try
- buffer := Http.Post('http://pastebin.com/api/api_post.php', Params);
- if Trim(buffer) = '' then
- buffer := BAD_REQUEST;
- except
- buffer := BAD_REQUEST;
- end;
- Result := (Pos(BAD_REQUEST, buffer) <> 1);
- SetLength(PasteList, 0);
- if Result then
- begin
- XMLToPasteList(buffer, PasteList);
- end;
- end;
- function TPastebinCom.GetUserAgent: string;
- begin
- Result := Http.Request.UserAgent;
- end;
- function TPastebinCom.GetUserInfo(var UserInfo: TUserInfo;
- const UserKey : string = ''): Boolean;
- const
- BAD_REQUEST = 'Bad API request';
- var
- buffer: string;
- i : integer;
- mc : TMatchCollection;
- begin
- with Params do
- begin
- Clear;
- Add('api_option=' + 'userdetails');
- if (UserKey = '')and(bAutoUserKey) then
- Add('api_user_key=' + LocUserKey)
- else
- Add('api_user_key=' + UserKey);
- Add('api_dev_key=' + DevApiKey);
- end;
- try
- buffer := Http.Post('http://pastebin.com/api/api_post.php', Params);
- if Trim(buffer) = '' then
- buffer := BAD_REQUEST;
- except
- buffer := BAD_REQUEST;
- end;
- Result := (Pos(BAD_REQUEST, buffer) <> 1);
- if Result then
- begin
- mc := TRegEx.Matches(buffer, '<user>[^>]+>([^<]*)[^>]+>[^>]+>([^<]*)[^>]+>[^>]+>' +
- '([^<]*)[^>]+>[^>]+>([^<]*)[^>]+>[^>]+>(\d*)[^>]+>' +
- '[^>]+>([^<]*)[^>]+>[^>]+>([^<]*)[^>]+>[^>]+>([^<]*)' +
- '[^>]+>[^>]+>(\d*)[^>]+>[^>]+>');
- if (mc.Count <> 0) then
- begin
- for i := 0 to Pred(mc.Count) do
- with UserInfo do
- begin
- Name := mc.Item[i].Groups.Item[1].Value;
- FormatShort := mc.Item[i].Groups.Item[2].Value;
- Expiry := mc.Item[i].Groups.Item[3].Value;
- Avatar := mc.Item[i].Groups.Item[4].Value;
- Privacy := StrToIntDef(mc.Item[i].Groups.Item[5].Value, 0);
- WebSite := mc.Item[i].Groups.Item[6].Value;
- Email := mc.Item[i].Groups.Item[7].Value;
- Location := mc.Item[i].Groups.Item[8].Value;
- ProUser := Boolean(StrToIntDef(mc.Item[i].Groups.Item[9].Value, 0) = 1);
- end;
- end;
- end;
- end;
- function TPastebinCom.Login(var UserKey : string;
- const Name : string;
- const Password: string): Boolean;
- const
- BAD_REQUEST = 'Bad API request';
- begin
- with Params do
- begin
- Clear;
- Add('api_user_name=' + String(HTTPEncode(AnsiString(Name))));
- Add('api_user_password=' + String(HTTPEncode(AnsiString(Password))));
- Add('api_dev_key=' + DevApiKey);
- end;
- try
- UserKey := Http.Post('http://pastebin.com/api/api_login.php', Params);
- if Trim(UserKey) = '' then
- UserKey := BAD_REQUEST;
- except
- UserKey := BAD_REQUEST;
- end;
- Result := (Pos(BAD_REQUEST, UserKey) <> 1);
- if not Result then
- UserKey := ''
- else
- LocUserKey := UserKey;
- end;
- function TPastebinCom.SendPaste(var Response: string;
- const Content : string;
- const Title : string = '';
- const Language: string = '';
- const UserKey : string = '';
- const Expiry : string = EXPIRY_NEVER;
- const Privacy : dword = PRIVACY_PUBLIC): Boolean;
- const
- BAD_REQUEST = 'Bad API request';
- begin
- with Params do
- begin
- Clear;
- Add('api_option=' + 'paste');
- if (UserKey = '')and(bAutoUserKey) then
- Add('api_user_key=' + LocUserKey)
- else
- Add('api_user_key=' + UserKey);
- Add('api_paste_private=' + IntToStr(Privacy));
- Add('api_paste_name=' + Title);
- Add('api_paste_expire_date=' + Expiry);
- Add('api_paste_format=' + Language);
- Add('api_dev_key=' + DevApiKey);
- Add('api_paste_code=' + Content);
- end;
- try
- Response := Http.Post('http://pastebin.com/api/api_post.php', Params);
- if Trim(Response) = '' then
- Response := BAD_REQUEST;
- except
- Response := BAD_REQUEST;
- end;
- Result := (Pos(BAD_REQUEST, Response) <> 1);
- if not Result then
- Response := '';
- end;
- procedure TPastebinCom.SetApiKey(const Value: string);
- begin
- DevApiKey := Value;
- end;
- procedure TPastebinCom.SetProxyHost(const Value: string);
- begin
- Http.ProxyParams.ProxyServer := Value;
- end;
- procedure TPastebinCom.SetProxyPort(const Value: integer);
- begin
- Http.ProxyParams.ProxyPort := Value;
- end;
- procedure TPastebinCom.SetUserAgent(const Value: string);
- begin
- Http.Request.UserAgent := Value;
- end;
- procedure TPastebinCom.XMLToPasteList(const XML : string;
- var PasteList: TPasteList);
- var
- i : integer;
- mc : TMatchCollection;
- begin
- try
- mc := TRegEx.Matches(XML, '<paste>[^>]+>([^<]*)[^>]+>[^>]+>(\d*)[^>]+>[^>]+>' +
- '([^<]*)[^>]+>[^>]+>(\d*)[^>]+>[^>]+>(\d*)[^>]+>[^>]+>' +
- '(\d*)[^>]+>[^>]+>([^<]*)[^>]+>[^>]+>([^<]*)[^>]+>[^>]+>' +
- '([^<]*)[^>]+>[^>]+>(\d*)[^>]+>[^>]+>');
- SetLength(PasteList, mc.Count);
- if mc.Count <> 0 then
- begin
- for i := 0 to Pred(mc.Count) do
- with PasteList[i] do
- begin
- Key := mc.Item[i].Groups.Item[1].Value;
- Date := UnixDateTimeToDelphiDateTime(StrToInt(mc.Item[i].Groups.Item[2].Value));
- Title := mc.Item[i].Groups.Item[3].Value;
- Size := StrToIntDef(mc.Item[i].Groups.Item[4].Value, 0);
- Expiry := UnixDateTimeToDelphiDateTime(StrToIntDef(mc.Item[i].Groups.Item[5].Value, 0));
- Privacy := StrToIntDef(mc.Item[i].Groups.Item[6].Value, 0);
- FormatLong := mc.Item[i].Groups.Item[7].Value;
- FormatShort := mc.Item[i].Groups.Item[8].Value;
- Url := mc.Item[i].Groups.Item[9].Value;
- Hits := StrToIntDef(mc.Item[i].Groups.Item[10].Value, 0);
- end;
- end;
- except end;
- end;
- end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement