Implement poke timeout to prevent the script from stalling

This commit is contained in:
Klaus-Uwe Mitterer 2015-01-02 19:48:16 +01:00
parent 0a5f50650c
commit fc05b6207a
2 changed files with 26 additions and 4 deletions

12
README
View file

@ -4,7 +4,7 @@ by Klaus-Uwe Mitterer <pokemon@klaus-uwe.me>
== 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.

View file

@ -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: