Last active
July 22, 2022 23:21
-
-
Save andfanilo/871367526a894da8a8ddeeb471aed437 to your computer and use it in GitHub Desktop.
Very Quick Streamlit Youtube scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import os | |
| import pickle | |
| from pathlib import Path | |
| import streamlit as st | |
| from google.auth.transport.requests import Request | |
| from google_auth_oauthlib.flow import InstalledAppFlow | |
| from googleapiclient.discovery import build | |
| CLIENT_SECRETS_FILE = "client_secret.json" | |
| SCOPES = ["https://www.googleapis.com/auth/yt-analytics.readonly"] | |
| API_SERVICE_NAME = "youtubeanalytics" | |
| API_VERSION = "v2" | |
| if "credentials" not in st.session_state: | |
| st.session_state.credentials = None | |
| @st.experimental_singleton | |
| def get_yt(): | |
| if Path("token.pickle").exists(): | |
| print("Loading Credentials from pickle") | |
| with open("token.pickle", "rb") as token: | |
| st.session_state.credentials = pickle.load(token) | |
| # Check token validity or login | |
| if not st.session_state.credentials or not st.session_state.credentials.valid: | |
| if ( | |
| st.session_state.credentials | |
| and st.session_state.credentials.expired | |
| and st.session_state.credentials.refresh_token | |
| ): | |
| print("Refreshing Access Token...") | |
| st.session_state.credentials.refresh(Request()) | |
| else: | |
| print("Fetching New Tokens...") | |
| flow = InstalledAppFlow.from_client_secrets_file( | |
| CLIENT_SECRETS_FILE, | |
| scopes=SCOPES, | |
| ) | |
| flow.run_console() | |
| st.session_state.credentials = flow.credentials | |
| with open("token.pickle", "wb") as f: | |
| pickle.dump(st.session_state.credentials, f) | |
| return build( | |
| API_SERVICE_NAME, API_VERSION, credentials=st.session_state.credentials | |
| ) | |
| def execute_api_request(client_library_function, **kwargs): | |
| response = client_library_function(**kwargs).execute() | |
| return response | |
| def main(): | |
| yt = get_yt() | |
| data = execute_api_request( | |
| yt.reports().query, | |
| ids="channel==MINE", | |
| startDate="2022-07-01", | |
| endDate="2022-07-20", | |
| metrics="views,estimatedMinutesWatched,averageViewDuration,averageViewPercentage,subscribersGained", | |
| dimensions="day", | |
| sort="day", | |
| ) | |
| st.write(data) | |
| if __name__ == "__main__": | |
| st.set_page_config( | |
| page_title="YT Dashboard", page_icon="chart_with_upwards_trend", layout="wide" | |
| ) | |
| # When running locally, disable OAuthlib's HTTPs verification. When | |
| # running in production *do not* leave this option enabled. | |
| os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1" | |
| st.title("My Youtube Dashboard") | |
| with st.expander("Resources"): | |
| st.markdown( | |
| """ | |
| - https://github.com/youtube/api-samples/blob/master/python/yt_analytics_v2.py | |
| - https://developers.google.com/youtube/analytics/data_model | |
| """ | |
| ) | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import streamlit as st | |
| import googleapiclient.discovery | |
| import googleapiclient.errors | |
| API_KEY = st.secrets["API_KEY"] | |
| @st.experimental_singleton | |
| def _get_youtube_client(): | |
| return googleapiclient.discovery.build("youtube", "v3", developerKey = API_KEY) | |
| @st.experimental_memo | |
| def _search(term: str): | |
| youtube = _get_youtube_client() | |
| request = youtube.search().list( | |
| part="snippet", | |
| maxResults=25, | |
| q=term | |
| ) | |
| response = request.execute() | |
| return response | |
| def main(): | |
| st.title("My Youtube Dashboard") | |
| search_term = st.text_input("Enter Search term", "Streamlit") | |
| if st.button("Search"): | |
| results = [item["snippet"] for item in _search(search_term)["items"]] | |
| n_rows, n_cols = (5, 5) | |
| cols = [column for _ in range(n_rows) for column in st.columns(n_cols)] | |
| for col, res in zip(cols, results): | |
| with col: | |
| st.markdown(res["title"]) | |
| st.image(res["thumbnails"]["default"]["url"]) | |
| if __name__ == "__main__": | |
| st.set_page_config(page_title="YT Dashboard", page_icon="chart_with_upwards_trend", layout="wide") | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment