Advertisement
Guest User

Instagram.cpp

a guest
Feb 1st, 2019
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. /*
  2.  
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7.  
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12.  
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this library; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16.  
  17. Written By Brian Lough
  18. https://www.youtube.com/user/witnessmenow
  19. https://github.com/witnessmenow
  20. https://twitter.com/witnessmenow
  21.  
  22. */
  23.  
  24. #include "InstagramStats.h"
  25.  
  26. String currentKey = "";
  27. String currentParent = "";
  28. InstagramUserStats userStatsResponse;
  29.  
  30. //**************************************************************************//
  31. // This code is the JSON Parser code written by squix78 as part of his example,
  32. // modified for this application //
  33. // https://github.com/squix78/json-streaming-parser //
  34.  
  35. class InstagramUserListener : public JsonListener {
  36. public:
  37. virtual void whitespace(char c);
  38.  
  39. virtual void startDocument();
  40.  
  41. virtual void key(String key);
  42.  
  43. virtual void value(String value);
  44.  
  45. virtual void endArray();
  46.  
  47. virtual void endObject();
  48.  
  49. virtual void endDocument();
  50.  
  51. virtual void startArray();
  52.  
  53. virtual void startObject();
  54. };
  55.  
  56. void InstagramUserListener::whitespace(char c) {
  57. // Serial.println("whitespace");
  58. }
  59.  
  60. void InstagramUserListener::startDocument() {
  61. // Serial.println("start document");
  62. }
  63.  
  64. void InstagramUserListener::key(String key) {
  65. currentKey = key;
  66. }
  67.  
  68. void InstagramUserListener::value(String value) {
  69. if (currentParent == "edge_followed_by") {
  70. if (currentKey == "count") {
  71. userStatsResponse.followedByCount = value.toInt();
  72. }
  73. }
  74. }
  75.  
  76. void InstagramUserListener::endArray() {
  77. // Serial.println("end array. ");
  78. }
  79.  
  80. void InstagramUserListener::endObject() {
  81. currentParent = "";
  82. // Serial.println("end object. ");
  83. }
  84.  
  85. void InstagramUserListener::endDocument() {
  86. // Serial.println("end document. ");
  87. }
  88.  
  89. void InstagramUserListener::startArray() {
  90. // Serial.println("start array. ");
  91. }
  92.  
  93. void InstagramUserListener::startObject() {
  94. currentParent = currentKey;
  95. // Serial.println("start object. ");
  96. }
  97. //*********** END of Json Parser code **************//
  98.  
  99.  
  100. InstagramStats::InstagramStats(Client& client) {
  101. this->client = &client;
  102. }
  103.  
  104. InstagramUserStats InstagramStats::getUserStats(String user) {
  105. if (_debug) Serial.println("starting getUserStats function");
  106. JsonStreamingParser parser;
  107. InstagramUserListener listener;
  108. currentKey = "";
  109. currentParent = "";
  110. userStatsResponse = InstagramUserStats();
  111. parser.setListener(&listener);
  112.  
  113. String sharedDataBuffer;
  114. sharedDataBuffer.reserve(20);
  115. String sharedData = "window._sharedData = ";
  116. bool sharedDataFound = false;
  117.  
  118. long now;
  119. // Connect to Instagram over ssl
  120.  
  121. // It's failing to connect ever second time on version 2.3 of ESP8266, so lets try this knonsense
  122. if (client->connect(INSTA_HOST, INSTA_SSL_PORT) || client->connect(INSTA_HOST, INSTA_SSL_PORT)) {
  123. if (_debug) Serial.println(".... connected to server");
  124.  
  125. client->println("GET /" + user + "/ HTTP/1.1");
  126. client->print("Host:"); client->println(INSTA_HOST);
  127. client->println();
  128.  
  129. now = millis();
  130. while (millis() - now < 3000) {
  131. while (client->available()) {
  132. char c = client->read();
  133.  
  134.  
  135. if (_debug) Serial.print(c);
  136.  
  137. if(!sharedDataFound) {
  138. sharedDataBuffer = sharedDataBuffer + c;
  139. if(sharedData.indexOf(sharedDataBuffer) == 0 ){
  140. if (sharedData.length() == sharedDataBuffer.length()){
  141. sharedDataFound = true;
  142. }
  143. } else {
  144. sharedDataBuffer = "";
  145. }
  146. } else {
  147. // parsing code:
  148. // most of the work happens in the header code
  149. // at the top of this file
  150. parser.parse(c);
  151. }
  152.  
  153. // This is the check to see if you have everything you connected
  154. // as the library is expanded to include more info this will connected
  155. // to change.
  156. if (userStatsResponse.followedByCount > 0) {
  157. if (_debug) Serial.println("finished");
  158. closeClient();
  159. return userStatsResponse;
  160. }
  161. }
  162. }
  163. }
  164. closeClient();
  165. return userStatsResponse;
  166. }
  167.  
  168. void InstagramStats::closeClient() {
  169. if(client->connected()){
  170. client->stop();
  171. }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement