Skip to content

Instantly share code, notes, and snippets.

@Brybry16
Last active July 28, 2016 16:25
Show Gist options
  • Save Brybry16/994c3027254ac3ee14e714baafb6c8b5 to your computer and use it in GitHub Desktop.
Save Brybry16/994c3027254ac3ee14e714baafb6c8b5 to your computer and use it in GitHub Desktop.
[Twitter Bot] Pokemon GO and Pokemon Trainer Club servers checker
import threading
import tweepy
import datetime
import requests
import sys
url = {"pgo": "https://pgorelease.nianticlabs.com/plfe", "ptc": "https://club.pokemon.com"}
servers = {"pgo": "Pokemon GO", "ptc": "Pokemon Trainer Club"}
data = {"pgo": {"online": True, "latency": 0, "idle": 0}, "ptc": {"online": True, "latency": 0, "idle": 0}}
timeout = 3
auth = tweepy.OAuthHandler(OAUTH_KEY, OAUTH_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
def tof(arg):
ua = str(arg).upper()
if ua == 'FALSE'[:len(ua)]:
return False
else:
return True
if len(sys.argv) > 1:
statusPGO = tof(sys.argv[1])
if len(sys.argv) > 2:
statusPTC = tof(sys.argv[2])
else:
statusPGO = True
statusPTC = True
def log(text):
print str(datetime.datetime.now().time().isoformat()) + ": " + str(text)
# On construit le tweet et on le balance
def tweet(status, server):
now = datetime.datetime.now()
text = "[{:02d}/{:02d}/{:04d} - {:02d}:{:02d} CEST]\n".format(now.day, now.month, now.year, now.hour, now.minute)
if status:
text += str(servers[server]) + " servers are back up!\nCurrent latency: " + str(data[server]["latency"]) + "s"
else:
text += str(servers[server]) + " servers seem down!\nWill tweet again when the service is back up !"
global api
api.update_status(text)
log(str("Tweeted:\n" + text + "\n\n---------------------------------------\n\n"))
# MaJ des datas si le service est up
def up(service, time):
global data
if data[service]["online"]:
data[service]["idle"] += 1
else:
data[service]["idle"] = 0
data[service]["online"] = True
data[service]["latency"] = time
# MaJ des datas si le service est down
def down(service, time):
global data
if not data[service]["online"]:
data[service]["idle"] += 1
else:
data[service]["idle"] = 0
data[service]["online"] = False
data[service]["latency"] = 0
# Teste si le service passe en parametre fonctionne
def request(service):
try:
r = requests.get(url[service])
time = r.elapsed.total_seconds()
if r.status_code == 500:
log(service + " erreur 500")
down(service, time)
elif time < timeout:
log(service + " up")
up(service, time)
else:
log(service + " timeout")
down(service, time)
except requests.exceptions.RequestException, e:
log(service + " timeout")
down(service, time)
# Checks des status
def execute():
threading.Timer(60.0, execute).start()
global statusPGO
global statusPTC
request("pgo")
request("ptc")
# Si les datas sont differentes du last tweet et qu'il y a eu au moins 5 exec depuis la derniere fois... on tweet !
if ((not data["pgo"]["online"] and statusPGO) or (data["pgo"]["online"] and not statusPGO)) and data["pgo"]["idle"] >= 5:
statusPGO = not statusPGO
tweet(statusPGO, "pgo")
# Si les datas sont differentes du last tweet et qu'il y a eu au moins 5 exec depuis la derniere fois... on tweet !
if ((not data["ptc"]["online"] and statusPTC) or (data["ptc"]["online"] and not statusPTC)) and data["ptc"]["idle"] >= 5:
statusPTC = not statusPTC
tweet(statusPTC, "ptc")
log(str("PGO datas - Online: " + str(data["pgo"]["online"]) + " | idle: " + str(data["pgo"]["idle"]) + " | Lat.: " + str(data["pgo"]["latency"]) + "s"))
log(str("PTC datas - Online: " + str(data["ptc"]["online"]) + " | idle: " + str(data["ptc"]["idle"]) + " | Lat.: " + str(data["ptc"]["latency"]) + "s"))
log(str("Status PGO: " + str(statusPGO) + " | Status PTC: " + str(statusPTC) + "\n"))
execute()
@PastaGringo
Copy link

PastaGringo commented Jul 28, 2016

Hello!
Merci pour le script!
J'essaye de le mettre en place pour twitter lors de l'indispo des des serveurs PTC.
Le script fonctionne bien je vois l'état des services mais lors des down le script ne twitte pas, est-normal ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment