Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. #include "rapidjson/include/rapidjson/document.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <stdio.h>
  5. #include <curl/curl.h>
  6. #include <unistd.h>
  7. #include <unordered_map>
  8. #include <string>
  9.  
  10. using namespace rapidjson;
  11.  
  12. struct myData
  13. {
  14. std::fstream *file;
  15. std::string *str;
  16. };
  17.  
  18. size_t write_data(void *ptr, size_t size, size_t nmemb, myData *data)
  19. {
  20. size_t numBytes = size * nmemb;
  21.  
  22. if (data->file)
  23. data->file->write((char*)ptr, numBytes);
  24.  
  25. if (data->str)
  26. *data->str += std::string((char*)ptr, numBytes);
  27.  
  28. return numBytes;
  29. }
  30.  
  31. //function to get coin data and perform analysis
  32. int getData()
  33. {
  34. int count = 0;
  35.  
  36. //begin non terminating loop
  37. while(true)
  38. {
  39. count++;
  40. CURL *curl = curl_easy_init();
  41. if (curl)
  42. {
  43. curl_easy_setopt(curl, CURLOPT_URL, "http://pubapi.cryptsy.com/api.php?method=singlemarketdata&marketid=155");
  44.  
  45. std::fstream file("/home/coinz/cryptsy/myfile.txt", std::ios_base::out | std::ios_base::ate);
  46. std::string json;
  47.  
  48. myData data;
  49. data.file = &file;
  50. data.str = &json;
  51.  
  52. curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, &write_data);
  53. curl_easy_setopt(curl, CURLOPT_WRITEDATA, &data);
  54.  
  55. /* Perform the request, res will get the return code */
  56. CURLcode res = curl_easy_perform(curl);
  57.  
  58. /* Check for errors */
  59. if (res != CURLE_OK)
  60. {
  61. std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
  62. }
  63. else
  64. {
  65. file << std::endl;
  66.  
  67. //begin deserialization
  68. Document document;
  69. document.Parse(json.c_str());
  70. assert(document.HasMember("success"));
  71. assert(document["success"].IsString());
  72. //std::cout << "The Last Traded Price is = " << document["lasttradeprice"].GetString() << std::endl;
  73. }
  74.  
  75. /* always cleanup */
  76. curl_easy_cleanup(curl);
  77. }
  78.  
  79. //timer for URL request. *ADUJST ME AS DESIRED*
  80. usleep(10000000);
  81. }
  82.  
  83. return 0;
  84. }
  85.  
  86. //Le Main
  87. int main(void)
  88. {
  89. getData();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement