From a7884d5fe0cf63bbcff577fe16a0a20e35521cc5 Mon Sep 17 00:00:00 2001 From: Bradlee Speice Date: Sun, 19 Mar 2017 02:16:29 -0400 Subject: [PATCH] Initial code commit --- .gitignore | 2 + .idea/misc.xml | 4 ++ .idea/modules.xml | 8 +++ .idea/playwithfriends.iml | 17 +++++ .idea/vcs.xml | 6 ++ playwithfriends/.gitignore | 1 + playwithfriends/__init__.py | 2 + playwithfriends/intersections.py | 12 ++++ playwithfriends/steam.py | 108 +++++++++++++++++++++++++++++++ setup.py | 18 ++++++ tests/__init__.py | 0 tests/test_intersections.py | 16 +++++ tests/test_steam.py | 21 ++++++ 13 files changed, 215 insertions(+) create mode 100644 .gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/playwithfriends.iml create mode 100644 .idea/vcs.xml create mode 100644 playwithfriends/.gitignore create mode 100644 playwithfriends/__init__.py create mode 100644 playwithfriends/intersections.py create mode 100644 playwithfriends/steam.py create mode 100644 setup.py create mode 100644 tests/__init__.py create mode 100644 tests/test_intersections.py create mode 100644 tests/test_steam.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f7a77ad --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Playwithfriends.egg-info/ +.eggs/ \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3e2aac3 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..e27c638 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/playwithfriends.iml b/.idea/playwithfriends.iml new file mode 100644 index 0000000..69bf76c --- /dev/null +++ b/.idea/playwithfriends.iml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/playwithfriends/.gitignore b/playwithfriends/.gitignore new file mode 100644 index 0000000..9de317b --- /dev/null +++ b/playwithfriends/.gitignore @@ -0,0 +1 @@ +api_key.txt diff --git a/playwithfriends/__init__.py b/playwithfriends/__init__.py new file mode 100644 index 0000000..8521cce --- /dev/null +++ b/playwithfriends/__init__.py @@ -0,0 +1,2 @@ +__version__ = '0.0.1' +__release = '0.0.1' diff --git a/playwithfriends/intersections.py b/playwithfriends/intersections.py new file mode 100644 index 0000000..d4feaad --- /dev/null +++ b/playwithfriends/intersections.py @@ -0,0 +1,12 @@ +from playwithfriends import steam + + +def intersecting_games(steamid_left, steamid_right): + api = steam.SteamAPI() + + games_left = api.get_games(steamid_left) + games_right = api.get_games(steamid_right) + + ids_mutual = set(games_left.keys()).intersection(games_right.keys()) + + return {game_id: games_left[game_id] for game_id in ids_mutual} diff --git a/playwithfriends/steam.py b/playwithfriends/steam.py new file mode 100644 index 0000000..8c3a394 --- /dev/null +++ b/playwithfriends/steam.py @@ -0,0 +1,108 @@ +import requests +from enum import Enum +from os import path +from copy import deepcopy +import logging + + +_base_url = 'http://api.steampowered.com' + + +class WebAPI(Enum): + news = 'ISteamNews' + user_stats = 'ISteamUserStats' + user = 'ISteamUser' + player_service = 'IPlayerService' + + +class SteamAPI(object): + + def __init__(self): + file_dir = path.abspath(path.dirname(__file__)) + api_file = path.join(file_dir, 'api_key.txt') + with open(api_file, 'r') as handle: + self.api_key = handle.read().strip() + + def _make_request(self, api, function, version, extra_params): + full_params = deepcopy(extra_params) + full_params['key'] = self.api_key + request_url = '/'.join([_base_url, api.value, function, version]) + '/' + logging.debug('GET: {}'.format(request_url)) + response = requests.get( + request_url, + params=full_params + ) + logging.debug('Return code: {}'.format(response.status_code)) + return response.json() + + def get_friends(self, steam_id): + """ + Return the steam_id's of all users this user is friends with + + :param steam_id: ID for the user we are looking up + :type steam_id: str + :return: List of all id's this user is friends with + :rtype: list[str] + """ + friends_list = self._make_request( + WebAPI.user, + 'GetFriendList', + 'v0001', + { + 'steamid': steam_id + })['friendslist'] + + return [ + f['steamid'] for f in friends_list['friends'] + ] + + def get_games(self, steam_id): + """ + Get all the appid's for a user that they can play. Structure + for return is a dict with `appid: name` values. + + :param steam_id: + :return: List of appid's + :rtype: dict + """ + games = self._make_request( + WebAPI.player_service, + 'GetOwnedGames', + 'v0001', + { + 'steamid': steam_id, + 'include_appinfo': 1, + 'include_played_free_games': 1 + })['response']['games'] + + return {g['appid']: g['name'] for g in games} + + def get_recent_games(self, steam_id): + """ + Get all appid's that a user has recently played + + :param steam_id: ID for a steam user + :type steam_id: str + :return: List of appids the user has recently played + """ + games = self._make_request( + WebAPI.player_service, + 'GetRecentlyPlayedGames', + 'v0001', + { + 'steamid': steam_id + } + )['response']['games'] + + return [g['appid'] for g in games] + + def get_player_summary(self, steam_id): + + return self._make_request( + WebAPI.user, + 'GetPlayerSummaries', + 'v0002', + { + 'steamids': steam_id + } + ) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..162ef59 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +from setuptools import setup, find_packages + +from playwithfriends import __version__ + +print(find_packages()) + +setup( + name='Playwithfriends', + version=__version__, + packages=find_packages(), + install_requires=[ + 'requests >= 2.10.0', + 'bottle >= 0.12.7' + ], + author='Bradlee Speice', + author_email='bradlee.speice@gmail.com', + description='Figure out what games you and your friends should play' +) diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_intersections.py b/tests/test_intersections.py new file mode 100644 index 0000000..7767a0c --- /dev/null +++ b/tests/test_intersections.py @@ -0,0 +1,16 @@ +from unittest import TestCase + +from playwithfriends import intersections + + +class TestIntersections(TestCase): + + def test_xannort_shares_project_cars(self): + steam_id_left = '76561198020882912' + steam_id_right = '76561198069992619' + + inter_games = intersections.intersecting_games( + steamid_left=steam_id_left, steamid_right=steam_id_right + ) + + self.assertIn('Project CARS', inter_games.values()) diff --git a/tests/test_steam.py b/tests/test_steam.py new file mode 100644 index 0000000..23b8e6c --- /dev/null +++ b/tests/test_steam.py @@ -0,0 +1,21 @@ +from unittest import TestCase + +from playwithfriends import steam + + +class SteamTest(TestCase): + + def test_returns_friends_list(self): + steam_id = '76561198020882912' + friends = steam.SteamAPI().get_friends(steam_id) + self.assertTrue(len(friends) > 0) + + def test_I_own_games(self): + steam_id = '76561198020882912' + games = steam.SteamAPI().get_games(steam_id) + self.assertTrue(len(games) > 0) + + def test_Ive_recently_played_games(self): + steam_id = '76561198020882912' + games = steam.SteamAPI().get_recent_games(steam_id) + self.assertTrue(len(games) > 0)