Python新手求助:如何启动GitHub上的Riot-Watcher项目?
Hey there! Let's walk through this step by step—since you're new to Python and Riot-Watcher, I'll keep things straightforward so you can get up and running quickly.
Before you can use Riot-Watcher, you need an API key from Riot Games. This is how the library authenticates your requests to Riot's servers. You can search for "Riot Developer API" to find their official portal, sign up for an account, and generate your own key. Keep this key safe—don't share it publicly!
Riot-Watcher isn't a standalone app you "start" like a desktop program—it's a Python library that gives you tools to build your own scripts for interacting with League of Legends data. The "Using It" section in the repo has snippets that show you how to call its core functions. Let's turn those snippets into a complete, runnable example:
A Full Example Script
Copy this into a new file (name it something like lol_test.py):
# Import the key parts of Riot-Watcher we need from riotwatcher import LolWatcher, ApiError # Replace this with your actual API key from Riot's developer portal MY_API_KEY = "your-personal-api-key-here" # Initialize the watcher with your key to connect to Riot's API lol_watcher = LolWatcher(MY_API_KEY) # Pick your region (common options: "na1" for North America, "euw1" for EU West, "kr" for Korea) region = "na1" # Replace with your in-game summoner name summoner_name = "YourSummonerName" try: # Call the API to get summoner details by their username summoner_data = lol_watcher.summoner.by_name(region, summoner_name) # Print out some useful info from the response print(f"Summoner ID: {summoner_data['id']}") print(f"Summoner Level: {summoner_data['summonerLevel']}") print(f"Account ID: {summoner_data['accountId']}") except ApiError as err: # Handle common API errors gracefully if err.response.status_code == 404: print("Oops, that summoner doesn't exist! Double-check the name.") elif err.response.status_code == 429: print("Slow down! You're making requests too quickly—wait a minute and try again.") else: # For other unexpected errors, let Python show the details raise
What Each Part Does
- Importing the library:
from riotwatcher import LolWatcher, ApiErrorbrings in the main tool (LolWatcher) and error handler (ApiError) we need to work with the API. - Initializing the watcher:
LolWatcher(MY_API_KEY)creates a authenticated connection to Riot's servers using your unique key. - Region setting: Riot's API is split into regional servers—make sure you use the code that matches the server you play on.
- Calling the API:
lol_watcher.summoner.by_name(region, summoner_name)fetches data for the summoner you specified. - Error handling: The
try/exceptblock catches common issues like a summoner not being found or hitting Riot's request rate limits.
- Save the code above into a file (e.g.,
lol_test.py) on your computer. - Open your terminal or command prompt.
- Navigate to the folder where you saved the file (use
cd path/to/your/folderon Windows/macOS/Linux). - Run the script with:
(Ifpython lol_test.pypythondoesn't work, trypython3 lol_test.py—this depends on how you set up Python 3.6 on your system.)
Riot-Watcher isn't a project you "launch" like an app. It's a library that provides building blocks for you to create your own League of Legends tools—like a script to track your win rate, check champion stats, or pull your recent match history. The examples in the repo are just starting points for you to build on!
内容的提问来源于stack exchange,提问作者dolu28

