Initial code commit

master
Bradlee Speice 2017-03-19 02:16:29 -04:00
commit a7884d5fe0
13 changed files with 215 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
Playwithfriends.egg-info/
.eggs/

4
.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="false" project-jdk-name="Python 3.5.2+ (/usr/bin/python3.5)" project-jdk-type="Python SDK" />
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/playwithfriends.iml" filepath="$PROJECT_DIR$/.idea/playwithfriends.iml" />
</modules>
</component>
</project>

17
.idea/playwithfriends.iml Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="Python" name="Python">
<configuration sdkName="" />
</facet>
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/playwithfriends" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tests" isTestSource="true" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

1
playwithfriends/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
api_key.txt

View File

@ -0,0 +1,2 @@
__version__ = '0.0.1'
__release = '0.0.1'

View File

@ -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}

108
playwithfriends/steam.py Normal file
View File

@ -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
}
)

18
setup.py Normal file
View File

@ -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'
)

0
tests/__init__.py Normal file
View File

View File

@ -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())

21
tests/test_steam.py Normal file
View File

@ -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)