Jump to content
Ian Branch

Thought for the Day stopped working..

Recommended Posts

Hi Team,

D10.4.2, Indy as distributed with D10.4.2.

I implemented this code many moons ago and it was working fine.  It may have been with Indy10 via GitHub.

    //
    HTTP := TIdHTTP.Create;
    try
      try
        interim := HTTP.Get('http://quotes4all.net');
        //
        ChopStart := pos(StartQuote, interim);
        //
        if ChopStart > 1 then
        begin
          //
          ChopEnd := PosEx(EndQuote, interim, ChopStart + 1);
          //
          if ChopEnd > ChopStart then
          begin
            //
            interim := Copy(interim, ChopStart, ChopEnd - ChopStart);
            InCmnd := False;
            for Cntr := 1 to Length(interim) do
            begin
              if interim[Cntr] = '<' then
                InCmnd := True
              else if interim[Cntr] = '>' then
                InCmnd := False
              else if not InCmnd then
                Quote := Quote + interim[Cntr];
            end;
            //
            TaskMessageDlg('Thought for the Day...', Quote, mtInformation, [mbOK], 0);
            //
          end;
        end;
      finally
        FreeAndNil(HTTP); // .Free;
        lThoughtForTheDay := True;
      end;
      //
    except
      on E: Exception do
      begin
        if E.ClassName = 'EIdConnClosedGracefully' then
        begin
          //
          ShowMessage('Unable to access Thought for the Day at this time.');
          //
          EXIT;
        end
        else
        begin
          //
          ShowMessage('Exception class name = ' + E.ClassName);
          ShowMessage('Exception message = ' + E.message);
          //
        end;
      end;
      //
    end

At a time not that long ago I had to reinstall everything.  !@#$%^& Windows..

Before I go through the pain of removing the distributed Indy and re-installing Indy10, is this my issue? 

This doesn't work under the distributed Indy??

 

Regards & TIA,

Ian

Share this post


Link to post
On 2/12/2022 at 9:48 PM, Ian Branch said:

I implemented this code many moons ago and it was working fine.  It may have been with Indy10 via GitHub.

I see some language-related issues in the code, but nothing that I would consider to be a "problem" with Indy itself.

 

You are creating the TIdHTTP object before the try..except block, instead of inside of it.

 

The 'if E.ClassName = 'EIdConnClosedGracefully'' statement should be using the 'is' operator instead:

if E is EIdConnClosedGracefully then

Or better, using separate 'on' clauses in your 'except' block:

except
  on E: EIdConnClosedGracefully do begin
    //
    ShowMessage('Unable to access Thought for the Day at this time.');
    //
    EXIT;
  end;
  on E: Exception do
  begin
    //
    ShowMessage('Exception class name = ' + E.ClassName);
    ShowMessage('Exception message = ' + E.message);
    //
  end;
//
end;

And you should use a proper HTML parser instead of what you have now.

On 2/12/2022 at 9:48 PM, Ian Branch said:

Before I go through the pain of removing the distributed Indy and re-installing Indy10, is this my issue? 

This doesn't work under the distributed Indy??

Why not?  What is the ACTUAL PROBLEM you are having?  Please be more specific.  Just saying it "doesn't work" is meaningless.

 

Edited by Remy Lebeau

Share this post


Link to post

Hi Remy,

Thank you for your feedback.

I found the code over 18 months ago, implemented it as is and it worked fine.

I have had no reason to re-visit it until now when I discovered it wasn't working.

What it used to do is scrape a thought for the day from the indicated web site and display it in the TaskMessageDialog to the User.

After your comments, this is now the full function code but it still doesn't retrieve a Thought for the Day. 😞

function OnAfterShow: Boolean;
var
  HTTP: TIdHTTP;
  Quote: string;
  interim: string;
  ChopStart: Integer;
  ChopEnd: Integer;
  InCmnd: Boolean;
  Cntr: Integer;
begin
  //
  Result := True;
  //
  if MainForm.DBWReg.ReadBool('ThoughtForTheDay', 'ShowAtStart', True) then
  begin
    //
    try
      HTTP := TIdHTTP.Create;
      try
        interim := HTTP.Get('http://quotes4all.net');
        //
        ChopStart := pos(StartQuote, interim);
        if ChopStart > 1 then
        begin
          ChopEnd := PosEx(EndQuote, interim, ChopStart + 1);
          if ChopEnd > ChopStart then
          begin
            interim := Copy(interim, ChopStart, ChopEnd - ChopStart);
            InCmnd := False;
            for Cntr := 1 to Length(interim) do
            begin
              if interim[Cntr] = '<' then
                InCmnd := True
              else if interim[Cntr] = '>' then
                InCmnd := False
              else if not InCmnd then
                Quote := Quote + interim[Cntr];
            end;
            TaskMessageDlg('Thought for the Day...', Quote, mtError, [mbOK], 0);
          end;
        end;
      finally
        FreeAndNil(HTTP); // .Free;
      end;
      //
    except
      //
      on E: EIdConnClosedGracefully do
      begin
        //
        ShowMessage('Unable to access Thought for the Day at this time.');
        //
        EXIT;
      end;
      on E: Exception do
      begin
        //
        ShowMessage('Exception class name = ' + E.ClassName);
        ShowMessage('Exception message = ' + E.message);
        //
      end;
      //
    end;
    //
  end;
  //
end;

It is of course not impossible that http://quotes4all.net has changed and therefore the code is no longer applicable.  I don't know.

34 minutes ago, Remy Lebeau said:

And you should use a proper HTML parser instead of what you have now.

I am more than open to any alternative means to the same end.  Can you point me to some appropriate code/example??

 

Regards & TIA,

Ian

Edited by Ian Branch

Share this post


Link to post
On 2/14/2022 at 7:47 PM, Ian Branch said:

I have had no reason to re-visit it until now when I discovered it wasn't working.

But, you haven't explained WHY it is not working.  Are you getting errors?  Are you not getting the data you are expecting?  Be SPECIFIC.  Have you tried to debug the code at all?

Quote

After your comments, this is now the full function code but it still doesn't retrieve a Thought for the Day. 😞

Does TIdHTTP.Get() complete without exception? What are StartQuote and EndQuote defined as?

Edited by Remy Lebeau

Share this post


Link to post

Apologies Remy,

My head has been elsewhere with other more pressing operational issues. :-(

I have since discovered by examining 'interim' that the page structure in http://quotes4all.net has changed.  That is why the routine is no longer working.

FYI - 

const
  StartQuote = '<span class="quote">';
  EndQuote = '</span>';

The functionality was a nice to have, dare I say a novelty piece, so no loss if I take it away.

Apologies for wasting your time.

 

Regards,

Ian

Share this post


Link to post
20 minutes ago, Ian Branch said:

I have since discovered by examining 'interim' that the page structure in http://quotes4all.net has changed.  That is why the routine is no longer working.

FYI - 


const
  StartQuote = '<span class="quote">';
  EndQuote = '</span>';

I think if you change StartQuote to '<div class="q-te"><span>', you should be OK.  At least, until the next time the website decides to change formats.  This is why scraping data from webpages is not a good idea.  It is cumbersome and unreliable over time.  It would be better if you can find a different feed that provides data in a machine-parsable format like XML or JSON instead, then you don't run into these kind of issues.

Edited by Remy Lebeau

Share this post


Link to post

Dave - Oh so true.

Thanks Remy,

Yes that now works.  But, I have to consider, it has been not working for X time and no User has reported/mentioned it, which suggests they don't really care for it.

I prefer to get rid of code if isn't being used or provides no value.

I will ponder my navel for a bit and probably end up removing the functionality.

Having said that, I am glad it is working again. 🙂

 

Regards & Tks,

Ian

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×