Ejemplo n.º 1
0
def setup():
    client = Client(client_id)
    client.start()
    auth = client.authorize(client_id, ['rpc'])
    client.close()
    rpcToken = auth["data"]["code"]

    if rpcToken != None:
        headers = {
            "rpcToken": rpcToken,
        }
        user_object = requests.get("https://rich-presence-api.glitch.me/setup",
                                   headers=headers)
        if user_object.status_code == 200:
            user_json = user_object.json()

            with open(os.path.abspath(os.getcwd()) + '\data.json',
                      'w') as outfile:
                json.dump(user_json, outfile)

            return user_json
        else:
            raise RuntimeWarning("One or more of the apis errored.")
    else:
        raise RuntimeWarning("Error when obtaining RPC token.")
Ejemplo n.º 2
0
def main():
    print("Starting RPC...")
    rpc = RPC(673133177274892290)
    rpc.start()

    try:
        start = None
        end = None
        pos = []
        est = "None"
        state = 0

        sys = system()  # Find Minecraft game dir
        if sys == "Windows":
            file = Path("AppData") / "Roaming" / ".minecraft"
        elif sys == "Darwin":
            file = Path("Library") / "Application Support" / "minecraft"
        elif sys == "Linux":
            file = ".minecraft"
        else:
            print("Unknown system, unable to find game dir")
        file = Path.home() / file / "logs" / "latest.log"

        with open(file
                  ) as f:  # TODO: Read previous logs if data is insufficient.
            while True:
                line = f.readline()
                if line:
                    text = line[11:-1]
                    try:
                        t = datetime.combine(date.today(),
                                             time.fromisoformat(
                                                 line[1:9])).timestamp()
                        if text.startswith("[main/INFO]: Connecting to "):
                            end = None
                            est = "None"
                            state = 1
                        elif text == "[main/INFO]: [CHAT] 2b2t is full":
                            start = int(t)
                            pos = []
                            state = 2
                        elif text.startswith(
                                "[main/INFO]: [CHAT] Position in queue: "):
                            p = int(text[39:])
                            pos.append((t, p))
                            if len(pos) == 61:
                                pos[0:1] = []

                            if len(pos) == 60:
                                if pos[0][1] == p:
                                    est = "never"
                                else:
                                    est = ""
                                    seconds = (t - pos[0][0]) / (
                                        pos[0][1] - p
                                    )  # FIXME: Time is negative when you're a time traveller.
                                    seconds = int(
                                        seconds *
                                        p)  # TODO: Rework the estimations.
                                    days = seconds // 86400
                                    if days:
                                        est += f"{days}d "
                                    hours = seconds % 86400 // 3600
                                    if hours:
                                        est += f"{hours}h "
                                    est += f"{seconds % 3600 // 60}m"

                            if state == 2:
                                state = 3
                        elif (text ==
                              "[main/INFO]: [CHAT] Connecting to the server..."
                              or text.startswith("[main/INFO]: Loaded ")):
                            pos = []
                            start = int(t)
                            state = 4
                        elif text.startswith(
                                "[main/INFO]: [CHAT] [SERVER] Server restarting in "
                        ):
                            if text.endswith(" minutes..."):
                                end = int(t) + 600 * int(text[50:-12])
                            elif text.endswith(" seconds..."):
                                end = int(t) + 10 * int(text[50:-11])
                            elif text.endswith(" second..."):
                                end = int(t) + 10 * int(text[50:-10])
                            state = 5
                        elif text == "[main/INFO]: Stopping!":
                            print("Hi!")
                            break
                    except ValueError:
                        pass
                else:
                    if state == 1:
                        rpc.set_activity(
                            large_image="image",
                            large_text="2b2t.org",
                            details="Connecting...",
                        )
                    elif state == 2:
                        rpc.set_activity(
                            large_image="image",
                            large_text="2b2t.org",
                            details="Waiting in queue",
                            start=start,
                        )
                    elif state == 3:
                        rpc.set_activity(
                            large_image="image",
                            large_text="2b2t.org",
                            details=f"Position in queue: {pos[-1][1]}",
                            state="Estimated time: " + est,
                            start=start,
                        )
                        sleep(10)
                    elif state == 4:
                        rpc.set_activity(
                            large_image="image",
                            large_text="2b2t.org",
                            details="Playing",
                            start=start,
                        )
                    elif state == 5:
                        rpc.set_activity(
                            large_image="image",
                            large_text="2b2t.org",
                            details=f"Waiting for restart...",
                            state=f"Position in queue: {pos[-1][1]}",
                            end=end,
                        )
                    if stat(f.fileno()).st_nlink == 0:
                        f.close()
                        f = open(file)
    except KeyboardInterrupt:
        pass
    finally:
        print("\nStopping RPC...")
        rpc.close()
Ejemplo n.º 3
0
def main():
    def isDiscordRunning():
        counter = 0
        r = os.popen('tasklist /v').read().strip().split('\n')
        for i in range(len(r)):
            s = r[i]
            if "Discord.exe" in r[i]:
                counter = counter + 1
        if counter >= 4:
            return True
        else:
            return False

    client_id = "696685746832277534"

    previous = {
        "details": None,
        "state": None,
        "image": None,
        "gameSize": None,
        "gameId": None,
    }
    start = None
    activityCleared = True

    client = None
    ClientClosed = True

    try:
        with open(os.path.abspath(os.getcwd()) + '\data.json') as json_file:
            data = json.load(json_file)
    except FileNotFoundError:
        data = setup()

    discordToken = data["discordToken"]
    robloxId = data["robloxId"]

    while True:
        while isDiscordRunning() == True:
            if ClientClosed == True:
                client = Client(client_id)
                client.start()
                client.authenticate(discordToken)
                ClientClosed = False

            headers = {
                "previous": json.dumps(previous),
                "robloxId": str(robloxId),
            }
            rawData = requests.get(
                "https://rich-presence-api.glitch.me/getStatus",
                headers=headers)
            if rawData.status_code == 200:
                data = rawData.json()
                details = data["details"]
                state = data["state"]
                image = data["image"]
                gameSize = data["gameSize"]
                gameId = data["gameId"]

                if details == None:
                    if activityCleared == False:
                        client.clear_activity()
                        activityCleared = True
                    else:
                        time.sleep(5)
                else:
                    if data != previous:
                        if details != previous["details"] or state != previous[
                                "state"] or gameId != previous["gameId"]:
                            start = round(time.time())
                        if details == "Playing":
                            client.set_activity(
                                state=state,
                                details=details,
                                start=start,
                                large_image=image,
                                party_size=gameSize,
                                large_text="Made by Tweakified")
                        else:
                            client.set_activity(
                                state=state,
                                details=details,
                                start=start,
                                large_image=image,
                                large_text="Made by Tweakified")
                        activityCleared = False
                        time.sleep(15)
                    else:
                        time.sleep(5)
                previous = data

        if ClientClosed == False:
            if client != None:
                client.close()
                ClientClosed = True