Skip to content

Instantly share code, notes, and snippets.

@HemulGM
Created September 14, 2021 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HemulGM/4a59b45fa1764302a624a401fcbed190 to your computer and use it in GitHub Desktop.
Save HemulGM/4a59b45fa1764302a624a401fcbed190 to your computer and use it in GitHub Desktop.
hh_delphi_stat
program hh_delphi_stat;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.JSON,
System.StrUtils,
System.NetEncoding,
HGM.Common.Download,
HGM.SQLang in '..\SQLite\HGM.SQLang.pas',
HGM.SQLite in '..\SQLite\HGM.SQLite.pas',
HGM.SQLite.Wrapper in '..\SQLite\HGM.SQLite.Wrapper.pas';
const
TG_BOT_TOKEN = {$INCLUDE bot_key.api_key};
VK_BOT_TOKEN = {$INCLUDE vk_bot_key.api_key};
var
Commontext: string;
procedure CreateTables(DB: TSQLiteDatabase);
begin
with SQL.CreateTable('items') do
begin
AddField('id', ftInteger, True, True);
AddField('name', ftString);
AddField('value', ftInteger);
AddField('date', ftDateTime);
DB.ExecSQL(GetSQL);
EndCreate;
end;
end;
procedure AddValue(DB: TSQLiteDatabase; Name: string; Value: integer);
begin
with SQL.InsertInto('items') do
begin
AddValueAsParam('name');
AddValueAsParam('value');
AddValueAsParam('date');
DB.ExecSQL(GetSQL, [Name, Value, Now]);
EndCreate;
end;
end;
function GetLastValue(DB: TSQLiteDatabase; Name: string): integer;
begin
with SQL.Select('items', ['value']) do
begin
WhereFieldLike('name', '?');
Result := DB.GetTableValue(GetSQL, [Name]);
EndCreate;
end;
end;
function FormatQuery(const Value: string): string;
const
Size = 13;
begin
Result := (Value + ': ').PadRight(Size, ' ');
end;
procedure QueryHHVacancie(DB: TSQLiteDatabase; const Name, Query: string);
begin
var Response: string;
if TDownload.GetText('https://api.hh.ru/vacancies?text=' + TURLEncoding.URL.Encode(Query), Response) then
begin
var JSON := TJSONValue.ParseJSONValue(Response);
var Count: integer;
if Assigned(JSON) and JSON.TryGetValue('found', Count) then
try
var DeltaStr := '';
var Delta := GetLastValue(DB, Name);
if Delta >= 0 then
begin
Delta := Count - Delta;
DeltaStr := ' ('+ IfThen(Delta > 0, '+') + Delta.ToString + ')';
end;
var QueryText := Query;
CommonText := CommonText + FormatQuery(QueryText) + Count.ToString + DeltaStr + #13#10;
Writeln('Вакансий на HH для ', QueryText, ': ', Count, DeltaStr);
AddValue(DB, Name, Count);
finally
JSON.Free;
end;
end;
end;
procedure SendToTelegram(ChatId: string; const Text: string);
begin
TDownload.GetRequest('https://api.telegram.org/' + TG_BOT_TOKEN +
'/sendMessage?chat_id=' + ChatId +
'&parse_mode=Markdown' +
'&text=' + TURLEncoding.URL.Encode(Text));
end;
procedure SendToVk(ChatId: integer; const Text: string);
begin
TDownload.GetRequest('https://api.vk.com/method/messages.send?access_token=' + VK_BOT_TOKEN +
'&peer_id=' + ChatId.ToString +
'&random_id=0' +
'&v=5.144' +
'&message=' + TURLEncoding.URL.Encode(Text));
end;
begin
try
var DB := TSQLiteDatabase.Create('stat.db');
try
CreateTables(DB);
CommonText := CommonText + 'Вакансий на HH: ' + #13#10;
QueryHHVacancie(DB, 'delphi_hh', 'Delphi');
QueryHHVacancie(DB, 'pascal_hh', 'Pascal');
QueryHHVacancie(DB, 'python_hh', 'Python');
QueryHHVacancie(DB, 'c-sharp_hh', 'C#');
QueryHHVacancie(DB, 'cpp_hh', 'C++');
QueryHHVacancie(DB, 'swift_hh', 'Swift');
QueryHHVacancie(DB, 'java_hh', 'Java');
QueryHHVacancie(DB, 'vb_hh', 'Visual Basic');
QueryHHVacancie(DB, 'go_hh', 'Go');
QueryHHVacancie(DB, 'ruby_hh', 'Ruby');
QueryHHVacancie(DB, 'kotlin_hh', 'Kotlin');
QueryHHVacancie(DB, 'rust_hh', 'Rust');
QueryHHVacancie(DB, 'fortran_hh', 'Fortran');
CommonText := CommonText;
{$IFDEF RELEASE}
//Delphi оффтоп
SendToTelegram('-1001212064902', '```'#13#10 + CommonText + '```');
{$ENDIF}
//Тестовый чат
SendToTelegram('-1001525223801', '```'#13#10 + CommonText + '```');
SendToVk(2000000008, CommonText);
finally
DB.Free;
end;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
{$IFDEF DEBUG}
readln;
{$ENDIF}
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment