Skip to content

Commit

Permalink
company api
Browse files Browse the repository at this point in the history
  • Loading branch information
nherbaut committed Mar 18, 2021
1 parent 5ece96b commit 5e9f140
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 64 deletions.
36 changes: 29 additions & 7 deletions README.md
Expand Up @@ -18,9 +18,9 @@ api=S4GAPI("foo","bar")
user_api=api.get_user_api()

#get the users from the API
for u in user_api.get_users():
for u in user_api.get_users():
#for each user, follow the all-thumbnails link by calling the all_thumbnails() function.
for t in u.all_thumbnails()["thumbnails"]:
for t in u.all_thumbnails()["thumbnails"]:
print(f"{u.user.user_id};{t.row};{t.col};{t.video_id};{t.timestamp}")
```

Expand Down Expand Up @@ -63,7 +63,7 @@ Get the current direct schedule, with metadata from the companion platform-api
```python
from s4gpy.s4gpy import S4GAPI
api=S4GAPI(<add your user here>,<add your password here>)
for s in api.get_direct_api().get_direct_schedule():
for s in api.get_direct_api().get_direct_schedule():
try:
imdb_data=s.content().imdb_id()
genres="+".join([g["genre"] for g in imdb_data.data.genres])
Expand All @@ -85,11 +85,11 @@ api=S4GAPI("foo","bar")
user_api=api.get_user_api()

#for each user
for u in user_api.get_users():
for u in user_api.get_users():
#get all the video she watched
watched_videos=[w.video_id for w in u.all_watches().watches]
#for all the thumbnails
for t in u.all_thumbnails()["thumbnails"]:
for t in u.all_thumbnails()["thumbnails"]:
#only dump the informations if the user has wached the video
if t.video_id in watched_videos:
print(f"{u.user.user_id};{t.row};{t.col};{t.video_id};{t.timestamp}")
Expand All @@ -102,7 +102,7 @@ Get the current direct schedule, with metadata from the companion platform-api
```python
from s4gpy.s4gpy import S4GAPI
api=S4GAPI(<add your user here>,<add your password here>)
for s in api.get_direct_api().get_direct_schedule():
for s in api.get_direct_api().get_direct_schedule():
try:
imdb_data=s.content().imdb_id()
genres="+".join([g["genre"] for g in imdb_data.data.genres])
Expand All @@ -120,4 +120,26 @@ Get some credentials for netflix to run a robot run
from s4gpy.s4gpy import S4GAPI
api=S4GAPI("foo","bar")
login, password = api.get_credentials_api().get_credentials("netflix")
```
```


### Company Mapping API

Get/Set the mappings for content and company

```python
from s4gpy.s4gpy import S4GAPI
from s4gpy.api.companyapi import CompanyAPI
from s4gpy.s4gsession import S4GSession
api = S4GAPI("foo","bar")

api.get_company_api().push_company("company1_cc_code",name="company 1 name",link="company 1 link")
api.get_company_api().push_company("company2_cc_code",name="company 2 name",link="company 2 link")
api.get_company_api().push_content("content_code",["company 1 name","company 2 name"])

for company in api.get_company_api().get_companies():
print(f"company code {company.company_id} is {company.company().name}")
for content in api.get_company_api().get_contents():
for company in content.content().companies():
print(f"content {content.content_id} is produced by {company.company_id}")
```
47 changes: 47 additions & 0 deletions data.csv
@@ -0,0 +1,47 @@
is_prime_time,is_prime_space,is_most_proposed,freq,airing_time_hour,title,genres,mean_row,mean_col,median_row,media_col,count
False,False,False,0.008770245182766789,7,Equinox,Drama+Fantasy+Mystery+Thriller,4.275280898876405,4.044943820224719,4.0,4.0,178
False,False,False,0.0003200819409768901,11,Quicksand,Drama,18.833333333333332,6.366666666666666,16.5,3.0,30
False,False,False,0.00019204916458613405,13,World's Most Wanted,Documentary+Crime,4.475409836065574,7.060109289617486,3,5,183
False,False,False,0.00012803277639075603,15,No Gogó do Paulinho,Comedy,6.981132075471698,5.773584905660377,6,5,53
False,False,False,0.00019204916458613405,4,Who Killed Little Gregory?,Documentary+Crime,8.51063829787234,6.035460992907802,5,4,141
False,False,False,0.0008322130465399142,7,Mank,Biography+Comedy+Drama,5.504065040650406,6.959349593495935,5,4,123
False,False,False,0.0005761474937584022,5,Mowgli: Legend of the Jungle,Adventure+Drama+Fantasy,10.33673469387755,7.959183673469388,7.5,5.0,98
False,False,False,0.008898277959157545,9,The Midnight Sky,Drama+Fantasy+Sci-Fi+Thriller,5.123505976095617,2.3446215139442232,4.0,2.0,502
True,False,False,0.00025606555278151206,19,The Woods,Crime+Drama+Mystery+Thriller,5.431972789115647,4.329931972789115,4.0,4.0,294
False,False,False,0.014275654567569298,3,Tiny Pretty Things,Drama+Mystery+Thriller,4.34720908230842,3.975402081362346,4,4,1057
False,False,False,0.0015363933166890724,0,Elite,Crime+Drama+Thriller,6.655765920826162,5.981067125645439,6,4,581
False,False,False,0.012099097368926445,8,Cobra Kai,Action+Comedy+Drama+Sport,5.509635974304069,4.173447537473233,4.0,3.0,934
False,False,False,0.0016644260930798285,4,Sex Education,Comedy+Drama,7.865561694290976,6.867403314917127,7,5,543
False,False,False,0.0009602458229306702,14,Big Mouth,Animation+Comedy+Romance,6.944915254237288,4.262711864406779,5.0,4.0,472
False,False,False,0.0003840983291722681,12,Young Wallander,Crime+Drama+Mystery+Thriller,5.3628318584070795,7.300884955752212,4.0,4.0,226
False,False,False,0.0003840983291722681,17,Ragnarok,Drama+Fantasy,12.970588235294118,5.352941176470588,9.0,2.0,102
False,False,False,0.0014723769284936943,8,Mosul,Action+Drama+War,4.087947882736156,3.550488599348534,3,3,307
False,False,False,0.0006401638819537802,1,The Rain,Sci-Fi+Thriller,6.893305439330544,4.864016736401673,4.0,3.0,478
False,False,False,0.00012803277639075603,4,How to Sell Drugs Online (Fast),Comedy+Crime+Drama+Romance,7.2076502732240435,3.9644808743169397,5.0,3.0,366
False,False,False,0.0008962294347352923,11,Baby,Short+Drama,6.407960199004975,7.437810945273632,4,4,201
False,False,False,0.00044811471736764613,17,Top Boy,Crime+Drama+Thriller,9.069620253164556,5.284810126582278,7.0,5.0,158
False,False,False,0.0003840983291722681,18,Don't Listen,Horror+Thriller,5.047169811320755,5.235849056603773,4.0,4.0,106
False,False,False,0.00044811471736764613,17,The English Game,Drama+History+Sport,12.145348837209303,6.255813953488372,9.0,3.0,172
True,False,False,0.0008322130465399142,23,Suburra: Blood on Rome,Action+Crime,7.59144893111639,7.104513064133017,6,5,421
False,False,False,0.0014723769284936943,0,Barbarians,Action+Adventure+Drama+History,5.105732484076433,3.0063694267515926,4,2,785
False,False,False,0.00044811471736764613,7,The Valhalla Murders,Crime+Drama+Mystery+Thriller,16,6.423076923076923,14.5,4.0,52
False,True,False,0.026310735548300365,11,Bridgerton,Drama+Romance,3.855252918287938,1.332295719844358,4,1,1285
False,False,False,0.0029447538569873886,15,Cable Girls,Drama,5.551229508196721,3.9453551912568305,5.0,3.0,1464
True,False,False,0.00025606555278151206,19,High Seas,Crime+Drama+Mystery,14.829113924050633,8.227848101265822,9.0,4.0,158
True,False,False,0.0017924588694705845,19,Ma Rainey's Black Bottom,Drama+Music,4.298507462686567,6.149253731343284,4,5,67
True,False,False,0.004929261891044107,21,Rose Island,Adventure+Comedy+Drama,4.716666666666667,4.088095238095238,3.0,3.0,420
False,False,False,0.003008770245182767,7,Sweet Home,Drama+Fantasy+Horror+Thriller,4.252964426877471,2.5573122529644268,4,2,253
False,False,False,0.00025606555278151206,14,Vampires,Drama+Fantasy+Horror,8.941176470588236,2.0588235294117645,4,2,17
False,False,False,0.0010242622111260482,13,The Prom,Comedy+Drama+Musical,8.537313432835822,6.992537313432836,5.0,4.0,134
False,False,False,6.401638819537802e-05,17,After Life,Comedy+Drama,8.115702479338843,8.181818181818182,3,4,121
False,False,False,6.401638819537802e-05,2,White Lines,Drama+Mystery,6.3023255813953485,4.828488372093023,5.0,4.0,344
False,False,False,6.401638819537802e-05,7,The Eddy,Drama+Musical,5,7.025316455696203,5,4,79
False,True,False,0.012483195698098713,8,Pieces of a Woman,Drama,3.8505747126436782,5.272030651340996,4,3,261
False,False,False,0.012035080980731068,14,The Crown,Drama+History,4.279254264180881,2.436334787782626,4,2,2521
False,False,False,0.0021125408104474747,16,Offering to the Storm,Crime+Thriller,4.333333333333333,3.4482758620689653,3.0,2.0,174
True,False,False,0.0013443441521029385,19,The Legacy of the Bones,Crime+Thriller,6.169642857142857,6.723214285714286,3.0,5.0,112
True,False,False,0.0005121311055630241,22,The Hookup Plan,Comedy,7.443526170798898,3.4269972451790633,5,3,363
False,False,False,0.00019204916458613405,3,GIMS: On the Record,Documentary+Music,7.666666666666667,5.992592592592593,7,3,135
True,False,False,0.0028167210805966326,19,Money Heist,Action+Crime+Mystery+Thriller,4.767732962447845,3.5931849791376913,4.0,3.0,1438
False,False,False,0.003904999679918059,7,Virgin River,Drama+Romance,5.919034090909091,4.686079545454546,4.0,4.0,704
True,False,False,0.0005121311055630241,20,Winter on Fire: Ukraine's Fight for Freedom,Documentary,11.141176470588235,4.235294117647059,12,4,85
3 changes: 3 additions & 0 deletions pyproject.toml
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta:__legacy__"
46 changes: 1 addition & 45 deletions requirements.txt
@@ -1,46 +1,2 @@
docutils~=0.16
lxml~=4.4.2
pip~=19.3.1
argparse~=1.4.0
configparser~=4.0.2
wheel~=0.33.6
cryptography~=2.8
typing~=3.7.4.3
filelock~=3.0.12
six~=1.12.0
packaging~=20.4
attrs~=19.3.0
future~=0.18.2
tqdm~=4.36.1
requests~=2.25.1
isort~=4.3.21
setuptools~=41.4.0
rfc3986~=1.4.0
urllib3~=1.25.7
wcwidth~=0.1.8
pexpect~=4.8.0
certifi~=2019.11.28
chardet~=3.0.4
idna~=2.8
pygments~=2.5.2
pillow~=7.0.0
sphinx~=3.2.1
simplejson~=3.17.0
pyparsing~=2.4.0
pycparser~=2.19
snowballstemmer~=2.0.0
virtualenv~=20.4.0
distlib~=0.3.1
appdirs~=1.4.3
bcrypt~=3.1.7
zipp~=3.1.0
requests~=2.25.1
munch~=2.5.0
sphinx-automodapi==0.13
sphinx-markdown-tables==0.0.15
sphinx-rtd-theme==0.5.0
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==1.0.3
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.4
59 changes: 59 additions & 0 deletions s4gpy/api/companyapi.py
@@ -0,0 +1,59 @@
from munch import munchify

from s4gpy.api.utils import expand_links


class CompanyAPI:
"""This class wraps the company-mapper api."""

def __init__(self, session):
"""Initialize a new CompanyAPI.
It works by inheriting from the session object of a S4GAPI.
"""
self.session = session

def get_root(self):
"""Get the root of the company mapping api"""

"""return a list of companies links"""
company_links = self.session.get("/").json()
expand_links(self.session, company_links)
return munchify(company_links)

def get_companies(self):
"""Get all the companies in the system"""
return self.get_root().companies()

def get_contents(self):
"""Get all the contents in the systel"""
return self.get_root().contents()

def get_company(self, id):
"""retreives information from a particular company, identified by its cc_code"""
r = self.session.get(f"/company/{id}")
if r.status_code > 299:
return None
company_links = r.json()
expand_links(self.session, company_links)
return munchify(company_links)

def get_content(self, id):
"""retreives information form a particular content, identified by its cc_code"""
r = self.session.get(f"/content/{id}")
if r.status_code > 299:
return None
content_links = r.json()
expand_links(self.session, content_links)
return munchify(content_links)

def push_company(self, id, name, link):
"""Publish a new company."""
self.session.post(f'/company/{id}',
json={'name': name, 'link': link})

def push_content(self, id, company_names):
"""Publish a new content associated with some company names."""
self.session.post(f'/content/{id}',
json={'company_names': [name for name in company_names]})

22 changes: 22 additions & 0 deletions s4gpy/api/netflixapi.py
@@ -0,0 +1,22 @@
from munch import munchify

from s4gpy.api.utils import expand_links


class NetflixAPI:
"""This class wraps the netflix-api."""

def __init__(self, session):
"""Initialize a new NetflixAPI.
It works by inheriting from the session object of a S4GAPI.
"""
self.session = session

def get_root(self):
"""Publish or updated a new direct schedule."""

"""returns a list of users."""
netflix_links = self.session.get("/api/netflix").json()
expand_links(self.session, netflix_links)
return munchify(netflix_links)
2 changes: 1 addition & 1 deletion s4gpy/api/user.py
Expand Up @@ -9,7 +9,7 @@ class UserAPI:
"""This class wraps the user API"""

def __init__(self, session):
"""intialiaze a new Credential API"""
"""intialiaze a new User API"""
self.session = session

def get_users(self):
Expand Down
4 changes: 2 additions & 2 deletions s4gpy/api/utils.py
Expand Up @@ -8,9 +8,9 @@ def expand_links(session, d):
rel = link["rel"]
href = link["href"]

def call(address=href):
def call(address=href, **kwargs):

resp = session.get(address)
resp = session.get(address, params=kwargs)
if resp.status_code != 200:
logging.error(f"calling {address} failed : {resp.status_code}")
return ""
Expand Down
16 changes: 16 additions & 0 deletions s4gpy/s4gpy.py
Expand Up @@ -6,6 +6,9 @@
from s4gpy.api.credentials import CredentialAPI
from s4gpy.api.user import UserAPI
from s4gpy.s4gsession import S4GSession
from s4gpy.api.netflixapi import NetflixAPI
from s4gpy.api.companyapi import CompanyAPI



class S4GAPI:
Expand Down Expand Up @@ -59,6 +62,19 @@ def get_user_api(self):
S4GSession(
prefix_url=f'{self.protocol}://api.{self.root_dns}',
access_token=self.access_token))
def get_netflix_api(self):
"""Return a configured instance of the NetflixAPI."""
return NetflixAPI(
S4GSession(
prefix_url=f'{self.protocol}://api.{self.root_dns}/api/netflix',
access_token=self.access_token))

def get_company_api(self):
"""Return a configured instance of the NetflixAPI."""
return CompanyAPI(
S4GSession(
prefix_url=f'{self.protocol}://imdb-company-mapper.{self.root_dns}/',
access_token=self.access_token))

def get_direct_api(self):
"""Return a configured instance of the ConsoAPI."""
Expand Down
11 changes: 6 additions & 5 deletions setup.py
Expand Up @@ -14,7 +14,7 @@
setup(
# Basic project information
name='s4gpy',
version='0.0.3',
version='0.0.7',
# Authorship and online reference
author='Nicolas Herbaut',
author_email='nicolas.herbaut@univ-paris1.fr',
Expand All @@ -24,21 +24,22 @@
long_description=long_description,
long_description_content_type='text/markdown',
keywords='streaming, recommendation, science',

classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Natural Language :: English',
'License :: OSI Approved :: Apache Software License',
'Programming Language :: Python',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8'
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9'
],
# Package configuration
packages=find_packages(exclude=('tests',)),
include_package_data=True,
python_requires='>= 3.6',
install_requires=[],
python_requires='>= 3.7',
install_requires=["requests~=2.25.1", "munch~=2.5.0"],
# Licensing and copyright
license='Apache 2.0'
)

0 comments on commit 5e9f140

Please sign in to comment.