Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # REDDUIT DEFENDER
- # Defends core flag for https://www.reddit.com/r/SwissNeutralityZone/
- # /u/buyacasha, adapted from /u/voltasalt
- import sys
- import time
- import requests
- import numpy
- from requests.adapters import HTTPAdapter
- #Arguments
- username = sys.argv[1]
- password = sys.argv[2]
- #Consts
- originX = 391
- originY = 491
- width = 24
- height = 24
- red = 5
- white = 0
- flagMatrix = numpy.zeros((24, 24))
- flagMatrix.fill(red)
- for x in range(4, width - 4 - 1):
- for y in range(10, 14):
- flagMatrix[x, y] = white
- for x in range(10, 14):
- for y in range(4, height - 4 - 1):
- flagMatrix[x, y] = white
- print(flagMatrix)
- print("\n\nFor the Redduit\n\n")
- s = requests.Session()
- s.mount('https://www.reddit.com', HTTPAdapter(max_retries=5))
- s.headers["User-Agent"] = "RedduitDefence"
- r = s.post("https://www.reddit.com/api/login/{}".format(username),
- data={"user": username, "passwd": password, "api_type": "json"})
- s.headers['x-modhash'] = r.json()["json"]["data"]["modhash"]
- def place_pixel(ax, ay, new_color):
- while True:
- r = s.get("http://reddit.com/api/place/pixel.json?x={}&y={}".format(ax, ay), timeout=5)
- if r.status_code == 200:
- data = r.json()
- break
- else:
- print("ERROR: ", r, r.text)
- time.sleep(5)
- old_color = data["color"] if "color" in data else 0
- if old_color == new_color:
- print("{},{} OK {}".format(ax, ay, new_color))
- else:
- print("Placing color #{} at {},{}".format(new_color, ax, ay))
- r = s.post("https://www.reddit.com/api/place/draw.json",
- data={"x": str(ax), "y": str(ay), "color": str(int(new_color))})
- print(r.json())
- secs = float(r.json()["wait_seconds"])
- if "error" not in r.json():
- print("Placed color - waiting {} seconds".format(secs))
- else:
- print("Cooldown already active - waiting {} seconds".format(int(secs)))
- time.sleep(secs + 2)
- if "error" in r.json():
- place_pixel(ax, ay, new_color)
- while True:
- print("Iterating over Swiss flag...")
- for x in range(originX, originX + width):
- for y in range(originY, originY + height):
- pixel = flagMatrix[x - originX, y - originY]
- place_pixel(x, y, pixel)
- print("Yay, Swiss flag complete. Restarting at the beginning..")
Add Comment
Please, Sign In to add comment