Advertisement
cheapassreviews

Untitled

Jan 16th, 2017
5,117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. """
  4. Raspberry Pi Youtube Counter for CheapAssReviews Channel
  5.  
  6. Cheapassreviews.com
  7.  
  8. Helped me:
  9. http://stackoverflow.com/questions/36252027/how-to-use-youtube-api-v3-for-realtime-subscribers
  10. https://max7219.readthedocs.io/en/latest/
  11. http://raspi.tv/2013/8-x-8-led-array-driven-by-max7219-on-the-raspberry-pi-via-python
  12. http://www.raspberrypi-spy.co.uk/2012/06/simple-guide-to-the-rpi-gpio-header-and-pins/
  13. http://raspberrypi.stackexchange.com/questions/8734/execute-script-on-start-up
  14. """
  15. import RPi.GPIO as GPIO
  16. import math
  17. import os
  18. import sys
  19.  
  20. import httplib2
  21. import json
  22. import urllib
  23.  
  24. import time
  25. import random
  26. import socket
  27.  
  28. from select import select
  29.  
  30. import max7219.led as led
  31.  
  32. #channel id and API key
  33. channel_id = "ENTER YOUR CHANNEL ID HERE" # channel id
  34. #channel_id = "UCtinbF-Q-fVthA0qrFQTgXQ" # casey
  35. api_key = "ENTER YOUR API KEY HERE"
  36. lookup_url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + channel_id + "&key=" + api_key
  37.  
  38. #Change brightness here
  39. brightness_setting = 1 # I hate bright lights
  40.  
  41.  
  42. def is_connected():
  43.     try:
  44.         host = socket.gethostbyname("www.google.com")
  45.         s = socket.create_connection((host, 80), 2)
  46.         return True
  47.     except:
  48.         pass
  49.     return False
  50.  
  51.  
  52. def main():
  53.     # create seven segment device
  54.     device = led.sevensegment(cascaded=1)
  55.  
  56.     device.brightness(brightness_setting)
  57.     device.clear()
  58.    
  59.     while 1:
  60.        
  61.         try:
  62.             # Catches the webpage from google
  63.             soup = urllib.urlopen(lookup_url)
  64.             markup = soup.read()
  65.            
  66.             # Access the part of the JSON object that we care about
  67.             feed_json = json.loads(markup)
  68.             sub_count = feed_json["items"][0]["statistics"]["subscriberCount"]
  69.  
  70.             # Tells us how great we are (writes to display
  71.             device.write_text(0, sub_count)
  72.  
  73.         except:
  74.             # If can't get new number, screen goes blank
  75.             device.clear()
  76.            
  77.         time.sleep(1)
  78.  
  79.  
  80.  
  81. if __name__ == '__main__':
  82.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement