Skip to content

Commit

Permalink
Extra verification and bug fixes
Browse files Browse the repository at this point in the history
Verify sort metrics better
Fix a few bugs
  • Loading branch information
parafoxia committed Jul 23, 2021
1 parent d34bdd6 commit 567e6e3
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.json
secrets
tests

testrun.py

### Data ###
*.csv
Expand Down
3 changes: 3 additions & 0 deletions analytix/youtube/analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ def retrieve(self, metrics="all", verify=True, **kwargs):
if not isinstance(filters, dict):
raise InvalidRequest(f"expected dict of filters, got {type(filters).__name__}")

if not isinstance(sort_by, (tuple, list, set)):
raise InvalidRequest(f"expected tuple, list, or set of sorting metrics, got {type(sort_by).__name__}")

r = self.get_report_type(metrics, dimensions, filters, verify=verify)
r.verify(metrics, dimensions, filters, max_results, sort_by)

Expand Down
88 changes: 86 additions & 2 deletions analytix/youtube/reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Playback locations (detailed)"

Expand Down Expand Up @@ -404,6 +412,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Traffic sources (detailed)"

Expand Down Expand Up @@ -483,7 +499,11 @@ def __init__(self):
(FeatureAmount.ZERO_OR_ONE, {"subscribedStatus"}),
]
self.metrics = ("shares",)
self.filters = [(FeatureAmount.ZERO_OR_ONE, {"country", "continent", "subContinent"}), Detail]
self.filters = [
(FeatureAmount.ZERO_OR_ONE, {"country", "continent", "subContinent"}),
(FeatureAmount.ZERO_OR_ONE, {"video", "group"}),
(FeatureAmount.ANY, {"subscribedStatus"}),
]

def __str__(self):
return "Engagement and content sharing"
Expand Down Expand Up @@ -523,6 +543,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Top videos by region"

Expand All @@ -542,6 +570,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Top videos by state"

Expand All @@ -564,6 +600,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Top videos by subscription status"

Expand All @@ -586,6 +630,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Top videos by YouTube product"

Expand All @@ -608,6 +660,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Top videos by playback detail"

Expand Down Expand Up @@ -757,6 +817,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Playback locations for playlists (detailed)"

Expand Down Expand Up @@ -808,6 +876,14 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Traffic sources for playlists (detailed)"

Expand Down Expand Up @@ -927,13 +1003,21 @@ def verify(self, metrics, dimensions, filters, max_results, sort_by):
if not sort_by:
raise InvalidRequest("you must provide at least 1 sort parameter")

if any(not s[0] == "-" for s in sort_by):
raise InvalidRequest(
(
"you can only sort in descending order for this report type. "
"You can do this by prefixing the sort metrics with '-'"
)
)

def __str__(self):
return "Top playlists"


class AdPerformance(ReportType):
def __init__(self):
self.dimensions = ((FeatureAmount.REQUIRED, {"adType"}), (FeatureAmount.OPTIONAL, {"day"}))
self.dimensions = ((FeatureAmount.REQUIRED, {"adType"}), (FeatureAmount.ANY, {"day"}))
self.metrics = ("grossRevenue", "adImpressions", "cpm")
self.filters = (
(FeatureAmount.ZERO_OR_ONE, {"video", "group"}),
Expand Down

0 comments on commit 567e6e3

Please sign in to comment.