daily pastebin goal
22%
SHARE
TWEET

Untitled

a guest Aug 10th, 2018 62 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import csv
  2. import requests
  3. import json
  4. import os
  5. from pprint import pprint
  6. from multiprocessing.dummy import Pool as ThreadPool
  7. import threading
  8. from urllib.parse import urlparse
  9. import re
  10. import argparse
  11. import random
  12. import time
  13.  
  14. POOL = ThreadPool(64)
  15. USED_ACCOUNTS = {}
  16. CRED = "\033[91m"
  17. CEND = "\033[0m"
  18.  
  19. # handle account timeout and displaying server player count
  20. class tick (threading.Thread):
  21.   def __init__(self, servers):
  22.     threading.Thread.__init__(self)
  23.     self.servers = servers
  24.   def run(self):
  25.     while 1:
  26.       time.sleep(1)
  27.       self.handle_timeouts()
  28.       self.display_server_count()
  29.   def handle_timeouts(self):
  30.     # prevent error that is thrown because of dict size changing while iteration is still ongoing
  31.     used_accounts = USED_ACCOUNTS
  32.     deletes = []
  33.     for k, v in used_accounts.items():
  34.       USED_ACCOUNTS[k] = v + 1
  35.       if v > 120:
  36.         deletes.append(k)
  37.     for k in deletes:
  38.       del USED_ACCOUNTS[k]
  39.   def display_server_count(self):
  40.     for server in self.servers:
  41.       print(str(server[1] + CRED + " Players: " + str(player_count(server[0]))) + CEND)
  42.  
  43. def reserve_slot(account, server, cookie):
  44.   url = 'http://battlelog.battlefield.com/bf4/launcher/reserveslotbygameid/1/'
  45.   url += account[3] + '/'  # soldierID
  46.   url += requests.get('https://keeper.battlelog.com/snapshot/'+server[0]).text[63:80] + '/1/' # gameID
  47.   url += server[0] + '/0/'  # serverGUID
  48.   s = requests.Session()
  49.   s.cookies.set("beaker.session.id", cookie)
  50.   r = s.get(url)
  51.  
  52. def player_count(guid):
  53.   count = requests.get("http://battlelog.battlefield.com/bf4/servers/getNumPlayersOnServer/pc/" + str(guid))
  54.   count = json.loads(count.text)
  55.   count = count["slots"]["2"]["current"]
  56.   return count
  57.  
  58. def seed(accounts, servers, cookies):
  59.   while 1:
  60.     for server in servers:
  61.       guid = server[0]
  62.       desired_player_count = int(server[2])
  63.       while player_count(guid) < desired_player_count:
  64.         for account in accounts:
  65.           email = account[0]
  66.           if not email in USED_ACCOUNTS:
  67.             reserve_slot(account, server, cookies[email])
  68.             USED_ACCOUNTS[email] = 0
  69.             break
  70.  
  71. def parse_args():
  72.   parser = argparse.ArgumentParser()
  73.   parser.add_argument("-v", "--verify", help="Verify account session data.", action="store_true")
  74.   args = parser.parse_args()
  75.   if not args.verify:
  76.     print(CRED + "PROTIP:" + CEND + " add -v flag to verify account data. Mandatory for first-time use.")
  77.   return args
  78.  
  79. def verify_sessions(accounts, cookies):
  80.   print("Verifying sessions.")
  81.   # check which sessions are broken and fix them, this part of code combines csv to get accounts list and searches JSON file for cookies for corresponding email
  82.   # this for loop reads from csv
  83.   broken_sessions = []
  84.   for rows in accounts:
  85.     email = rows[0]
  86.     name = rows[1]
  87.     password = rows[2]
  88.     # if there is no cookies at all add to array to reinitialize session
  89.     if not email in cookies:
  90.       broken_sessions.append({"email": email, "password": password})
  91.     # else check if session is still valid
  92.     else:
  93.       r = requests.get("http://battlelog.battlefield.com/bf4/launcher/reserveslotbygameid/1/1/1/1/1/", cookies={"beaker.session.id": cookies[email]})
  94.       if "Logged out" in r.text:
  95.         print("Expired session data for account with name", name)
  96.         broken_sessions.append({"email": email, "password": password})
  97.  
  98.   if broken_sessions:
  99.     print("Getting session data for " +  str(len(broken_sessions)) + " accounts.")
  100.     result = POOL.map(lambda account: get_session(account["email"], account["password"], 0), broken_sessions)
  101.     POOL.close()
  102.     POOL.join()
  103.     for item in result:
  104.       key = item["email"]
  105.       value = item["cookies"]
  106.       cookies[key] = value
  107.     with open("cookies.json", "w") as f:
  108.       f.write(json.dumps(cookies, indent=2))
  109.   else:
  110.     print("All sessions are valid.")
  111.  
  112. def get_session(email, password, retries):
  113.   s = requests.Session()
  114.   # redirect to login page and get execution code
  115.   r = s.get("https://accounts.ea.com/connect/auth?locale=en_US&state=bf4&redire"\
  116.             "ct_uri=https%3A%2F%2Fbattlelog.battlefield.com%2Fsso%2F%3Ftokentyp"\
  117.             "e%3Dcode&response_type=code&client_id=battlelog&display=web%2Flogin")
  118.   execution = urlparse(r.url).query.split("&")[0].replace("execution=", "")
  119.   # send data to login URL and get JS redirect URL
  120.   params = (('execution', execution),)
  121.   data = [
  122.     ('email', email),
  123.     ('password', password),
  124.     ('_rememberMe', 'on'),
  125.     ('rememberMe', 'on'),
  126.     ('_eventId', 'submit'),
  127.   ]
  128.   r = s.post('https://signin.ea.com/p/web2/login', params=params,  data=data)
  129.   redirect_url = re.findall(r'var.*?=\s*(.*?);', r.text, re.DOTALL | re.MULTILINE)[1].replace('\'', "") + "&_eventId=end"
  130.   # this request will redirect a few times until it reaches battlelog index page
  131.   try:
  132.     r = s.get(redirect_url)
  133.   except:
  134.     if retries > 3:
  135.       return(get_session(email, password, retries))
  136.     else:
  137.       print("Can't get session data for this account: " + email + ":" + password)
  138.   cookies = s.cookies["beaker.session.id"]
  139.   return {"email": email, "cookies": cookies}
  140.  
  141. def verify_cookies():
  142.   # create cookies file if it doesn't exist
  143.   if not os.path.exists("cookies.json"):
  144.     with open("cookies.json", "w+") as f:
  145.       f.write("{}")
  146.       print("New cookies file initialized.")
  147.   try:
  148.     # open cookies file and verify it
  149.     with open("cookies.json", "r+") as f:
  150.       cookies = json.load(f)
  151.   except:
  152.     raise Exception("Your cookies file is corrupted. Delete it and restart program to reinitialize or fix it manually(JSON format is invalid).")
  153.   return cookies
  154.  
  155. def main():
  156.   accounts = list(csv.reader(open("accounts.csv")))
  157.   servers = list(csv.reader(open("servers.csv")))
  158.   cookies = verify_cookies()
  159.   args = parse_args()
  160.   if(args.verify):
  161.     verify_sessions(accounts, cookies)
  162.   tick_thread = tick(servers)
  163.   tick_thread.start()
  164.   seed(accounts, servers, cookies)
  165.  
  166.  
  167. if __name__ == '__main__':
  168.   main()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top