-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (151 loc) · 6.62 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# python -m venv venv
# on windows
# venv\Scripts\activate
# on macos/linux
# source venv/bin/activate
# python -m pip install google-api-python-client google-auth google-auth-httplib2 google-auth-oauthlib tk
import os
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox
from datetime import datetime
import google.oauth2.credentials
import google_auth_oauthlib.flow
from googleapiclient.discovery import build
# vars
search_var = None
table = None
search_results = []
current_result_index = -1
root = tk.Tk()
def get_authenticated_service():
# Load the credentials from the client secrets JSON file
credentials = None
if os.path.exists('credentials.json'):
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
'credentials.json',
scopes=['https://www.googleapis.com/auth/youtube.readonly']
)
credentials = flow.run_local_server()
# Return an authenticated YouTube Data API service
return build('youtube', 'v3', credentials=credentials)
def get_subscribed_channels(youtube):
subscribed_channels = []
next_page_token = None
while True:
# Get the list of subscribed channels with a page token
subscriptions = youtube.subscriptions().list(
part='snippet',
mine=True,
maxResults=50, # Adjust this value based on your preference
pageToken=next_page_token
).execute()
subscribed_channels.extend(subscriptions.get('items', []))
next_page_token = subscriptions.get('nextPageToken')
if not next_page_token:
break
return subscribed_channels
def display_subscribed_channels_gui(subscribed_channels):
root.title("Subscribed Channels")
global search_var
search_var = tk.StringVar()
search_frame = ttk.Frame(root)
search_frame.pack(pady=10)
search_entry = ttk.Entry(search_frame, textvariable=search_var)
search_entry.pack(side="left", expand=True, padx=5)
search_button = ttk.Button(search_frame, text="Search", command=filter_channels, style="Sharp.TButton")
search_button.pack(side="left", expand=True,padx=5)
prev_button = ttk.Button(search_frame, text="<<", command=select_previous_result, style="Sharp.TButton")
prev_button.pack(side="left", padx=5)
next_button = ttk.Button(search_frame, text=">>", command=select_next_result, style="Sharp.TButton")
next_button.pack(side="left", padx=5)
global table
table = ttk.Treeview(root, columns=("date_subscribed", "time_since_subscribed", "channel_name", "channel_link"), show="headings")
table.heading("date_subscribed", text="Date Subscribed")
table.heading("time_since_subscribed", text="Time Since Subscribed")
table.heading("channel_name", text="Channel Name")
table.heading("channel_link", text="Channel Link")
scrollbar = ttk.Scrollbar(root, orient="vertical", command=table.yview)
table.configure(yscrollcommand=scrollbar.set)
table.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
for channel in subscribed_channels:
channel_id = channel['snippet']['resourceId']['channelId']
channel_name = channel['snippet']['title']
subscribed_at = channel['snippet']['publishedAt']
channel_link = f"https://www.youtube.com/channel/{channel_id}"
time_since_subscribed = calculate_time_since_subscribed(subscribed_at)
table.insert("", "end", values=(subscribed_at[:10], time_since_subscribed, channel_name, channel_link))
root.mainloop()
def calculate_time_since_subscribed(subscribed_at):
try:
# Try parsing with milliseconds format
subscribed_date = datetime.strptime(subscribed_at, "%Y-%m-%dT%H:%M:%S.%fZ")
except ValueError:
try:
# If parsing with milliseconds format fails, try without milliseconds
subscribed_date = datetime.strptime(subscribed_at, "%Y-%m-%dT%H:%M:%SZ")
except ValueError:
raise ValueError(f"Unable to parse subscribed_at: {subscribed_at}")
# Get the current date
current_date = datetime.now()
# Calculate the time difference
time_since_subscribed = current_date - subscribed_date
# Calculate the years, months, and days
years = time_since_subscribed.days // 365
months = (time_since_subscribed.days % 365) // 30
days = time_since_subscribed.days % 30
# Construct the result string
result = f"{years}y, {months}m, {days}d"
return result
def filter_channels():
global search_results
global current_result_index
search_text = search_var.get().lower()
search_results = []
current_result_index = -1
for row in table.get_children():
if search_text in table.item(row)['values'][2].lower():
search_results.append(row)
if len(search_results) > 0:
current_result_index = 0
table.selection_set(search_results[0])
table.focus(search_results[0])
table.see(search_results[0])
update_prev_next_buttons()
else:
table.selection_clear()
update_prev_next_buttons()
def create_styles():
ttk.Style().configure("Sharp.TButton", relief="flat", borderwidth=0)
ttk.Style().configure("Light.Treeview", background="#FFFFFF", foreground="#000000")
ttk.Style().map("Light.Treeview", background=[("selected", "#D3D3D3")])
ttk.Style().configure("Dark.Treeview", background="#222222", foreground="#FFFFFF")
ttk.Style().map("Dark.Treeview", background=[("selected", "#555555")])
def select_previous_result():
global current_result_index
current_result_index -= 1
if current_result_index < 0:
current_result_index = len(search_results) - 1
table.selection_set(search_results[current_result_index])
table.focus(search_results[current_result_index])
table.see(search_results[current_result_index])
update_prev_next_buttons()
def select_next_result():
global current_result_index
current_result_index += 1
if current_result_index >= len(search_results):
current_result_index = 0
table.selection_set(search_results[current_result_index])
table.focus(search_results[current_result_index])
table.see(search_results[current_result_index])
update_prev_next_buttons()
def update_prev_next_buttons():
global search_results
global current_result_index
if __name__ == '__main__':
youtube = get_authenticated_service()
subscribed_channels = get_subscribed_channels(youtube)
# Sort channels by subscription date (oldest to newest)
subscribed_channels.sort(key=lambda channel: channel['snippet']['publishedAt'])
display_subscribed_channels_gui(subscribed_channels)