Skip to content

Commit

Permalink
Add playlist report types and fix broken ones
Browse files Browse the repository at this point in the history
Numerous changes include:
- Modifying the secrets home path
- Fixing verification constants
- Making the verifier check sort options
  • Loading branch information
parafoxia committed Jul 24, 2021
1 parent 5ac2682 commit f4d8866
Show file tree
Hide file tree
Showing 6 changed files with 436 additions and 87 deletions.
2 changes: 1 addition & 1 deletion analytix/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__productname__ = "analytix"
__version__ = "2.0.0.dev5"
__version__ = "2.0.0.dev6"
__description__ = "A simple yet powerful API wrapper to make getting analytical information from the YouTube Analytics API easier than ever."
__url__ = "https://github.com/parafoxia/analytix"
__docs__ = "https://analytix.readthedocs.io/en/latest/"
Expand Down
7 changes: 1 addition & 6 deletions analytix/secrets.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import os
from pathlib import Path

if os.name == "nt":
TOKEN_STORE = Path("%USERPROFILE%/.analytix")
else:
TOKEN_STORE = Path(f"/home/{os.environ['USER']}/.analytix")

TOKEN_STORE = Path().home() / ".analytix"
YT_ANALYTICS_TOKEN = "youtube-analytics-token.json"
4 changes: 2 additions & 2 deletions analytix/youtube/analytics/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def _get_token():

def authorise(self, store_token=True, force=False, **kwargs):
if self._token and not force:
logging.warning("Client is already authorised! Skipping...")
logging.info("Client is already authorised! Skipping...")
return

url, _ = self._session.authorization_url(
Expand Down Expand Up @@ -228,7 +228,7 @@ def retrieve(
logging.debug("Using these metrics: " + ", ".join(metrics))

logging.debug("Verifying report...")
rtype.verify(dimensions, metrics, filters, max_results, sort_by)
rtype.verify(dimensions, metrics, filters, sort_by, max_results)
logging.debug("Verification complete")

url = (
Expand Down
74 changes: 74 additions & 0 deletions analytix/youtube/analytics/verify/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@
"self",
"thirdParty",
),
"isCurated": ("1",),
}

YOUTUBE_ANALYTICS_ALL_FILTERS = tuple(
Expand Down Expand Up @@ -343,6 +344,50 @@
"playbackBasedCpm",
"adImpressions",
"cpm",
"viewerPercentage",
"audienceWatchRatio",
"relativeRetentionPerformance",
"playlistStarts",
"viewsPerPlaylistStart",
"averageTimeInPlaylist",
)

YOUTUBE_ANALYTICS_ALL_VIDEO_METRICS = (
"views",
"redViews",
"comments",
"likes",
"dislikes",
"videosAddedToPlaylists",
"videosRemovedFromPlaylists",
"shares",
"estimatedMinutesWatched",
"estimatedRedMinutesWatched",
"averageViewDuration",
"averageViewPercentage",
"annotationClickThroughRate",
"annotationCloseRate",
"annotationImpressions",
"annotationClickableImpressions",
"annotationClosableImpressions",
"annotationClicks",
"annotationCloses",
"cardClickRate",
"cardTeaserClickRate",
"cardImpressions",
"cardTeaserImpressions",
"cardClicks",
"cardTeaserClicks",
"subscribersGained",
"subscribersLost",
"estimatedRevenue",
"estimatedAdRevenue",
"grossRevenue",
"estimatedRedPartnerRevenue",
"monetizedPlaybacks",
"playbackBasedCpm",
"adImpressions",
"cpm",
)

YOUTUBE_ANALYTICS_ALL_PROVINCE_METRICS = (
Expand Down Expand Up @@ -434,3 +479,32 @@
"viewsPerPlaylistStart",
"averageTimeInPlaylist",
)

YOUTUBE_ANALYTICS_ALL_SORT_OPTIONS = (
"views",
"redViews",
"estimatedRevenue",
"estimatedRedPartnerRevenue",
"estimatedMinutesWatched",
"estimatedRedMinutesWatched",
"subscribersGained",
"subscribersLost",
)

YOUTUBE_ANALYTICS_LOCATION_AND_TRAFFIC_SORT_OPTIONS = (
"views",
"estimatedMinutesWatched",
)

YOUTUBE_ANALYTICS_TOP_VIDEOS_SORT_OPTIONS = (
"views",
"redViews",
"estimatedMinutesWatched",
"estimatedRedMinutesWatched",
)

YOUTUBE_ANALYTICS_LOCATION_AND_TRAFFIC_PLAYLIST_SORT_OPTIONS = (
"views",
"estimatedMinutesWatched",
"playlistStarts",
)
31 changes: 27 additions & 4 deletions analytix/youtube/analytics/verify/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"Dimensions",
"Filters",
"Metrics",
"SortOptions",
"Required",
"ExactlyOne",
"OneOrMore",
Expand Down Expand Up @@ -98,10 +99,8 @@ def verify(self, against):
s.verify(against, "filter")

for k, v in against.items():
if (
against[k]
and v not in YOUTUBE_ANALYTICS_VALID_FILTER_OPTIONS[k]
):
valid_values = YOUTUBE_ANALYTICS_VALID_FILTER_OPTIONS[k]
if against[k] and valid_values and v not in valid_values:
raise InvalidRequest(
f"'{v}' is not a valid value for filter '{k}'"
)
Expand Down Expand Up @@ -131,6 +130,30 @@ def verify(self, against):
)


class SortOptions:
def __init__(self, *values):
self.values = values

def __iter__(self):
return iter(self.values)

def verify(self, against):
against = set([a.strip("-") for a in against])

diff = against - set(YOUTUBE_ANALYTICS_ALL_METRICS)
if diff:
raise InvalidRequest(
f"one or more sort options you provided are invalid ({', '.join(diff)})"
)

diff = against - set(self.values)
if diff:
raise InvalidRequest(
"one or more sort options you provided are not supported by the "
f"selected report type ({', '.join(diff)})"
)


class Required(FeatureSet):
def verify(self, against, ftype):
values = []
Expand Down
Loading

0 comments on commit f4d8866

Please sign in to comment.