Mar-15-2019, 08:54 AM
(This post was last modified: Mar-15-2019, 08:54 AM by misterfrickel.)
Hello Forum,
(edit: Added emphasis so this is actually a question instead of a ramble.)
to set the stage: I'm an absolute python newbie (Really, used it for the first time about a week ago) and I am also not a person that is in forums very much, so I apologize in advance if I break any unwritten rules!
Situation: As a novelty gift I was planning to build a notifier that blinks an LED when a new subscriber is added to the YouTube or Instagram account of my better half. That notifier is on a Pi Zero W with the Pimoroni Blinkt LED HAT. I figured out how to make it blink with the provided examples and also how to edit those examples. Yay me! Then I figured "Can't be that hard to have it check subscribers and blink red for new YT subscribers and green for IG followers!" Ohhh boy.
Long story short: With some help from a nice person I made up the stuff below and it actually does show the correct numbers for subs/follows in my SSH terminal window, it also blinks the leds ONCE on start. It does not, however, blink them when the subs/followers increase. How do I fix this?
Could you people have a quick look and tell me what my (probably very beginner-) mistake is? I would very much appreciate the help!
(Also: If you want to just chime in with "this is not how you are supposed to learn this and you totally bit of more than you can chew!" go ahead, I totally agree with you! You're soooo right! If you have a good beginner course you want to recommend I'd also be thankful :-) )
Code for blinking... Blinkt?
(edit: Added emphasis so this is actually a question instead of a ramble.)
to set the stage: I'm an absolute python newbie (Really, used it for the first time about a week ago) and I am also not a person that is in forums very much, so I apologize in advance if I break any unwritten rules!
Situation: As a novelty gift I was planning to build a notifier that blinks an LED when a new subscriber is added to the YouTube or Instagram account of my better half. That notifier is on a Pi Zero W with the Pimoroni Blinkt LED HAT. I figured out how to make it blink with the provided examples and also how to edit those examples. Yay me! Then I figured "Can't be that hard to have it check subscribers and blink red for new YT subscribers and green for IG followers!" Ohhh boy.
Long story short: With some help from a nice person I made up the stuff below and it actually does show the correct numbers for subs/follows in my SSH terminal window, it also blinks the leds ONCE on start. It does not, however, blink them when the subs/followers increase. How do I fix this?
Could you people have a quick look and tell me what my (probably very beginner-) mistake is? I would very much appreciate the help!
(Also: If you want to just chime in with "this is not how you are supposed to learn this and you totally bit of more than you can chew!" go ahead, I totally agree with you! You're soooo right! If you have a good beginner course you want to recommend I'd also be thankful :-) )
Code for blinking... Blinkt?
import blinkt import time # ---------------------------------------- blink_ontime = 2 def notify_new_sub_yt(): # blink RED for YouTube blinkt.set_all(255, 0, 0) blinkt.show() time.sleep(int(blink_ontime)) blinkt.set_all(0, 0, 0) blinkt.show() def notify_new_follower_insta(): # blink GREEN for InstaGram blinkt.set_all(0, 255, 0) blinkt.show() time.sleep(int(blink_ontime)) blinkt.set_all(0, 0, 0) blinkt.show()And the code that actually is checking for the counts(note: I named the LED ibrary "pOmoroni", but I carried that typo over everywhere, so that should be fine, right?):
import json import requests from threading import * import time from PomoroniBlinkt_blink import * # -------------------------------Global Variables---------------------------------------- YT_followers_count = 0 insta_followers_count = 0 YT_followers_count_old = 0 insta_followers_count_old = 0 # ------------------------Youtube Credentials------------------------------ GOOGLE_API_KEY = "fill_in_your_api_key" YOUTUBE_CHANNEL_ID = "your_channel_ID" # ----------------------Instagram Credentials------------------------------ # INSTA_ACCESS_TOKEN = "fill_in" INSTA_ACCESS_TOKEN = "fill_in" INSTA_ID = "fill_in" # ----------------Main Thread to check followers/subscription--------------------------- def followers_count(): global YT_followers_count global insta_followers_count global YT_followers_count_old global insta_followers_count_old # -------------------------------------API CALLS---------------------------------------------------- yt_url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + YOUTUBE_CHANNEL_ID + \ "&fields=items/statistics/subscriberCount&key=" + GOOGLE_API_KEY insta_url = "https://api.instagram.com/v1/users/"+INSTA_ID+"/?access_token="+INSTA_ACCESS_TOKEN while True: # ---------------------Get data from API endpoint------------------------------------------------------ yt_response = requests.get(yt_url) insta_response = requests.get(insta_url) # ----------------------format in json----------------------------------------------------------- insta_data = json.loads(insta_response.content) YT_data = json.loads(yt_response.content) # -----------------------Check if API request is successful------------------------------------ if yt_response.status_code == 200: YT_followers_count = int(YT_data['items'][0]['statistics']['subscriberCount']) print "Youtube followers are "+str(YT_followers_count) if insta_response.status_code == 200: insta_followers_count = int(insta_data['data']['counts']['followed_by']) print "Instagram followers are " + str(insta_followers_count) # ------------------------Check if there is a new subscriber/follower---------------------------- if YT_followers_count - YT_followers_count_old > 0: notify_new_sub_yt() YT_followers_count_old = YT_followers_count if insta_followers_count - insta_followers_count_old > 0: notify_new_follower_insta() insta_followers_count_old = insta_followers_count time.sleep(300) # ------------------------------------------------------------------------------------------------------------- def main(): t1 = Thread(target=followers_count) t1.daemon = True t1.start() while True: time.sleep(1) if __name__ == "__main__": main()