Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime, timedelta
- from time import time
- from playsound import playsound
- from PIL import Image, ImageChops, ImageGrab
- from pushbullet import PushBullet
- import configparser
- import operator
- import requests
- import win32api
- import win32con
- import win32gui
- import webbrowser
- import os
- def Avg(lst):
- return sum(lst) / len(lst)
- def enum_cb(hwnd, results):
- winlist.append((hwnd, win32gui.GetWindowText(hwnd)))
- def write(message, pushing=0, add_time=True):
- if add_time:
- m = datetime.now().strftime("%H:%M:%S") + ": " + str(message)
- else:
- m = message
- print(m)
- if pushing >= 2:
- device.push_note("CSGO AUTO ACCEPT", m)
- def click(x, y):
- win32api.SetCursorPos((x, y))
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0)
- win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0)
- def compare_list(l1, l2, relate=operator.le):
- if not l1:
- return False
- l3 = []
- for i, val in enumerate(l1, start=0):
- l3.append(relate(val, l2[i]))
- return all(l3)
- def color_average(image, x1, y1, x2, y2, compare_images=True, org_image=0):
- average = []
- r, g, b = [], [], []
- if compare_images:
- data = ImageChops.difference(org_image, image).getdata()
- else:
- data = image.getdata()
- for y in range(y1, y2 + 1):
- for x in range(x1, x2 + 1):
- r.append(data[y * 2560 + x][0])
- g.append(data[y * 2560 + x][1])
- b.append(data[y * 2560 + x][2])
- average.append(Avg(r))
- average.append(Avg(g))
- average.append(Avg(b))
- return average
- def getScreenShot(window_id):
- win32gui.ShowWindow(window_id, win32con.SW_MAXIMIZE)
- try:
- bbox = win32gui.GetWindowRect(window_id)
- except pywintypes.error:
- return 0
- image = ImageGrab.grab(bbox)
- if not image:
- return 0
- i_width, i_height = image.size
- if i_width != 2650 and i_height != 1440:
- image = image.resize([2560, 1440])
- return image
- def getOldSharecodes(num=-1):
- last_game = open("last_game.txt", "r")
- games = last_game.readlines()
- last_game.close()
- last_game = open("last_game.txt", "w")
- games = games[-200:]
- for i, val in enumerate(games):
- games[i] = "CSGO" + val.strip("\n").split("CSGO")[1]
- last_game.write(games[i]+"\n")
- last_game.close()
- return games[num:]
- def getNewCSGOMatches(game_id):
- sharecodes = []
- next_code = game_id
- last_game = open("last_game.txt", "a")
- while next_code != "n/a":
- URL = "https://api.steampowered.com/ICSGOPlayers_730/GetNextMatchSharingCode/v1?key=" + steam_api_key + "&steamid=" + steam_id + "&steamidkey=" + game_code + "&knowncode=" + game_id
- try:
- next_code = (requests.get(URL).json()["result"]["nextcode"])
- except KeyError:
- write("WRONG GAME_CODE, GAME_ID or STEAM_ID ")
- return 0
- if next_code:
- if next_code != "n/a":
- sharecodes.append(next_code)
- game_id = next_code
- last_game.write(next_code + "\n")
- else:
- write("Found %s Game[s]" % len(sharecodes))
- return sharecodes
- def UpdateCSGOstats(sharecodes):
- if any(sharecodes):
- for i in sharecodes:
- write('Added Sharecode: %s' % i, push_active)
- response = requests.post("https://csgostats.gg/match/upload/ajax", data={'sharecode': i, 'index': '1'})
- info = response.text.split('"msg":"')[1].split("<")[0][:-2].rstrip(" ")
- write("Game %s." % info, pushing=push_active)
- config = configparser.ConfigParser()
- config.read("config.ini")
- steam_api_key = config.get("csgostats.gg", "API Key")
- steam_id = config.get("csgostats.gg", "Steam ID")
- game_code = config.get("csgostats.gg", "Game Code")
- PushBulletDeviceName, PushBulletAPIKey = 0, 0
- screen_width, screen_height = win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1)
- toplist, winlist = [], []
- hwnd = 0
- test_for_live_game, test_for_success, push_active, debugging = False, False, False, False
- accept_avg = []
- img_accept = Image.open("images/accept.jpg")
- img_not_searching = Image.open("images/not_searching.jpg")
- start_time = time()
- write("Ready")
- while True:
- if win32api.GetAsyncKeyState(0x78) & 1: # F9 (ACTIVATE / DEACTIVATE SCRIPT)
- test_for_live_game = not test_for_live_game
- write("TESTING: %s" % test_for_live_game)
- accept_avg = []
- if test_for_live_game:
- playsound('sounds/activated.mp3')
- time_searching = time()
- else:
- playsound('sounds/deactivated.mp3')
- if win32api.GetAsyncKeyState(0x77) & 1: # F8 (ACTIVATE / DEACTIVATE PUSH NOTIFICATION)
- push_active += 1
- if push_active > 2:
- push_active = 0
- push_info = ["not active", "little information", "all information"]
- write("Pushing: %s" % push_info[push_active])
- if not PushBulletDeviceName:
- PushBulletDeviceName = config.get('Pushbullet', 'DeviceName')
- PushBulletAPIKey = config.get('Pushbullet', 'API Key')
- device = PushBullet(PushBulletAPIKey).get_device(PushBulletDeviceName)
- if win32api.GetAsyncKeyState(0x76) & 1: # F7 Key (UPLOAD NEWEST MATCH)
- UpdateCSGOstats(getNewCSGOMatches(getOldSharecodes()[0]))
- if win32api.GetAsyncKeyState(0x75) & 1: # F6 Key (GET INFO ON LAST X MATCHES)
- try:
- last_x_matches = abs(int(input("Last X Matches: ")))
- except ValueError:
- last_x_matches = 1
- UpdateCSGOstats(getOldSharecodes(num=last_x_matches * -1))
- if win32api.GetAsyncKeyState(0x74) & 1: # F5 Key (GET INFO ON NEWEST MATCH)
- UpdateCSGOstats(getOldSharecodes())
- if win32api.GetAsyncKeyState(0x7C) & 1: # F13 Key (OPEN WEBBROWSER ON LIVE TAB)
- webbrowser.open_new_tab("https://csgostats.gg/player/" + steam_id + "#/live")
- if win32api.GetAsyncKeyState(0x24) & 1: # POS1/HOME Key
- write("Exiting Script")
- break
- winlist = []
- win32gui.EnumWindows(enum_cb, toplist)
- csgo = [(hwnd, title) for hwnd, title in winlist if 'counter-strike: global offensive' in title.lower()]
- if not csgo:
- continue
- hwnd = csgo[0][0]
- # TESTING HERE
- if win32api.GetAsyncKeyState(0x73) & 1: # F5, TEST CODE
- write("Executing TestCode")
- test_for_live_game = False
- debugging = not debugging
- test_for_success = debugging
- print(debugging, test_for_success)
- start_time = time()
- # TESTING ENDS HERE
- if test_for_live_game:
- if time() - start_time < 4:
- continue
- start_time = time()
- img = getScreenShot(hwnd)
- if not img:
- continue
- accept_avg = color_average(img, 1265, 760, 1295, 785, org_image=img_accept)
- if compare_list(accept_avg, [15, 30, 15]):
- write("Trying to Accept", push_active)
- test_for_success = True
- test_for_live_game = False
- accept_avg = []
- for _ in range(5):
- click(int(screen_width / 2), int(screen_height / 1.78))
- # sleep(0.5)
- # pass
- write("Trying to catch a loading map")
- playsound('sounds/accept_found.mp3')
- start_time = time()
- if test_for_success:
- if time() - start_time < 40:
- img = getScreenShot(hwnd)
- success_avg = color_average(img, 467, 1409, 1300, 1417, compare_images=False)
- searching_avg = color_average(img, 2435, 70, 2550, 100, org_image=img_accept)
- not_searching_avg = color_average(img, 2435, 55, 2550, 100, org_image=img_not_searching)
- solo_not_searching_avg = color_average(img, 2435, 115, 2555, 135, org_image=img_not_searching)
- no_success_truth = [compare_list(searching_avg, [1, 15, 5]), compare_list(not_searching_avg, [8, 9, 9]), compare_list(solo_not_searching_avg, [1, 1.5, 2])]
- if debugging:
- print(searching_avg)
- continue
- if no_success_truth[0]:
- write(searching_avg)
- # print(not_searching_avg)
- # print(solo_not_searching_avg)
- # print(no_success_truth)
- continue
- if compare_list(success_avg, [17, 100, 150], relate=operator.ge):
- write("Game should have started", push_active+1)
- write("Took %s s since pressing accept." % str(timedelta(seconds=int(time() - start_time))), pushing=push_active, add_time=False)
- write("Took %s s since trying to find a game." % str(timedelta(seconds=int(time() - time_searching))), pushing=push_active, add_time=False)
- test_for_success = False
- playsound('sounds/done_testing.mp3')
- if any(no_success_truth):
- write("Game doesnt seem to have started. Continuing to search for accept Button!", push_active)
- write("Took: %s " % str(timedelta(seconds=int(time() - start_time))), pushing=push_active, add_time=False)
- playsound('sounds/back_to_testing.mp3')
- test_for_success = False
- test_for_live_game = True
- time_searching = time()
- else:
- write("Unknown Error")
- test_for_success = False
- print(success_avg)
- print(searching_avg)
- print(not_searching_avg)
- print(solo_not_searching_avg)
- playsound('sounds/fail.mp3')
- img.save(os.path.expanduser("~") + '\\Unknown Error.png')
Add Comment
Please, Sign In to add comment