• R/O
  • SSH
  • HTTPS

instalikes: Commit


Commit MetaInfo

Revision11 (tree)
Zeit2019-11-18 04:35:38
Autorderekwildstar

Log Message

Utilização de uma imagem para representar a situação que ocorre quando um usuário deixa de existir ou mudou seu username
Método wait agora atualiza um label no FORMStatus para mostrar o timeout para a próxima ação
UpdateRequests foi colocado junto a todas as instância de Request
Otimizações de código aplicadas nos métodos de seguir e dar likes

Ändern Zusammenfassung

Diff

--- trunk/src/UDAMOPrincipal.pas (revision 10)
+++ trunk/src/UDAMOPrincipal.pas (revision 11)
@@ -13,7 +13,7 @@
1313 Vcl.PlatformDefaultStyleActnCtrls, Vcl.ActnPopup, System.Actions,
1414 Vcl.ActnList, Vcl.ActnMan, KRK.Components.DataControls.DBGrid, JSON,
1515 Vcl.Forms, FireDAC.Phys.MySQLDef, FireDAC.Phys.MySQL,
16- FireDAC.Comp.ScriptCommands, FireDAC.Stan.Util, FireDAC.Comp.Script;
16+ FireDAC.Comp.ScriptCommands, FireDAC.Stan.Util, FireDAC.Comp.Script, UFORMStatus;
1717
1818 type
1919 TMedia = record
@@ -136,8 +136,8 @@
136136 FLoggedUser: TInstagramUser;
137137 FStopProcessing: Boolean;
138138 function UserExists(AUserName: String): Boolean;
139- procedure LoadProfilePicture(AUserId: String; APictureURL: String; AImage: TImage; AForceUpdateCache: Boolean = False);
140- procedure DownloadProfilePicture(APictureURL: String; AUserId: String; AImage: TImage);
139+ procedure LoadProfilePicture(AUserId: String; APictureURL: String; AImage: TImage; const ANoImage: TIMage; AForceUpdateCache: Boolean = False);
140+ procedure DownloadProfilePicture(APictureURL: String; AUserId: String; AImage: TImage; const ANoImage: TImage);
141141 procedure UpdateRequestsSoFar;
142142 function ParseSharedData(AResponse: TStringStream; out ASharedData: TJSONObject): Boolean;
143143 function ParseFollowersPage(AResponse: TStringStream; out AFollowersData: TJSONObject): Boolean;
@@ -170,7 +170,7 @@
170170 procedure ConfigureErrorHint(ATitle, AText: String; AWinControl: TWinControl; AShowHint: Boolean);
171171 function Login(AUserName, APassword: String): Boolean;
172172 function GetUserInfo(AUserName: String; var AInstagramUser: TInstagramUser): Boolean;
173- procedure Wait; { isso é gambi }
173+ procedure Wait(AFormStatus: TFormStatus); { isso é gambi }
174174 function GetLoggedUser(out AInstagramUser: TInstagramUser): Boolean;
175175 function Logout: Boolean;
176176 procedure ConnectToDatabase;
@@ -189,9 +189,8 @@
189189 uses
190190 KRK.Lib.Rtl.Win.WinInet.Utilities, Windows, WinInet,
191191 KRK.Lib.RegExp.Utils, NetEncoding, System.RegularExpressionsCore,
192- UFORMPrincipal, jpeg, KRK.Lib.Vcl.Forms.FormBlender,
193- UFORMStatus, UFORMAddUserParams, System.Generics.Collections, Math,
194- UConfigurations;
192+ UFORMPrincipal, jpeg, KRK.Lib.Vcl.Forms.FormBlender, UFORMAddUserParams,
193+ System.Generics.Collections, Math, UConfigurations;
195194
196195 {%CLASSGROUP 'Vcl.Controls.TControl'}
197196
@@ -263,7 +262,7 @@
263262 if Trim(FORMPrincipal.EDITProfile.Text) <> '' then
264263 begin
265264 AddUser(FORMPrincipal.EDITProfile.Text);
266- LoadProfilePicture(FDTAUserid.AsString,FDTAUserpictureurl.AsString,FORMPrincipal.IMAGProfile);
265+ LoadProfilePicture(FDTAUserid.AsString,FDTAUserpictureurl.AsString,FORMPrincipal.IMAGProfile,FORMPrincipal.IMAGNoImage);
267266 end
268267 else
269268 ConfigureErrorHint('Blank Field','Please inform the profile name to add',FORMPrincipal.EDITProfile,True);
@@ -408,11 +407,11 @@
408407 FORMPrincipal.LAEDMaximumWaitInterval.Text := Configurations.MaximumWaitSeconds.ToString;
409408 end;
410409
411-procedure TDAMOPrincipal.LoadProfilePicture(AUserId: String; APictureURL: String; AImage: TImage; AForceUpdateCache: Boolean = False);
410+procedure TDAMOPrincipal.LoadProfilePicture(AUserId: String; APictureURL: String; AImage: TImage; const ANoImage: TIMage; AForceUpdateCache: Boolean = False);
412411 begin
413412 if AForceUpdateCache or (not FileExists(ExtractFilePath(ParamStr(0)) + '\cache\' + AUserId + '.jpg')) then
414413 begin
415- DownloadProfilePicture(APictureURL,AUserId,AImage);
414+ DownloadProfilePicture(APictureURL,AUserId,AImage,ANoImage);
416415
417416 if not DirectoryExists(ExtractFilePath(ParamStr(0)) + '\cache') then
418417 ForceDirectories(ExtractFilePath(ParamStr(0)) + '\cache');
@@ -434,7 +433,7 @@
434433 end;
435434
436435
437-procedure TDAMOPrincipal.DownloadProfilePicture(APictureURL: String; AUserId: String; AImage: TImage);
436+procedure TDAMOPrincipal.DownloadProfilePicture(APictureURL: String; AUserId: String; AImage: TImage; const ANoImage: TImage);
438437 var
439438 RO: TRequestOptions;
440439 RE: TResponse;
@@ -466,15 +465,20 @@
466465 Screen.Cursor := crHourGlass;
467466 try
468467 Request(RO, RE);
468+ UpdateRequestsSoFar;
469+// Wait; pra baixar a imagem não é necessário esperar
469470 finally
470471 Screen.Cursor := crDefault;
471472 end;
472473
473- UpdateRequestsSoFar;
474474 RE.Content.Seek(0, soFromBeginning);
475+ try
476+ AImage.Picture.LoadFromStream(RE.Content);
477+ except
478+ { Algumas pessoas não existem mais então ao carregar a imagem dá erro }
479+ AImage.Picture.Assign(ANoImage.Picture);
480+ end;
475481
476- AImage.Picture.LoadFromStream(RE.Content);
477-
478482 // if not DirectoryExists(ExtractFilePath(ParamStr(0)) + '\cache') then
479483 // ForceDirectories(ExtractFilePath(ParamStr(0)) + '\cache');
480484 //
@@ -546,15 +550,21 @@
546550 //end;
547551
548552
549-procedure TDAMOPrincipal.Wait;
553+procedure TDAMOPrincipal.Wait(AFormStatus: TFormStatus);
550554 var
551- NextTick: Cardinal;
555+ TimeOut: Cardinal;
552556 begin
553557 Randomize;
554558
555- NextTick := GetTickCount + Cardinal(RandomRange(Configurations.MinimumWaitSeconds,Configurations.MaximumWaitSeconds)) * 1000;
556- while (GetTickCount < NextTick) and (not FStopProcessing) do
559+ TimeOut := GetTickCount + Cardinal(RandomRange(Configurations.MinimumWaitSeconds,Configurations.MaximumWaitSeconds)) * 1000;
560+
561+ while (GetTickCount < TimeOut) and (not FStopProcessing) do
562+ begin
563+ if Assigned(AFormStatus) then
564+ AFormStatus.LABETimeOut.Caption := 'Next action in ' + ((TimeOut - GetTickCount) div 1000).ToString + ' seconds...';
565+
557566 Application.ProcessMessages;
567+ end;
558568 end;
559569
560570 procedure TDAMOPrincipal.GetFollowersPage(AUserId: String; var ANextPageToken: String; out AInstagramUsers: TInstagramUsers; out ATotalFollowers: Cardinal; AUsersPerPage: Byte = 50);
@@ -613,10 +623,11 @@
613623 Screen.Cursor := crHourGlass;
614624 try
615625 Request(RO, RE);
626+ UpdateRequestsSoFar;
627+ // Wait; não precisa esperar para pegar a proxima página, porque isso pode ser feito também rapidamente pelo bnavegador
616628 finally
617629 Screen.Cursor := crDefault;
618630 end;
619- UpdateRequestsSoFar;
620631
621632 // ParseSharedData(TStringStream(RE.Content),JO);
622633
@@ -744,7 +755,7 @@
744755 procedure TDAMOPrincipal.FDTAUserAfterScroll(DataSet: TDataSet);
745756 begin
746757 if (DataSet.State = dsBrowse) and (DataSet.RecordCount > 0) then
747- LoadProfilePicture(FDTAUserid.AsString,FDTAUserpictureurl.AsString,FormPrincipal.IMAGProfile);
758+ LoadProfilePicture(FDTAUserid.AsString,FDTAUserpictureurl.AsString,FormPrincipal.IMAGProfile,FORMPrincipal.IMAGNoImage);
748759 end;
749760
750761 procedure TDAMOPrincipal.FDTAUserfollowersGetText(Sender: TField; var Text: string; DisplayText: Boolean);
@@ -813,10 +824,12 @@
813824 Screen.Cursor := crHourGlass;
814825 try
815826 Request(RO, RE);
827+ UpdateRequestsSoFar;
828+// Wait;
816829 finally
817830 Screen.Cursor := crDefault;
818831 end;
819- UpdateRequestsSoFar;
832+
820833 //ParseSharedData(TStringStream(RE.Content),JO);
821834
822835 Result := ParseFollowResult(TStringStream(RE.Content));
@@ -863,7 +876,6 @@
863876 Registered := 0;
864877 Page := 1;
865878 repeat
866- Wait;
867879 if FStopProcessing then
868880 Break;
869881
@@ -1036,10 +1048,12 @@
10361048 Screen.Cursor := crHourGlass;
10371049 try
10381050 Request(RO, RE);
1051+ UpdateRequestsSoFar;
1052+// Wait;
10391053 finally
10401054 Screen.Cursor := crDefault;
10411055 end;
1042- UpdateRequestsSoFar;
1056+
10431057 // ParseSharedData(TStringStream(RE.Content),JO);
10441058
10451059 //Result := RegExMatch(TStringStream(RE.Content).DataString,'window\._sharedData = ({.*})',1,1,False,[],JS);
@@ -1283,7 +1297,7 @@
12831297 if (FDTAUser.State = dsBrowse) and (FDTAUser.RecordCount > 0) and GetUserInfo(FDTAUserusername.AsString,IU) then
12841298 begin
12851299 UpdateUser(IU);
1286- LoadProfilePicture(IU.id,IU.PictureURL,FORMPrincipal.IMAGProfile,True);
1300+ LoadProfilePicture(IU.id,IU.PictureURL,FORMPrincipal.IMAGProfile,FORMPrincipal.IMAGNoImage,True);
12871301 FDTAUser.Refresh;
12881302 FDTAFollowers.Refresh;
12891303 FDTAMedias.Refresh;
@@ -1331,10 +1345,12 @@
13311345 Screen.Cursor := crHourGlass;
13321346 try
13331347 Request(RO, RE);
1348+ UpdateRequestsSoFar;
1349+// Wait;
13341350 finally
13351351 Screen.Cursor := crDefault;
13361352 end;
1337- UpdateRequestsSoFar;
1353+
13381354 // Precisa UpdateHeaders porque esta função é usada ao iniciar o programa
13391355 // para pegar os cabeçalhos de quem estiver logado
13401356 // ParseResponseHeaders(RE.Headers);
@@ -1439,10 +1455,12 @@
14391455 Screen.Cursor := crHourGlass;
14401456 try
14411457 Request(RO, RE);
1458+ UpdateRequestsSoFar;
1459+// Wait;
14421460 finally
14431461 Screen.Cursor := crDefault;
14441462 end;
1445- UpdateRequestsSoFar;
1463+
14461464 // ParseSharedData(TStringStream(RE.Content),JO);
14471465
14481466 Result := TStringStream(RE.Content).DataString = '{"status": "ok"}';
@@ -1456,7 +1474,7 @@
14561474
14571475 procedure TDAMOPrincipal.DisplayLoggedUserImage;
14581476 begin
1459- LoadProfilePicture(FLoggedUser.id,FLoggedUser.PictureURL,FORMPrincipal.IMAGUserLogged,True);
1477+ LoadProfilePicture(FLoggedUser.id,FLoggedUser.PictureURL,FORMPrincipal.IMAGUserLogged,FORMPrincipal.IMAGNoImage,True);
14601478 end;
14611479
14621480 function TDAMOPrincipal.Login(AUserName: String; APassword: String): Boolean;
@@ -1497,13 +1515,16 @@
14971515 // Acessa a página inicial deslogado e obtém alguns headers ================
14981516 RO.InternetConnectParams.ServerName := 'https://www.instagram.com';
14991517 RO.HttpOpenRequestParams.Verb := 'GET';
1518+
15001519 Screen.Cursor := crHourGlass;
15011520 try
15021521 Request(RO, RE);
1522+ UpdateRequestsSoFar;
1523+// Wait;
15031524 finally
15041525 Screen.Cursor := crDefault;
15051526 end;
1506- UpdateRequestsSoFar;
1527+
15071528 //ParseResponseHeaders(RE.Headers);
15081529 ParseSharedData(TStringStream(RE.Content),JO); // Só precisa disso para preencher configurar FRolloutHash e FCSRFToken
15091530 TStringStream(RE.Content).Clear;
@@ -1523,10 +1544,12 @@
15231544 Screen.Cursor := crHourGlass;
15241545 try
15251546 Request(RO, RE);
1547+ UpdateRequestsSoFar;
1548+// Wait;
15261549 finally
15271550 Screen.Cursor := crDefault;
15281551 end;
1529- UpdateRequestsSoFar;
1552+
15301553 //ParseSharedData(TStringStream(RE.Content),JO);
15311554
15321555 Result := ParseLoginResult(TStringStream(RE.Content),FLoggedUser);
@@ -1586,10 +1609,12 @@
15861609 Screen.Cursor := crHourGlass;
15871610 try
15881611 Request(RO, RE);
1612+ UpdateRequestsSoFar;
1613+// Wait;
15891614 finally
15901615 Screen.Cursor := crDefault;
15911616 end;
1592- UpdateRequestsSoFar;
1617+
15931618 //ParseResponseHeaders(RE.Headers);
15941619 //ParseSharedData(TStringStream(RE.Content),JO);
15951620 Result := not GetLoggedUser(Dummy);
@@ -1733,29 +1758,30 @@
17331758
17341759 with AKRKDBGridUser.DataSource.DataSet do
17351760 begin
1736- DisableControls;
17371761 BM := Bookmark;
17381762 try
17391763 Followed := 0;
1740- First;
1741- while not EOF do
1764+
1765+ for var i: Cardinal := 0 to Pred(AKRKDBGridUser.SelectedRows.Count) do
17421766 begin
1743- if AKRKDBGridUser.SelectedRows.CurrentRowSelected then
1744- begin
1745- Wait;
1767+ if FStopProcessing then
1768+ Break;
17461769
1747- if Follow(FDTAUserid.AsString) in [frRequested,frFollowing] then
1748- Inc(Followed);
1770+ // Posicionando no registro marcado
1771+ GotoBookmark(AKRKDBGridUser.SelectedRows[i]);
17491772
1750- FS.LABEStatus.Caption := Followed.ToString + ' / ' + AKRKDBGridUser.SelectedRows.Count.ToString;
1751- FS.Update;
1752- end;
1773+ // Considerando que o comando acima foi bem sucedido, neste ponto
1774+ // estamos no registro marcado
17531775
1754- Next;
1776+ if Follow(FDTAUserid.AsString) in [frFollowing,frRequested] then
1777+ Inc(Followed);
1778+
1779+ FS.LABEStatus.Caption := Followed.ToString + ' / ' + AKRKDBGridUser.SelectedRows.Count.ToString;
1780+ FS.Update;
1781+ Wait(FS);
17551782 end;
17561783 finally
17571784 Bookmark := BM;
1758- EnableControls;
17591785 end;
17601786 end;
17611787 // - ///////////////////////////////////////////////////////////////////////
@@ -1776,7 +1802,7 @@
17761802 BM: TBookmark;
17771803 FB: TKRKFormBlender;
17781804 FS: TFORMStatus;
1779- Liked: Word;
1805+ Processed: Word;
17801806 begin
17811807 TKRKFormBlender.ShowMe(TForm(AKRKDBGridUser.Owner),TFORMStatus,FB);
17821808 try
@@ -1788,6 +1814,7 @@
17881814 FS.LABEStatus.Caption := '0 / ' + AKRKDBGridUser.SelectedRows.Count.ToString;
17891815 FS.Update;
17901816 // - ///////////////////////////////////////////////////////////////////////
1817+
17911818 FStopProcessing := False;
17921819
17931820 with AKRKDBGridUser.DataSource.DataSet do
@@ -1796,25 +1823,30 @@
17961823 // mestre detalhe e não posso usar BlockReadSize := 1 porque
17971824 // currentrowselected não funciona mais além de serem apresentados
17981825 // comportamenos estranhos no grid :/
1826+ // Não sei ao certo se com o MySQL eu ainda não posso desativar os
1827+ // controles. É preciso testar isso no futuro
17991828 BM := Bookmark;
18001829 try
1801- Liked := 0;
1802- First;
1803- while not EOF do
1830+ Processed := 0;
1831+
1832+ for var i: Cardinal := 0 to Pred(AKRKDBGridUser.SelectedRows.Count) do
18041833 begin
1805- if AKRKDBGridUser.SelectedRows.CurrentRowSelected then
1834+ if FStopProcessing then
1835+ Break;
1836+
1837+ // Posicionando no registro marcado
1838+ GotoBookmark(AKRKDBGridUser.SelectedRows[i]);
1839+
1840+ // Considerando que o comando acima foi bem sucedido, neste ponto
1841+ // estamos no registro marcado
1842+ if (FDTAMedias.RecordCount > 0) then
18061843 begin
1807- if (FDTAMedias.RecordCount > 0) then
1844+ FDTAMedias.First;
1845+
1846+ while (not FDTAMedias.Eof) and (not FStopProcessing) do
18081847 begin
1809- FDTAMedias.First;
1810-
18111848 if not FDTAMediaswasliked.AsBoolean then
18121849 begin
1813- Wait;
1814-
1815- if FStopProcessing then
1816- Break;
1817-
18181850 if LikeMedia(FDTAMediasid.AsString) then
18191851 begin
18201852 FDTAMedias.Edit;
@@ -1821,15 +1853,18 @@
18211853 FDTAMediaswasliked.AsBoolean := True;
18221854 FDTAMedias.Post;
18231855 end;
1856+
1857+ Wait(FS);
1858+ Break; // Só curte a primeira mídia não curtida
18241859 end;
1860+
1861+ FDTAMedias.Next;
18251862 end;
1826-
1827- Inc(Liked);
1828- FS.LABEStatus.Caption := Liked.ToString + ' / ' + AKRKDBGridUser.SelectedRows.Count.ToString;
1829- FS.Update;
18301863 end;
18311864
1832- Next;
1865+ Inc(Processed);
1866+ FS.LABEStatus.Caption := Processed.ToString + ' / ' + AKRKDBGridUser.SelectedRows.Count.ToString;
1867+ FS.Update;
18331868 end;
18341869 finally
18351870 Bookmark := BM;
--- trunk/src/UFORMPrincipal.pas (revision 10)
+++ trunk/src/UFORMPrincipal.pas (revision 11)
@@ -7,7 +7,7 @@
77 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.ExtCtrls,
88 Vcl.Imaging.pngimage, System.ImageList, Vcl.ImgList, UPngImageList, Data.DB,
99 Vcl.Grids, Vcl.DBGrids, KRK.Components.DataControls.DBGrid, Vcl.Buttons,
10- UPngBitBtn, UPngSpeedButton, UDAMOPrincipal, Vcl.DBCtrls;
10+ UPngBitBtn, UPngSpeedButton, UDAMOPrincipal, Vcl.DBCtrls, Vcl.Imaging.jpeg;
1111
1212 type
1313 TFORMPrincipal = class(TForm)
@@ -64,6 +64,7 @@
6464 TASHConfigurations: TTabSheet;
6565 LAEDMinimumWait: TLabeledEdit;
6666 LAEDMaximumWaitInterval: TLabeledEdit;
67+ IMAGNoImage: TImage;
6768 procedure PNSBLogarClick(Sender: TObject);
6869 procedure FormShow(Sender: TObject);
6970 procedure PNSBLogoutClick(Sender: TObject);
--- trunk/src/UFORMStatus.pas (revision 10)
+++ trunk/src/UFORMStatus.pas (revision 11)
@@ -4,7 +4,7 @@
44
55 uses
66 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
7+ Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls;
88
99 type
1010 TFORMStatus = class(TForm)
@@ -11,8 +11,14 @@
1111 LABEStatus: TLabel;
1212 BUTNCancel: TButton;
1313 LABETitle: TLabel;
14+ LABETimeOut: TLabel;
15+ LABETimeIn: TLabel;
16+ TMER: TTimer;
17+ procedure TMERTimer(Sender: TObject);
18+ procedure FormShow(Sender: TObject);
1419 private
1520 { Private declarations }
21+ FStartTime: Cardinal;
1622 public
1723 { Public declarations }
1824 end;
@@ -21,5 +27,15 @@
2127
2228 {$R *.dfm}
2329
30+procedure TFORMStatus.FormShow(Sender: TObject);
31+begin
32+ FStartTime := GetTickCount;
33+end;
34+
35+procedure TFORMStatus.TMERTimer(Sender: TObject);
36+begin
37+ LABETimeIn.Caption := 'Elapsed time: ' + FormatDateTime('nn:ss',((GetTickCount - FStartTime) div 1000 / 86400));
38+end;
39+
2440 end.
2541
Show on old repository browser