Advertisement
Guest User

Untitled

a guest
May 15th, 2016
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.81 KB | None | 0 0
  1. #include "plex-connect.h"
  2.  
  3. #import <Cocoa/Cocoa.h>
  4.  
  5. #include "restclient-cpp/restclient.h"
  6. #include "restclient-cpp/connection.h"
  7. #include <json/json.h>
  8. #include <string>
  9.  
  10. /*
  11. $host = "https://plex.tv/users/sign_in.json";
  12. $username = 'myPlexUser';
  13. $password = 'myPlexPass';
  14. $header = array(
  15. 'Content-Type: application/xml; charset=utf-8',
  16. 'Content-Length: 0',
  17. 'X-Plex-Client-Identifier: 8334-8A72-4C28-FDAF-29AB-479E-4069-C3A3',
  18. 'X-Plex-Product: Test',
  19. 'X-Plex-Version: v1_06',
  20. );
  21. $process = curl_init($host);
  22. curl_setopt($process, CURLOPT_HTTPHEADER, $header);
  23. curl_setopt($process, CURLOPT_HEADER, 0);
  24. curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  25. curl_setopt($process, CURLOPT_USERPWD, $username . ":" . $password);
  26. curl_setopt($process, CURLOPT_TIMEOUT, 30);
  27. curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
  28. curl_setopt($process, CURLOPT_POST, 1);
  29. curl_setopt($process, CURLOPT_RETURNTRANSFER, true);
  30. $data = curl_exec($process);
  31. $curlError = curl_error($process);
  32. $json = json_decode($data, true);
  33. */
  34.  
  35. CPlexConnect::CPlexConnect()
  36. : mConnection(nullptr)
  37. {
  38. }
  39.  
  40. CPlexConnect::~CPlexConnect()
  41. {
  42. }
  43.  
  44. void CPlexConnect::Open()
  45. {
  46. mConnection = new RestClient::Connection("http://192.168.2.202:32400");
  47.  
  48. //mConnection->SetTimeout(1);
  49. mConnection->SetBasicAuth("", "");
  50. //conn->SetBasicAuth("myPlexUser", "myPlexPass");
  51. mConnection->SetBasicAuth("", "");
  52. mConnection->AppendHeader("Accept", "application/json");
  53.  
  54. Json::Value root;
  55. std::string root_url = "";
  56. if (GetAndParse(root_url, root))
  57. {
  58.  
  59. bool allowChannelAccess = root.get("allowChannelAccess", "").asString() == "1";
  60. bool allowSync = root.get("allowSync", "").asString() == "1";
  61. bool backgroundProcessing = root.get("backgroundProcessing", "").asString() == "1";
  62. bool allowMediaDeletion = root.get("allowMediaDeletion", "").asString() == "1";
  63. bool transcoderAudio = root.get("transcoderAudio", "").asString() == "1";
  64. bool transcoderVideo = root.get("transcoderVideo", "").asString() == "1";
  65. std::string serverClass = root.get("allowMediaDeletion", "").asString();
  66. std::string version = root.get("version", "").asString();
  67. //std::string serverClass = root["allowMediaDeletion"].asString();
  68. //std::string version = root["version"].asString();
  69. // updatedAt
  70. // transcoderVideoResolutions
  71. // transcoderVideoBitrates
  72. // transcoderVideoQualities
  73.  
  74. Json::Value sections_root;
  75. std::string sections_root_url = "/library/sections";
  76. if (GetAndParse(sections_root_url, sections_root))
  77. {
  78. Json::Value sections = sections_root.get("_children", "");
  79. for (int i = 0; i < sections.size(); ++i)
  80. {
  81. NSLog(@"%s", sections[i]["key"].asString().c_str());
  82. NSLog(@"%s", sections[i]["title"].asString().c_str());
  83.  
  84. Json::Value section;
  85. std::string section_url = sections_root_url + "/";
  86. section_url += sections[i]["key"].asString() + "/all";
  87. if (GetAndParse(section_url, section))
  88. {
  89. Json::Value items = section.get("_children", "");
  90. for (int i = 0; i < items.size(); ++i)
  91. {
  92. //NSLog(@"%s", items[i]["key"].asString().c_str());
  93. NSLog(@"%s", items[i]["title"].asString().c_str());
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100.  
  101. bool CPlexConnect::GetAndParse(const std::string path, Json::Value &value)
  102. {
  103. bool parsingSuccessful = false;
  104. RestClient::Response res = mConnection->get(path);
  105. if (res.code == 200)
  106. {
  107. NSLog(@"%s", res.body.c_str());
  108.  
  109. Json::Reader reader;
  110. std::istringstream body(res.body);
  111. parsingSuccessful = reader.parse(body, value, false);
  112. }
  113.  
  114. return parsingSuccessful;
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement