Advertisement
F1remind

Untitled

Feb 1st, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.95 KB | None | 0 0
  1. #!/usr/bin/python3.4
  2.  
  3. #requests to execute GET and POST requests
  4. import requests
  5.  
  6. #bs4 to parse html
  7. from bs4 import BeautifulSoup as bs
  8.  
  9. #time to sleep for 60 seconds
  10. import time
  11.  
  12. #os to play sounds
  13. import os
  14.  
  15. #random to not request _every_ 60 seconds
  16. import random
  17.  
  18. def main(retry = 0):
  19.  
  20.    
  21.     #credentials and server
  22.     username = 'YourUserName'
  23.     password = 'YourPassword'
  24.     realm = 'en'
  25.     server = 's142-en.ogame.gameforge.com'
  26.  
  27.     #to appear more human a random interval is selected.
  28.     #it should be between 30 seconds and 5 minutes
  29.     min_wait = 30
  30.     max_wait = 500
  31.  
  32.     #Starte eine Session
  33.     session = requests.session()
  34.  
  35.     #get the homepage
  36.     homepage = session.get('https://' + realm + '.gameforge.com')
  37.  
  38.     #make it usable with BeautifulSoup
  39.     soup = bs(homepage.content, 'html.parser')
  40.  
  41.     #prepare the POST data
  42.     logindata = {'kid':'', 'login':username, 'pass':password, 'uni': server}
  43.  
  44.     #get the logged in page
  45.     gamepage = session.post('https://' + realm + '.ogame.gameforge.com:443/main/login/index.php?', data=logindata)
  46.  
  47.     #make it usable with BeautifulSoup
  48.     soup = bs(gamepage.content, 'html.parser')
  49.  
  50.     #save, if the login was successful or not
  51.     login = False
  52.  
  53.     #game loop
  54.     while(username in session.get('https://' + server + '/game/index.php?page=overview').text):
  55.  
  56.         #the login was successful, save it
  57.         login = True
  58.  
  59.         #get the events using ajax
  60.         eventpage = session.get('https://' + server + '/game/index.php?page=eventList&ajax=1')
  61.  
  62.         #make it usable using BeautifulSoup
  63.         soup = bs(eventpage.content, 'html.parser')
  64.  
  65.         #Every fleet entry uses the class 'eventFleet', so select all of them
  66.         fleets = soup.select('.eventFleet')
  67.  
  68.         #save, if any events happned
  69.         event = False
  70.  
  71.         #check every fleet movement
  72.         for fleet in fleets:
  73.  
  74.             #type 1 means attack
  75.             if fleet.get('data-mission-type') == '1':
  76.  
  77.                 #If the attack is hostile, warn!
  78.                 if fleet.select('.hostile'):
  79.                     event = True
  80.                     print('ENEMY ATTACKS!')
  81.                     for i in range(3):
  82.                         #can be replaced with a simple print or as complex as an email. this needs "SoX" to be installed
  83.                         os.system('play --no-show-progress --null --channels 1 synth 2.5 sin     667 gain -5 bend .35,180,.25 .15,740,.53 0,-520,.3 vol 0.25')
  84.             #type 6 means espionage
  85.             elif fleet.get('data-mission-type') == '6':
  86.  
  87.                 #if the espionage is hostile, warn!
  88.                 if fleet.select('.hostile'):
  89.                     event = True
  90.                     print('ENEMY SPIES!')
  91.  
  92.         #if no event happened, at least say so
  93.         if not event:
  94.             print("Nothing happened...")
  95.  
  96.         #sleep for a random interval
  97.         time.sleep(random.randint(min_sleep, max_sleep))
  98.  
  99.         #if it retried before and worked now, its safe to assume
  100.         #a force logout and no user interaction
  101.         retry = False
  102.  
  103.     if not login:
  104.         print("Login not successful. Check credentials.")
  105.     else:
  106.         print("Player logged in or force logout.")
  107.         if not retry:
  108.             if time.gmtime().tm_hour == 1 and time.gmtime().tm_min > 50:
  109.                     print("Attempting to login again..")
  110.                     main(retry = time.time())
  111.             elif time.gmtime().tm_hour == 2 and time.gmtime().tm_min < 10:
  112.                     print("Attempting to login again..")
  113.                     main(retry = time.time())
  114.             else:
  115.                 print("Not the typical time to reset. Shutting down.")
  116.         else:
  117.             print("Already retryed login, user is possibly active. Shutting down.")
  118.     print("Attackwarner shut down at {}:{}.".format(time.localtime().tm_hour, time.localtime().tm_min))
  119.            
  120.  
  121. if __name__ == '__main__':
  122.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement