import os
from googleapiclient.discovery import build
import csv
import time
# Replace with your API key
API_KEY = "###"
# Create a YouTube Data API client
youtube = build("youtube", "v3", developerKey=API_KEY)
def get_channel_videos(channel_id):
videos = []
next_page_token = None
# Get the "uploads" playlist ID for the channel
channels_response = youtube.channels().list(
id=channel_id,
part='contentDetails'
).execute()
uploads_playlist_id = channels_response['items'][0]['contentDetails']['relatedPlaylists']['uploads']
while True:
# Fetch the videos in the "uploads" playlist
playlist_items_request = youtube.playlistItems().list(
playlistId=uploads_playlist_id,
part='snippet',
# maxResults=50,
pageToken=next_page_token
)
playlist_items_response = playlist_items_request.execute()
# Extract video IDs
video_ids = [item['snippet']['resourceId']['videoId'] for item in playlist_items_response['items']]
# Fetch video details and statistics for each video ID
videos_request = youtube.videos().list(
part="snippet,statistics",
id=','.join(video_ids)
)
videos_response = videos_request.execute()
# Process each video
for item in videos_response['items']:
video = {
"title": item['snippet']['title'],
"views": item['statistics']['viewCount'],
"video_link": f"https://www.youtube.com/watch?v={item['id']}"
}
videos.append(video)
# Check if there are more pages of results
next_page_token = playlist_items_response.get("nextPageToken")
if not next_page_token:
break
return videos
def save_to_csv(videos, file_path):
# Define the field names (columns) for the CSV file
field_names = ["Title", "Views", "Video Link"]
# Write the video information to the CSV file
with open(file_path, mode="w", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=field_names)
writer.writeheader()
for video in videos:
writer.writerow({
"Title": video["title"],
"Views": video["views"],
"Video Link": video["video_link"]
})
if __name__ == "__main__":
channel_id = "UCTA395IvJGrr7LBsqVAlJsw" # Replace with the channel ID of the target channel
videos = get_channel_videos(channel_id)
# Specify the path where you want to save the CSV file
csv_file_path = "/Users/jitendersingh/Downloads/hindi med knowledge 20 apr 12 am.csv"
# Save video information to the CSV file
save_to_csv(videos, csv_file_path)
print("Video information saved to", csv_file_path)
Another file create for csv correction.
import pandas as pd
import os
import time
def synchronize_csv_files(old_csv_path, new_csv_path, output_csv_path):
# Read the old and new CSV files into DataFrames
old_df = pd.read_csv(old_csv_path)
new_df = pd.read_csv(new_csv_path)
# Find the matching value in the first column of the second row in the new CSV
matching_value = old_df.iloc[1, 0]
# Delete rows from the new CSV until the matching value is found in the first column
while new_df.iloc[1, 0] != matching_value:
new_df = new_df.iloc[1:]
# Reset the index of the new DataFrame
new_df.reset_index(drop=True, inplace=True)
# Combine the rows from both CSV files into a single DataFrame
combined_df = pd.concat([old_df, new_df], axis=1)
# Save the synchronized CSV to the output file
combined_df.to_csv(output_csv_path, index=False)
def calculate_numbers(input_csv_path, output_csv_path):
# Read the synchronized CSV file into a DataFrame
df = pd.read_csv(input_csv_path)
# Calculate the result in the 6th column based on the values in the 2nd and 5th columns
df['GAP'] = df['Views.1'] - df['Views']
# Save the modified CSV to the output file with a new name
df.to_csv(output_csv_path, index=False)
channel_name = "Tech Droid Hindi"
# Example usage:
old_csv_path = "/Users/jitendersingh/Downloads/"+channel_name+" 11 apr 11 am.csv"
new_csv_path = "/Users/jitendersingh/Downloads/"+channel_name+" 12 apr 11 am.csv"
output_csv_path = "/Users/jitendersingh/Downloads/temp_csv.csv"
synchronize_csv_files(old_csv_path, new_csv_path, output_csv_path)
# Example usage:
input_csv_path = "/Users/jitendersingh/Downloads/temp_csv.csv"
output_csv_path = "/Users/jitendersingh/Downloads/"+channel_name+" 24 hours.csv"
calculate_numbers(input_csv_path, output_csv_path)
time.sleep(1)
os.remove("/Users/jitendersingh/Downloads/temp_csv.csv")
0 comments:
Post a Comment