{"id":18504197,"url":"https://github.com/tran1595/youtubeanalytics","last_synced_at":"2026-04-18T07:31:17.926Z","repository":{"id":261394336,"uuid":"881805985","full_name":"Tran1595/YouTubeAnalytics","owner":"Tran1595","description":" I'm excited to share a guide I created to help you explore what makes YouTube videos go viral. Whether you're a content creator, data enthusiast, or just curious about viral trends, this guide is for you!","archived":false,"fork":false,"pushed_at":"2024-11-06T09:59:22.000Z","size":45,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-17T01:44:13.655Z","etag":null,"topics":["dataanalysisusingpython","datainsights","postgresql","sql","trends-analysis","viralvideo","youtube"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Tran1595.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-01T09:16:42.000Z","updated_at":"2024-11-06T09:59:25.000Z","dependencies_parsed_at":"2024-11-06T11:01:47.078Z","dependency_job_id":"83b6338a-879b-4377-aa67-cafb61558922","html_url":"https://github.com/Tran1595/YouTubeAnalytics","commit_stats":null,"previous_names":["tran1595/youtubeanalytics"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tran1595%2FYouTubeAnalytics","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tran1595%2FYouTubeAnalytics/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tran1595%2FYouTubeAnalytics/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Tran1595%2FYouTubeAnalytics/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Tran1595","download_url":"https://codeload.github.com/Tran1595/YouTubeAnalytics/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101559,"owners_count":22014908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dataanalysisusingpython","datainsights","postgresql","sql","trends-analysis","viralvideo","youtube"],"created_at":"2024-11-06T14:03:28.522Z","updated_at":"2026-04-18T07:31:17.893Z","avatar_url":"https://github.com/Tran1595.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Extracting YouTube Data and Structuring PgAdmin Tables\n### This repository provides technical steps for analyzing viral YouTube videos:\n\n- YouTube API Tutorial: For more detailed steps on using the YouTube API, check out this [YouTube API Docs](https://developers.google.com/youtube/v3/getting-started)\n  and [YouTube API Key](https://www.youtube.com/watch?v=DuudSp4sHmg\u0026t=137s), [YouTube API for Python](https://www.youtube.com/watch?v=D56_Cx36oGY\u0026t=811s).\n- Note: This repository does not provide SQL for analysis. Please conduct your own analysis based on the provided data.\n- Here are covered steps:\n   1. Creating a Table in PgAdmin. (I used Pycharm, and it doesn't support SQL)\n   2. Fetching Data with Python and YouTube API.\n   3. Providing the CSV File for Non-Python/Postgres Users.\n   \n\n## 1. Creating a Table in PgAdmin\nAssuming you have PostgreSQL and PgAdmin set up, here’s the SQL script to create the table:\n\n```bash\nCREATE TABLE youtube_videos (\n    title TEXT NOT NULL,\n    category_name TEXT NOT NULL,\n    view_count INTEGER NOT NULL,\n    like_count INTEGER NOT NULL,\n    comment_count INTEGER NOT NULL,\n    published_at TIMESTAMP NOT NULL,\n    thumbnail_url TEXT NOT NULL,\n    tags TEXT NOT NULL,\n    subscriber_count INTEGER NOT NULL,\n    video_count INTEGER NOT NULL,\n    is_weekend BOOLEAN NOT NULL,\n    video_length INTEGER NOT NULL\n);\n```\n## 2. Fetching Data with Python and YouTube API\n\nFirst, make sure you have the necessary Python packages installed, for example psycopg2:\n\n(_You will probably need to install more packages if encounter errors when running the code_)\n\n```bash\npip install google-api-python-client pandas psycopg2\n```\nNext, fetch data from the YouTube API. Here’s a sample Python script:\n\n```bash\nimport psycopg2\nimport googleapiclient.discovery\nimport isodate\nfrom datetime import datetime\nfrom datetime import timedelta\n\n# YouTube API credentials\nDEVELOPER_KEY = \"YOUR_API_KEY\"\nYOUTUBE_API_SERVICE_NAME = \"youtube\"\nYOUTUBE_API_VERSION = \"v3\"\nregionCode = \"VN\" # Adjusting region\nmaxResults = 50  # Adjusting to smaller pages for pagination\ntotal_videos = 200  # Total number of videos you want\n\n# PostgreSQL credentials\nDB_NAME = \"postgres\"\nDB_USER = \"postgres\"\nDB_PASSWORD = \"your_password\"\nDB_HOST = \"localhost\"\nDB_PORT = \"5432\"\n\n# Connect to PostgreSQL database\ntry:\n    conn = psycopg2.connect(\n        dbname=DB_NAME,\n        user=DB_USER,\n        password=DB_PASSWORD,\n        host=DB_HOST,\n        port=DB_PORT\n    )\n    cursor = conn.cursor()\nexcept psycopg2.Error as e:\n    print(f\"Database connection error: {e}\")\n\n# Get YouTube data\ntry:\n    youtube = googleapiclient.discovery.build(\n        YOUTUBE_API_SERVICE_NAME,\n        YOUTUBE_API_VERSION,\n        developerKey=DEVELOPER_KEY\n    )\nexcept Exception as e:\n    print(f\"Error creating YouTube client: {e}\")\n\n# Fetch video categories\ntry:\n    categories_request = youtube.videoCategories().list(\n        part=\"snippet\",\n        regionCode=regionCode\n    )\n    categories_response = categories_request.execute()\n    category_dict = {cat['id']: cat['snippet']['title'] for cat in categories_response['items']}\nexcept Exception as e:\n    print(f\"Error fetching video categories: {e}\")\n\ndef fetch_and_store_videos(page_token=None, videos_fetched=0):\n    print(f\"Fetching with page_token: {page_token}, videos_fetched: {videos_fetched}\")\n    if videos_fetched \u003e= total_videos:\n        return\n    try:\n        request = youtube.videos().list(\n            part=\"snippet,statistics,contentDetails\",\n            chart=\"mostPopular\", # Adjust categories\n            regionCode=regionCode,\n            maxResults=maxResults,\n            pageToken=page_token\n        )\n        response = request.execute()\n    except Exception as e:\n        print(f\"Error fetching YouTube data: {e}\")\n        return\n    for item in response['items']:\n        if videos_fetched \u003e= total_videos:\n            break\n        title = item['snippet']['title']\n        category_name = category_dict.get(item['snippet']['categoryId'], \"Unknown\")\n        view_count = item['statistics'].get('viewCount', 0)\n        like_count = item['statistics'].get('likeCount', 0)\n        comment_count = item['statistics'].get('commentCount', 0)\n        published_at = item['snippet']['publishedAt']\n        video_length = isodate.parse_duration(item['contentDetails']['duration']).total_seconds()\n        # Convert to HH:MM:SS format\n        video_duration = str(timedelta(seconds=video_length))\n        print(video_duration)  # Outputs: formatted time like '2:08:11'\n        thumbnail_url = item['snippet']['thumbnails']['high']['url']\n        tags = ','.join(item['snippet'].get('tags', []))\n        published_datetime = datetime.strptime(published_at, \"%Y-%m-%dT%H:%M:%SZ\")\n        is_weekend = published_datetime.weekday() \u003e= 5\n        channel_id = item['snippet']['channelId']\n        try:\n            channel_request = youtube.channels().list(\n                part=\"statistics\",\n                id=channel_id\n            )\n            channel_response = channel_request.execute()\n            channel_info = channel_response['items'][0]['statistics']\n            subscriber_count = channel_info.get('subscriberCount', 0)\n            video_count = channel_info.get('videoCount', 0)\n        except Exception as e:\n            print(f\"Error fetching channel data: {e}\")\n            subscriber_count = 0\n            video_count = 0\n\n# INSERT DATA TO POSTGRES TABLE, THE TABLE SHOULD ALREADY BEEN CREATED.\n        try:\n            cursor.execute('''\n                            INSERT INTO youtube_videos (title, category_name, view_count, like_count, comment_count,\n                             published_at, thumbnail_url, tags, subscriber_count, video_count, is_weekend,video_length)\n                            VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)\n                        ''', (\n                title, category_name, view_count, like_count, comment_count, published_at,\n                thumbnail_url,\n                tags, subscriber_count, video_count, is_weekend,video_duration\n            ))\n            conn.commit()  # Commit the transaction\n            videos_fetched += 1\n            print(f\"Fetched and stored video #{videos_fetched}\")\n        except psycopg2.Error as e:\n            print(f\"Database insertion error: {e}\")\n    next_page_token = response.get('nextPageToken')\n    print(f\"Next Page Token: {next_page_token}\")\n    if next_page_token and videos_fetched \u003c total_videos:\n        fetch_and_store_videos(next_page_token, videos_fetched)\n\n# Start fetching and storing videos\nfetch_and_store_videos()\n\n# Close the cursor and connection\ncursor.close()\nconn.close()\n```\n\nTo confirm the data has been successfully inserted, run the following SQL query in PgAdmin:\nrun ``` SELECT * FROM youtube_videos; ```\n\n## 3. CSV File for Non-Python/Postgres Users:\n\nYou can analyze the data using [youtube_data.csv](https://github.com/Tran1595/YouTubeAnalytics/blob/main/20241028_ytb_db.csv) in Excel or Google Sheets.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftran1595%2Fyoutubeanalytics","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftran1595%2Fyoutubeanalytics","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftran1595%2Fyoutubeanalytics/lists"}