Reputation: 1
I'm making a counter for a YouTube channel, but the problem with my code and the API is that because on YouTube the channel shows to have 1,32k subs, my counter gives me 1320 subscriber, but the actual number is 1326, so I was wandering if there was a better way to do it?
import time
import requests
#from Adafruit_SSD1306 import SSD1306_128_64
# API key and channel details
API_KEY = 'this part works im just not giving it'
CHANNEL_ID = 'this part works im just not giving it'
API_URL = f'https://www.googleapis.com/youtube/v3/channels?part=statistics&id={CHANNEL_ID}&key={API_KEY}'
def fetch_subscriber_count():
"""Fetch subscriber count from YouTube API."""
try:
response = requests.get(API_URL)
response.raise_for_status()
data = response.json()
subscriber_count = data['items'][0]['statistics']['subscriberCount']
return int(subscriber_count)
except Exception as e:
print(f"Error fetching subscriber count: {e}")
return None
def display_subscriber_count(count):
"""Display subscriber count on the OLED without image generation."""
if count is not None:
print(f"Subscribers: {count}")
else:
print("Error fetching data")
# Main loop
try:
while True:
subscriber_count = fetch_subscriber_count()
display_subscriber_count(subscriber_count)
time.sleep(60) # Update every minute
except KeyboardInterrupt:
print("Exiting...")
Upvotes: 0
Views: 53