From fc05b6207a4a78197e15d53ebcf615feba04ca5f Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Fri, 2 Jan 2015 19:48:16 +0100 Subject: [PATCH] Implement poke timeout to prevent the script from stalling --- README | 12 +++++++++++- pokemon.py | 18 +++++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/README b/README index cd583c0..1a1e4a5 100644 --- a/README +++ b/README @@ -4,7 +4,7 @@ by Klaus-Uwe Mitterer == Prerequisites == -This script requires the modules urllib, cookielib, urllib2, time and BeautifulSoup. You should be able to install those using pip, but I'm not going to give detailed installation instructions. +This script requires the modules urllib, cookielib, urllib2, signal, time and BeautifulSoup. You should be able to install those using pip if you don't have them anyway, but I'm not going to give detailed installation instructions. == Setup == @@ -23,8 +23,10 @@ If your password contains special characters, specifically double quotes (") or If you have changed the settings as intended, you should be ready to go. Simply run the script by double-clicking it in your GUI or through your console like so: +-------------------------- cd PATH_TO_POKEMON ./pokemon.py +-------------------------- Obviously, replace PATH_TO_POKEMON with the actual path in your file system where you put pokemon.py. @@ -64,5 +66,13 @@ If despite all this, you can not get the script running, chances are that Facebo There are several reasons why the script may suddenly stop working. Most of the time you will just need to take a little break and try again later. +If you keep getting messages like this: + +-------------------------- +Poke timed out. +-------------------------- + +Chances are that your Internet connection is slow. This message is just a notice - no changes on your end should be necessary. You may, however, try to increase the script's waiting time at line 67 (time = 10). Values like 15 or 20 (seconds) will still be sensible and may reduce the number of messages. + Generally, the script should recover from most problems automatically. If it ends up stalling anyway, you will have to kill it manually with Ctrl+C. But really, this shouldn't happen. diff --git a/pokemon.py b/pokemon.py index dba77c8..9ea39c3 100755 --- a/pokemon.py +++ b/pokemon.py @@ -1,14 +1,13 @@ #!/usr/bin/env python -# vim: set fileencoding=utf8 : '''pokemon.py: Automatically poke back on Facebook''' __author__ = "Klaus-Uwe Mitterer" __maintainer__ = __author__ __email__ = "info@klaus-uwe.me" -__version__ = "0.1" +__version__ = "0.2" -import urllib, cookielib, urllib2, time, BeautifulSoup +import urllib, cookielib, urllib2, time, BeautifulSoup, signal class Account: jar = cookielib.CookieJar() @@ -64,6 +63,18 @@ class Account: me = Account() me.login() +class Timeout: + time = 10 + + def timeouthandler(self, signum, frame): + raise Exception("Poke timed out.") + + def timeoutstarter(self): + signal.signal(signal.SIGALRM, self.timeouthandler) + signal.alarm(self.time) + +to = Timeout() + if me.loggedin(): print("Login successful!") else: @@ -74,6 +85,7 @@ else: while True: try: + to.timeoutstarter() if not me.poke(): me.login() except KeyboardInterrupt: