We are writing monitoring of the availability of tickets for Russian Railways, part 2

  • Tutorial

The first part showed how to write the simplest wrapper around the existing http API rzhd. In this part, we continue and write the actual monitoring with a notification by mail. The project site is still located here and here is a spare .

In the first part, the application was not threadsafe, i.e. in the app.yaml settings it was listed:
threadsafe: false
fix this situation, change:

def main():
if __name__ == "__main__":

on the:
app = webapp2.WSGIApplication([
    ('/', MainPage),
], debug=False)

Naturally, I also had to rewrite a bit of code, the gain from all of this is as follows: with threadsafe: false appengine puts new sessions in the queue and starts a new instance for each session (with a small time lag), when threadsafe is turned on, it means that the developer correctly implemented multi-threaded logic.

Next, it was necessary to solve two issues: mail and launch by crown, let's start with the crown: for this you need to create the cron.yaml file in the working directory and specify where and how often the crown should be addressed to the daemon:

- description: mail summary job
  url: /summary_mail
  schedule: every 1 hours

On the
we’ll hang the handler for distribution to active accounts, so that bad uncles can’t pull it on their own and, as a result, eat all our quotas and spam active users, we need to restrict access to it only for admins and google krona, add the lines in app.yaml :

- url: /summary_mail
  script: web.app
  login: admin

The list of admins for the application is configured from the administrator’s console (guys, schoolchildren are hackers, don’t worry, there is no admin in this application and there is no need to try to “punch” it, as it follows from the logs when publishing the last post, there were experts ..., this console is provided by appengine).
For some time I thought - what to do with the mailing list, screw on a third-party captcha or do my own, eventually went the simplest way - I use a Google account and after going through Google authorization (of course everything goes through a redirect and I do not store any passwords) I get the user's mailbox, to which he will receive letters about free places. The functionality for working with mail is the simplest:

def sendMail(account, body):
  mail.send_mail(sender  = "rzd wrapper support ",
                 to      = account.email(),
                 subject = "train report",
                 html    = body,
                 body    = body)

Yes, the body of the letter in textual presentation and html is the same for me, but soon I will fix it.
For the sake of completeness, I must say that the radioactive sources are still there , of course I will put things in order there over time, there may also be rare but operational errors, because the Railways does not perfectly respond, and where I could have laid straws, but it seems not everywhere , in general, the functionality is quite working, everyone from the source can start their local application and finish it to their own needs. Cron interrogates the availability of tickets once an hour. At the moment, you can only subscribe to one train. In future plans, fasten Jinja2 and make a more pleasant interface, just try to implement on APPENGINE the interface to open source morphologyand do something like a spell checker. I hope this service will be useful for someone else.

Also popular now: