Conversating with AI for Kodi Development
#1
Hello everyone!

I just wanted to share this conversation I had with ChatGPT. It was pretty amazing. My thought is it may be a useful tool in software development (kodi specific).

I'll just dive right into it...



ME:
"I need a python script that retrieves steam user profile information, including profile name, avatar, level, achievements, games owned, average game completion percentage"



CHATGPT:
"You can use the Steam Web API to retrieve information about a Steam user's profile. The API allows you to query for information such as a user's profile name, avatar, level, and games owned. You can also use the API to retrieve a user's achievement information, though it does not provide average game completion percentage directly.

Here's an example script that shows how to use the Steam Web API to retrieve a user's profile information:

python:
import requests
import json

# Replace YOUR_API_KEY with your Steam Web API key
API_KEY = "YOUR_API_KEY"

# Replace YOUR_STEAM_ID with the Steam ID of the user whose information you want to retrieve
STEAM_ID = "YOUR_STEAM_ID"

# Make a request to the Steam Web API to retrieve a user's profile information
url = f"http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={API_KEY}&steamids={STEAM_ID}"
response = requests.get(url)

# Parse the JSON data returned by the API
data = json.loads(response.text)

# Print the profile information for the user
print("Profile Name:", data['response']['players'][0]['personaname'])
print("Avatar:", data['response']['players'][0]['avatar'])
print("Level:", data['response']['players'][0]['player_level'])

# Next Step, To get the player's owned games, you have to make another request
# Make a request to the Steam Web API to retrieve the games owned by the user
url = f"http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={API_KEY}&steamid={STEAM_ID}&format=json"
response = requests.get(url)
data = json.loads(response.text)
print("Games Owned:")
for game in data['response']['games']:
    print("-", game['name'])
    
#And for the achievements, you will make another request
# Make a request to the Steam Web API to retrieve the games owned by the user
url = f"http://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/?appid={APP_ID}&key={API_KEY}&steamid={STEAM_ID}"
response = requests.get(url)
data = json.loads(response.text)
print("Achievements:")
for achievement in data['playerstats']['achievements']:
    print("-", achievement['name'])

Note that the above script uses the requests library to make HTTP requests to the Steam Web API, and the json library to parse the JSON data returned by the API.
Also note that you need a Steam Web API key in order to access the API. You can get one by visiting https://steamcommunity.com/dev/apikey and for average game completion percentage you need to do some calculations by your own, you can use the playtime_forever and playtime_2weeks field and divide it by the total playtime of the game"



ME:
"can you make this into an addon for kodi leia"



CHATGPT:
"Yes, it is possible to create a Kodi addon that uses the Steam Web API to retrieve information about a Steam user's profile.

Here's an example of how you might go about creating such an addon:

1. First, you'll need to create a new addon directory in the addons folder of your Kodi installation. The directory should have a unique name and should contain the following files:
addon.xml: This file contains metadata about the addon, such as its name, version, and a brief description.
script.py: This file will contain the code for your addon.

2. In the addon.xml file, include the following basic information :

xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<addon id="YOUR_ADDON_ID" version="1.0.0" name="Steam User Profile" provider-name="YOUR_NAME">
    <requires>
        <import addon="xbmc.python" version="2.19.0"/>
    </requires>
    <extension point="xbmc.python.script" library="script.py"/>
</addon>
3. In the script.py file, include the python code that use the steam web API to retrieve the information about the user profile as I provided before, and you can use the kodi python built-in modules such as xbmcgui, xbmcplugin to create GUI in order to prompt the user to enter Steam ID, the API Key and make it easy to interact with.

4. once you have completed the above steps, you can then install the addon by placing the directory containing the files in the addons folder in Kodi and then enable it from the Kodi addons menu.
Please note that, this is a high-level overview of the process and there may be additional steps and considerations involved in creating a fully-functional Kodi addon. Also, this example code is provided as-is and may not work as expected."



ME:
"can you make this work for the default skin, confluence"



CHATGPT:
"Yes, it is possible to make the addon you created work with the default Kodi skin "Confluence."

Here are a few things you might consider when making the addon work with Confluence:

1. Creating a menu item for your addon in Confluence's main menu. To do this, you would need to add a new entry to the default.py file in the skin.confluence.resources directory. This entry should specify the label for the menu item, the icon to use, and the script to execute when the menu item is selected.

2. Creating an entry point for your addon in the Confluence home screen. To do this, you would need to create a new section in the home.xml file in the skin.confluence.720p directory, and define the properties of the addon like its image, label, etc..

3. Creating new dialogs for your addon. To do this, you need to create new XML files in the skin.confluence.dialogs directory, and use the built-in dialog classes from the Kodi UI library to define the layout and behavior of the dialogs.

4. Using the proper images and icons that match the look and feel of the confluence skin.

Keep in mind that this can be a complex task and it may require a bit of experience with Kodi skinning and XML. And also the above steps i mentioned are only the basic steps, there could be other adjustments that you need to make to fit the addon properly with the Confluence skin
You can find the documentation for Kodi skinning and the Confluence skin on the Kodi Wiki which could be a good resource to check before you start."



ME:
"can the script be optimized"



CHATGPT:
"Sure, here is an example of how you might optimize the script to retrieve Steam user profile information and games owned:

python:
import requests
import json
import os
from datetime import datetime, timedelta

# Replace YOUR_API_KEY with your Steam Web API key
API_KEY = "YOUR_API_KEY"

# Replace YOUR_STEAM_ID with the Steam ID of the user whose information you want to retrieve
STEAM_ID = "YOUR_STEAM_ID"

# cache data for 60 minutes
CACHE_TIME = 60 * 60

# define the folder where the cache will be stored
CACHE_DIR = "cache"

def make_request(url):
    """
    make request to the Steam web api,
    if the file exist in the cache folder and the cache is not expired
    the function will return the content of the file
    """
    cache_path = os.path.join(CACHE_DIR, url.split("/")[-1])
    if os.path.exists(cache_path):
        with open(cache_path, "r") as f:
            data = json.load(f)
            if (datetime.now() - datetime.fromtimestamp(data["timestamp"])).total_seconds() < CACHE_TIME:
                return data["data"]
    try:
        response = requests.get(url)
        response.raise_for_status()
    except requests.exceptions.RequestException as e:
        # handle errors here
        raise e
    data = json.loads(response.text)
    # save the response in the cache folder
    os.makedirs(CACHE_DIR, exist_ok=True)
    with open(cache_path, "w") as f:
        json.dump({"timestamp": datetime.now().timestamp(), "data": data}, f)
    return data

# Make a request to the Steam Web API to retrieve a user's profile information
url = f"http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key={API_KEY}&steamids={STEAM_ID}"
data = make_request(url)

# Print the profile information for the user
profile_data = data['response']['players'][0]
print("Profile Name:", profile_data['personaname'])
print("Avatar:", profile_data['avatar'])
print("Level:", profile_data['player_level'])

# Make a request to the Steam Web API to retrieve the games owned by the user
url = f"http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key={API_KEY}&steamid={STEAM_ID}&format=json"
data = make_request(url)
print("Games Owned:")
for game in data['response']['games']:
    print("-", game['name'])
In this script I've added caching, using a function called 'make_request' which will check if the response exist in the cache folder and if it's not expired, the function will return the content of the file. Also, I've added a global variable called 'CACHE_TIME' that defines the time that the cache"


ME:
"can you make the necessary dialog XML for confluence"



CHATGPT:
"Sure, here is an example of how you might create a dialog in XML for Confluence:

xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Dialog to show the user's Steam profile information -->
<window id="steam_profile_dialog"
    position="center"
    width="720"
    height="450">
    <controls>
        <control type="image" id="img_avatar" width="250" height="250" position="20,20" aspectratio="keep" />
        <control type="label" id="lbl_profile_name" width="450" height="40" position="290,20" font="MediumBold" textcolor="#ffffff"/>
        <control type="label" id="lbl_level" width="450" height="40" position="290,70" font="MediumBold" textcolor="#ffffff"/>
        <control type="list" id="list_games" width="660" height="320" position="30,120">
            <item>
                <label>Game Name</label>
                <onclick>RunScript(script.py, action=play_game, game_id=%listitemid%)</onclick>
            </item>
        </control>
    </controls>
</window>
This is an example of how you might structure a dialog in XML for displaying a Steam user's profile information.

The dialog has an img_avatar control for displaying the user's avatar, lbl_profile_name and lbl_level for displaying the user's profile name and level respectively, and list_games for displaying the games that the user owns. And as you see, there is a list item which has a label and an onclick action that run the script.py and pass an additional parameter game_id, this will allow you to handle the click on the game item.

Please note that this is just an example and the actual structure and layout of the dialog may vary depending on your needs and preferences. You will also need to add this dialog xml file in the skin.confluence.dialogs folder and reference it properly in your script.py file. And don't forget to include the path of your image as the avatar src.

Keep in mind that creating a dialog in XML for Confluence is a complex task and it may require a bit of experience with Kodi skinning and XML. And also the above example is provided as-is and may not work as expected, it's meant to be used as a starting point."




This is where our "conversation" ended and now I'm just left scratching my head and thinking of all the possibilities.
Image
Reply
#2
Oh boy.

Will this be the end of script kids or just the beginning of a younger generation.

Very cool, thanks for sharing.
Reply

Logout Mark Read Team Forum Stats Members Help
Conversating with AI for Kodi Development0