File: device_id_auth_trophies.py

package info (click to toggle)
python-asyncprawcore 3.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,328 kB
  • sloc: python: 2,224; makefile: 4
file content (41 lines) | stat: -rwxr-xr-x 1,100 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3

"""Example program that outputs a user's list of trophies.

This program demonstrates the use of ``asyncprawcore.DeviceIDAuthorizer``.

"""

import asyncio
import os
import sys

import asyncprawcore


async def main():
    """Provide the program's entry point when directly executed."""
    if len(sys.argv) != 2:
        print(f"Usage: {sys.argv[0]} USERNAME")
        return 1

    authenticator = asyncprawcore.UntrustedAuthenticator(
        asyncprawcore.Requestor("asyncprawcore_device_id_auth_example"),
        os.environ["PRAWCORE_CLIENT_ID"],
    )
    authorizer = asyncprawcore.DeviceIDAuthorizer(authenticator)
    await authorizer.refresh()

    user = sys.argv[1]
    async with asyncprawcore.session(authorizer) as session:
        data = await session.request("GET", f"/api/v1/user/{user}/trophies")

    for trophy in data["data"]["trophies"]:
        description = trophy["data"]["description"]
        print(trophy["data"]["name"] + (f" ({description})" if description else ""))

    return 0


if __name__ == "__main__":
    sys.exit(asyncio.run(main()))