Source code for langchain_community.utilities.steam
"""Util that calls Steam-WebAPI."""fromtypingimportAny,ListfrompydanticimportBaseModel,ConfigDict,model_validatorfromlangchain_community.tools.steam.promptimport(STEAM_GET_GAMES_DETAILS,STEAM_GET_RECOMMENDED_GAMES,)
[docs]classSteamWebAPIWrapper(BaseModel):"""Wrapper for Steam API."""steam:Any=None# for python-steam-api# operations: a list of dictionaries, each representing a specific operation that# can be performed with the APIoperations:List[dict]=[{"mode":"get_game_details","name":"Get Game Details","description":STEAM_GET_GAMES_DETAILS,},{"mode":"get_recommended_games","name":"Get Recommended Games","description":STEAM_GET_RECOMMENDED_GAMES,},]model_config=ConfigDict(extra="forbid",)
[docs]defget_operations(self)->List[dict]:"""Return a list of operations."""returnself.operations
@model_validator(mode="before")@classmethoddefvalidate_environment(cls,values:dict)->Any:"""Validate api key and python package has been configured."""# check if the python package is installedtry:fromsteamimportSteamexceptImportError:raiseImportError("python-steam-api library is not installed. ")try:fromdecoupleimportconfigexceptImportError:raiseImportError("decouple library is not installed. ")# initialize the steam attribute for python-steam-api usageKEY=config("STEAM_KEY")steam=Steam(KEY)values["steam"]=steamreturnvalues
[docs]defparse_to_str(self,details:dict)->str:# For later parsing"""Parse the details result."""result=""forkey,valueindetails.items():result+="The "+str(key)+" is: "+str(value)+"\n"returnresult
[docs]defget_id_link_price(self,games:dict)->dict:"""The response may contain more than one game, so we need to choose the right one and return the id."""game_info={}forappingames["apps"]:game_info["id"]=app["id"]game_info["link"]=app["link"]game_info["price"]=app["price"]breakreturngame_info
[docs]defdetails_of_games(self,name:str)->str:games=self.steam.apps.search_games(name)info_partOne_dict=self.get_id_link_price(games)info_partOne=self.parse_to_str(info_partOne_dict)id=str(info_partOne_dict.get("id"))info_dict=self.steam.apps.get_app_details(id)data=info_dict.get(id).get("data")detailed_description=data.get("detailed_description")# detailed_description contains <li> <br> some other html tags, so we need to# remove themdetailed_description=self.remove_html_tags(detailed_description)supported_languages=info_dict.get(id).get("data").get("supported_languages")info_partTwo=("The summary of the game is: "+detailed_description+"\n"+"The supported languages of the game are: "+supported_languages+"\n")info=info_partOne+info_partTworeturninfo
[docs]defrecommended_games(self,steam_id:str)->str:try:importsteamspypiexceptImportError:raiseImportError("steamspypi library is not installed.")users_games=self.get_users_games(steam_id)result:dict[str,int]={}most_popular_genre=""most_popular_genre_count=0forgameinusers_games["games"]:# type: ignore[call-overload]appid=game["appid"]data_request={"request":"appdetails","appid":appid}genreStore=steamspypi.download(data_request)genreList=genreStore.get("genre","").split(", ")forgenreingenreList:ifgenreinresult:result[genre]+=1else:result[genre]=1ifresult[genre]>most_popular_genre_count:most_popular_genre_count=result[genre]most_popular_genre=genredata_request=dict()data_request["request"]="genre"data_request["genre"]=most_popular_genredata=steamspypi.download(data_request)sorted_data=sorted(data.values(),key=lambdax:x.get("average_forever",0),reverse=True)owned_games=[game["appid"]forgameinusers_games["games"]]# type: ignore[call-overload]remaining_games=[gameforgameinsorted_dataifgame["appid"]notinowned_games]top_5_popular_not_owned=[game["name"]forgameinremaining_games[:5]]returnstr(top_5_popular_not_owned)
[docs]defrun(self,mode:str,game:str)->str:ifmode=="get_games_details":returnself.details_of_games(game)elifmode=="get_recommended_games":returnself.recommended_games(game)else:raiseValueError(f"Invalid mode {mode} for Steam API.")