|
2 | 2 | from datetime import date, timedelta
|
3 | 3 | import json, csv
|
4 | 4 | from enum import Enum
|
5 |
| -from typing import Any, TypeVar |
| 5 | +from typing import Any, TypeVar, Literal, TypedDict |
6 | 6 | from SlyAPI import *
|
7 | 7 |
|
8 | 8 | def makeFilters(filters: dict[str, Any]) -> str:
|
@@ -48,11 +48,20 @@ class Metrics(Enum):
|
48 | 48 | def __add__(self: 'Metrics', other: 'Metrics|set[Metrics]') -> _AddOperator_Set['Metrics']:
|
49 | 49 | return _AddOperator_Set((self,)) + other
|
50 | 50 |
|
| 51 | +class ColumnHeader(TypedDict): |
| 52 | + 'From https://developers.google.com/youtube/analytics/v2/reference/reports/query' |
| 53 | + name: str |
| 54 | + columnType: Literal['DIMENSION'] | Literal['METRIC'] |
| 55 | + dataType: str |
| 56 | + |
51 | 57 | @dataclass
|
52 | 58 | class QueryResult:
|
| 59 | + ''' |
| 60 | + Table of data returned by YouTube Analytics API. |
| 61 | + From https://developers.google.com/youtube/analytics/v2/reference/reports/query''' |
53 | 62 | kind: str
|
54 |
| - columnHeaders: list[dict[str, str]] # { "name": ..., "columnType": ..., "dataType": ... } |
55 |
| - rows: list[list[Any]] |
| 63 | + columnHeaders: list[ColumnHeader] |
| 64 | + rows: list[list[int|str|float|bool|None]] |
56 | 65 |
|
57 | 66 | def saveJSON(self, path: str):
|
58 | 67 | with open(path, mode='w', encoding='utf8') as f:
|
@@ -80,7 +89,19 @@ class YouTubeAnalytics(WebAPI):
|
80 | 89 |
|
81 | 90 | channel_id: str
|
82 | 91 |
|
83 |
| - def __init__(self, channel_id: str, auth: OAuth2): |
| 92 | + def __init__(self, channel_id: str, auth_or_app: OAuth2|str, user: str|None=None, _scopes: Any|None=None): |
| 93 | + if user is not None: |
| 94 | + user_ = OAuth2User.from_json_file(user) |
| 95 | + else: |
| 96 | + user_ = None |
| 97 | + if isinstance(auth_or_app, str): |
| 98 | + app = OAuth2App.from_json_file(auth_or_app) |
| 99 | + if user_ is None: |
| 100 | + raise ValueError('user must be specified when auth_or_app is a file path') |
| 101 | + auth = OAuth2(app, user_) |
| 102 | + else: |
| 103 | + auth = auth_or_app |
| 104 | + |
84 | 105 | super().__init__(auth)
|
85 | 106 | self.channel_id = channel_id
|
86 | 107 |
|
|
0 commit comments