Skip to content

Add error handling support and apply PEP-8 #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions micropython_youtube_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# THE SOFTWARE.

"""
`micropython_youtube_api` - YouTube API
`micropython_youtube_api` - YouTube API
====================================================
See examples folder for how to use
* Author(s): Seon Rozenblum
Expand All @@ -30,11 +30,12 @@
__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/unexpectedmaker/micropython-youtube-api"

import time

import urequests as ureq
import json, time

class YoutubeAPI:

class YoutubeAPI:
def __init__(self, channel_id, app_key_id, query_interval_sec=60):
if not isinstance(channel_id, str):
raise TypeError("'channel_id' must be provided")
Expand All @@ -53,6 +54,8 @@ def __init__(self, channel_id, app_key_id, query_interval_sec=60):
self._comments = 0
self._videos = 0

self.last_status = 0

def __enter__(self):
return self

Expand All @@ -68,29 +71,35 @@ def _update_stats(self):
def _grab_stats(self):
# Create the API query to send to GoogleAPI
urlbase = "https://www.googleapis.com/youtube/v3/channels"
youtube_url = "{}?part=statistics&id={}&key={}".format(urlbase, self.channel_id, self.app_key_id )
youtube_url = "{}?part=statistics&id={}&key={}".format(urlbase, self.channel_id, self.app_key_id)

#print ("Contacting GoogleAPI... " )
# print ("Contacting GoogleAPI... " )

# request the data from Google
req = ureq.get(youtube_url)
if req.status_code == 200:
stats = [{
'subs': stat['statistics']['subscriberCount'],
self.last_status = req.status_code
if self.last_status == 200:
response = req.json()
# print("Response: ", response)
stats = [
{
'subs': stat['statistics']['subscriberCount'],
'views': stat['statistics']['viewCount'],
'videos': stat['statistics']['videoCount'],
'comments': stat['statistics']['commentCount']
} for stat in req.json()['items']]

'comments': stat['statistics']['commentCount'],
}
for stat in response['items']
]

# for stat in YoutubeApi.stats
self._subs = stats[0]['subs']
self._views = stats[0]['views']
self._videos = stats[0]['videos']
self._comments = stats[0]['comments']
self._subs = stats[0].get('subs', 0)
self._views = stats[0].get('views', 0)
self._videos = stats[0].get('videos', 0)
self._comments = stats[0].get('comments', 0)
else:
print( "ERROR: status_code: " + str(req.status_code) )
print("ERROR: status_code: " + str(req.status_code))
req.close()

# Accessorss for each of the stats returned by the API
@property
def subs(self):
Expand Down