Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.90 KB | None | 0 0
  1. /*******************************************************************
  2.  *  Here is the Sourcecode for my Social Media Counter
  3.  *  
  4.  *  
  5.  *  
  6.  *  This Social Media Counter is based on an idea and sourcecode from Becky Stern                  
  7.  *  
  8.  *  Have a look at her Social media channels
  9.  *  https://youtu.be/3Q2JZhgOsG4
  10.  *  https://www.instructables.com/id/EOGVPMLJD2FC7WM/
  11.  *******************************************************************/
  12.  
  13. // requires the following libraries, search in Library Manager or download from github:
  14.  
  15. #include <Wire.h>                  // installed by default
  16. #include <TwitterApi.h>            // https://github.com/witnessmenow/arduino-twitter-api
  17. #include <ArduinoJson.h>           // V5.1.3  // https://github.com/bblanchon/ArduinoJson    
  18. #include "InstagramStats.h"        // https://github.com/witnessmenow/arduino-instagram-stats
  19. #include "JsonStreamingParser.h"   // https://github.com/squix78/json-streaming-parser  
  20. #include <YoutubeApi.h>            // https://github.com/witnessmenow/arduino-youtube-api
  21. #include <TM1637Display.h>         // https://github.com/avishorp/TM1637
  22. //#include <Adafruit_GFX.h>          // https://github.com/adafruit/Adafruit-GFX-Library
  23. //#include "Adafruit_LEDBackpack.h"  // https://github.com/adafruit/Adafruit_LED_Backpack
  24. #include "DigitLedDisplay.h"        // https://github.com/ozhantr/DigitLedDisplay
  25. #include "TM1637.h"                 //  https://github.com/Seeed-Studio/Grove_4Digital_Display
  26.  
  27. // these libraries are included with ESP8266 support
  28. #include <ESP8266WiFi.h>
  29. #include <WiFiClientSecure.h>
  30.  
  31. //------- Replace the following! ------
  32. char ssid[] = "xxxxxxxxxxxxxxx";       // your network SSID (name)
  33. char password[] = "xxxxxxxxxx";  // your network key
  34.  
  35. // Twitter Details
  36. //******************************
  37. // Normally we would use these to generate the bearer token but its not working yet :/
  38. // Use steps on the readme to generate the Bearer Token
  39.  
  40. #define BEARER_TOKEN "Your Token"
  41. bool haveBearerToken = false;
  42.  
  43. //Using curl to get bearer token
  44. // curl -u "$CONSUMER_KEY:$CONSUMER_SECRET" \
  45. //    --data 'grant_type=client_credentials' \
  46. //    'https://api.twitter.com/oauth2/token'
  47.  
  48. WiFiClientSecure secureClient;
  49. WiFiClient client;
  50.  
  51. TwitterApi TwitterStats(secureClient);
  52. InstagramStats instaStats(secureClient);
  53.  
  54. unsigned long api_delay = 1 * 60000; //time between api requests (1mins)
  55. unsigned long api_due_time;
  56.  
  57. //Inputs
  58.  
  59. String screenName = "your Twittername";    // Your Twittername here
  60. String userName = "xxxxxxxxxxx";    // Yout Instagramname here
  61.  
  62. // YouTube Details
  63. // *************************
  64. // google API key
  65. // create yours: https://support.google.com/cloud/answer/6158862?hl=en
  66. #define API_KEY "your API key"
  67. // youtube channel ID
  68. // find yours: https://support.google.com/youtube/answer/3250431?hl=en
  69. #define CHANNEL_ID "your Channel ID"
  70. //YoutubeApi api(API_KEY, secureClient);
  71.  
  72. unsigned long api_mtbs = 1000; //mean time between api requests
  73. unsigned long api_lasttime;   //last time api request has been done
  74.  
  75. long subs = 0;
  76.  
  77. // Facebook Details
  78. //***************************
  79. String page_url = "xxxxxxxxxxxxx";              
  80. const char* host = "mbasic.facebook.com";
  81. const char* fingerprint = "93:6F:91:2B:AF:AD:21:6F:A5:15:25:6E:57:2C:DC:35:A1:45:1A:A5";
  82.  
  83. // 7 Segment Displays
  84. //***************************
  85. const int CLK_TW = D1;
  86. const int DIO_TW = D2;
  87. TM1637Display displayTW(CLK_TW, DIO_TW);
  88.  
  89. const int CLK_FB = D1;
  90. const int DIO_FB = D3;
  91. TM1637Display displayFB(CLK_FB, DIO_FB);
  92.  
  93. const int CLK_YT = D1;
  94. const int DIO_YT = D4;
  95. TM1637Display displayYT(CLK_YT, DIO_YT);
  96.  
  97. const int CLK_IN = D1;
  98. const int DIO_IN = D5;
  99. DigitLedDisplay displayIN(CLK_IN, DIO_IN);
  100. //TM1637Display displayIN(CLK_IN, DIO_IN);
  101.  
  102. void setup() {
  103.   Serial.begin(115200);
  104.   WiFi.mode(WIFI_STA);
  105.   WiFi.disconnect();
  106.   delay(100);
  107.  
  108.   // Attempt to connect to Wifi network:
  109.   Serial.println("");
  110.   Serial.print("Connecting Wifi: ");
  111.   Serial.println(ssid);
  112.   WiFi.begin(ssid, password);
  113.   while (WiFi.status() != WL_CONNECTED) {
  114.     Serial.print(".");
  115.     delay(500);
  116.   }
  117.   Serial.println("");
  118.   Serial.println("WiFi connected");
  119.   Serial.print("IP address : ");
  120.   IPAddress ip = WiFi.localIP();
  121.   Serial.println(ip);
  122.   Serial.println("******************");
  123.   TwitterStats.setBearerToken(BEARER_TOKEN);
  124.   haveBearerToken = true;
  125.      
  126.      displayTW.setBrightness(15);
  127.      displayYT.setBrightness(15);
  128.      displayIN.setBrightness(15);
  129.      displayFB.setBrightness(15);
  130. }
  131.  
  132. void loop() {
  133.  if (millis() > api_due_time)  {
  134.     if(haveBearerToken){
  135.       getTwitterStats(screenName);
  136.       }
  137.       getInstagramStatsForUser();
  138.       getFacebookUsers();  
  139.       getYoutubeUsers();
  140.  
  141.    delay(10000);
  142.    // api_due_time = millis() + api_delay;
  143.     Serial.println("******************");
  144.   }
  145. }
  146.  
  147. void getTwitterStats(String name) {
  148.    String responseString = TwitterStats.getUserStatistics(name);
  149.      DynamicJsonBuffer jsonBuffer;
  150.      JsonObject& response = jsonBuffer.parseObject(responseString);
  151.    if (response.success()) {
  152.       int followerCount = response["followers_count"];
  153.       Serial.print("Twitter Followers   : ");
  154.       Serial.println(followerCount);
  155.      
  156.       displayTW.showNumberDec(followerCount);
  157.  
  158.     } else {
  159.       Serial.println("Failed to parse Json");
  160.     }
  161. }
  162.  
  163. void getInstagramStatsForUser() {
  164.   InstagramUserStats response = instaStats.getUserStats(userName);
  165.   Serial.print("Instagram Followers : ");
  166.       int like_in = response.followedByCount;
  167.       Serial.println(like_in);
  168.       displayIN.showNumberDec(like_in);
  169.  
  170. }
  171.  
  172. void getYoutubeUsers() {
  173.  if (millis() > api_lasttime + api_mtbs)  {
  174.     if(api.getChannelStatistics(CHANNEL_ID))
  175.     {
  176.       Serial.print("Youtube Subscriber  : ");
  177.       Serial.println(api.channelStats.subscriberCount);
  178.  
  179.             displayYT.showNumberDec(api.channelStats.subscriberCount);
  180.  
  181.      }
  182.     api_lasttime = millis();
  183.   }
  184. }
  185.  
  186. void getFacebookUsers() {
  187.   if (!secureClient.connect(host, 443)) { Serial.println("connection failed");}
  188.  
  189.   secureClient.verify(fingerprint, host);
  190.   String url = "/"+page_url+"/community?locale=en_US";  //
  191.   secureClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
  192.                "Host: " + host + "\r\n" +              
  193.                "User-Agent: ESP8266\r\n" +
  194.                "Connection: close\r\n\r\n");
  195.    while (secureClient.connected()) {
  196.       String line = secureClient.readStringUntil('>');
  197.       if(line.startsWith("<meta name=\"description\"")){      
  198.       int st = line.indexOf(".")+2;
  199.       int sp = line.indexOf("likes",st)-1;
  200.       int st2 = sp + 14;
  201.       int sp2 = line.indexOf("talking",st2)-1;
  202.       String like = line.substring(st,sp);
  203.       String talk = line.substring(st2,sp2);
  204.       Serial.print("Facebook Likes      : ");
  205.       Serial.println(like);
  206.           int like_i = (like.toInt());
  207.           displayFB.showNumberDec(like_i);
  208.   }
  209.   }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement