VLCPlexParser.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. /*****************************************************************************
  2. * VLCPlexParser.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2015 VideoLAN. All rights reserved.
  6. *
  7. * Authors: Pierre Sagaspe <pierre.sagaspe # me.com>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCPlexParser.h"
  12. #import "VLCPlexWebAPI.h"
  13. #import "SSKeychain.h"
  14. static NSString *const kPlexMediaServerDirInit = @"/library/sections";
  15. static NSString *const kPlexVLCDeviceName = @"VLC for iOS";
  16. @interface VLCPlexParser () <NSXMLParserDelegate>
  17. {
  18. NSMutableArray *_containerInfo;
  19. NSMutableDictionary *_dicoInfo;
  20. NSString *_PlexMediaServerUrl;
  21. }
  22. @end
  23. @implementation VLCPlexParser
  24. - (NSArray *)PlexMediaServerParser:(NSString *)address port:(NSNumber *)port navigationPath:(NSString *)path authentification:(NSString *)auth
  25. {
  26. _containerInfo = [[NSMutableArray alloc] init];
  27. _dicoInfo = [[NSMutableDictionary alloc] init];
  28. NSURLComponents *urlComponents = [[NSURLComponents alloc] init];
  29. urlComponents.scheme = @"http";
  30. urlComponents.host = address;
  31. urlComponents.port = port;
  32. _PlexMediaServerUrl = [urlComponents URL].absoluteString;
  33. NSString *mediaServerUrl;
  34. if ([path length] == 0) {
  35. urlComponents.path = kPlexMediaServerDirInit;
  36. } else {
  37. if ([path rangeOfString:@"library"].location != NSNotFound) {
  38. urlComponents.path = [@"/" stringByAppendingPathComponent:path];
  39. } else {
  40. urlComponents.path = [kPlexMediaServerDirInit stringByAppendingPathComponent:path];
  41. }
  42. }
  43. mediaServerUrl = [urlComponents URL].absoluteString;
  44. VLCPlexWebAPI *PlexWebAPI = [[VLCPlexWebAPI alloc] init];
  45. NSURL *url = [[NSURL alloc] initWithString:[PlexWebAPI urlAuth:mediaServerUrl authentification:auth]];
  46. NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  47. NSHTTPURLResponse *response = nil;
  48. NSError *error = nil;
  49. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  50. if ([response statusCode] != 200) {
  51. NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  52. if([responseString rangeOfString:@"Unauthorized"].location != NSNotFound) {
  53. NSString *serviceString = [NSString stringWithFormat:@"plex://%@%@", address, port];
  54. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  55. if (!accounts) {
  56. serviceString = @"plex://Account";
  57. accounts = [SSKeychain accountsForService:serviceString];
  58. }
  59. if (accounts) {
  60. NSDictionary *account = [accounts firstObject];
  61. NSString *username = [account objectForKey:@"acct"];
  62. NSString *password = [SSKeychain passwordForService:serviceString account:username];
  63. auth = [PlexWebAPI PlexAuthentification:username password:password];
  64. url = [NSURL URLWithString:[PlexWebAPI urlAuth:mediaServerUrl authentification:auth]];
  65. request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  66. response = nil;
  67. error = nil;
  68. data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  69. if ([response statusCode] != 200) {
  70. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"PLEX_ERROR_ACCOUNT", nil) message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil) cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  71. [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
  72. }
  73. [_containerInfo removeAllObjects];
  74. [_dicoInfo removeAllObjects];
  75. } else {
  76. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"UNAUTHORIZED", nil) message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil) cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  77. [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
  78. }
  79. } else
  80. APLog(@"PlexParser url Errors : %ld", (long)[response statusCode]);
  81. }
  82. if (error) {
  83. APLog(@"PlexParser url Error: %@", error);
  84. }
  85. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  86. [xmlparser setDelegate:self];
  87. if (![xmlparser parse])
  88. APLog(@"PlexParser url Errors : %@", url);
  89. [_containerInfo setValue:auth forKey:@"authentification"];
  90. return [NSArray arrayWithArray:_containerInfo];
  91. }
  92. - (NSArray *)PlexExtractDeviceInfo:(NSData *)data
  93. {
  94. _containerInfo = [[NSMutableArray alloc] init];
  95. _dicoInfo = [[NSMutableDictionary alloc] init];
  96. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  97. [xmlparser setDelegate:self];
  98. if (![xmlparser parse])
  99. APLog(@"PlexParser data Errors : %@", data);
  100. return [NSArray arrayWithArray:_containerInfo];
  101. }
  102. #pragma mark - Parser
  103. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  104. {
  105. if ([elementName isEqualToString:@"MediaContainer"]) {
  106. if ([attributeDict objectForKey:@"friendlyName"])
  107. [_dicoInfo setObject:[attributeDict objectForKey:@"friendlyName"] forKey:@"libTitle"];
  108. else if ([attributeDict objectForKey:@"title1"])
  109. [_dicoInfo setObject:[attributeDict objectForKey:@"title1"] forKey:@"libTitle"];
  110. if ([attributeDict objectForKey:@"title2"])
  111. [_dicoInfo setObject:[attributeDict objectForKey:@"title2"] forKey:@"libTitle"];
  112. if ([attributeDict objectForKey:@"grandparentTitle"])
  113. [_dicoInfo setObject:[attributeDict objectForKey:@"grandparentTitle"] forKey:@"grandparentTitle"];
  114. } else if ([elementName isEqualToString:@"Directory"]) {
  115. [_dicoInfo setObject:@"directory" forKey:@"container"];
  116. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  117. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  118. } else if ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"]) {
  119. [_dicoInfo setObject:@"item" forKey:@"container"];
  120. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  121. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  122. [_dicoInfo setObject:[attributeDict objectForKey:@"ratingKey"] forKey:@"ratingKey"];
  123. if ([attributeDict objectForKey:@"summary"])
  124. [_dicoInfo setObject:[attributeDict objectForKey:@"summary"] forKey:@"summary"];
  125. if ([attributeDict objectForKey:@"viewCount"])
  126. [_dicoInfo setObject:@"watched" forKey:@"state"];
  127. else
  128. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  129. } else if ([elementName isEqualToString:@"Media"]) {
  130. if ([attributeDict objectForKey:@"audioCodec"])
  131. [_dicoInfo setObject:[attributeDict objectForKey:@"audioCodec"] forKey:@"audioCodec"];
  132. if ([attributeDict objectForKey:@"videoCodec"])
  133. [_dicoInfo setObject:[attributeDict objectForKey:@"videoCodec"] forKey:@"videoCodec"];
  134. } else if ([elementName isEqualToString:@"Part"]) {
  135. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  136. if([attributeDict objectForKey:@"file"])
  137. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  138. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  139. [_dicoInfo setObject:duration forKey:@"duration"];
  140. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  141. if (sizeFile)
  142. [_dicoInfo setObject:sizeFile forKey:@"size"];
  143. } else if ([elementName isEqualToString:@"Stream"]) {
  144. if ([attributeDict objectForKey:@"key"])
  145. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  146. if ([attributeDict objectForKey:@"codec"])
  147. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  148. if ([attributeDict objectForKey:@"language"])
  149. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  150. if ([attributeDict objectForKey:@"languageCode"])
  151. [_dicoInfo setObject:[attributeDict objectForKey:@"languageCode"] forKey:@"languageCode"];
  152. } else if ([elementName isEqualToString:@"Device"] && [[attributeDict objectForKey:@"name"] isEqualToString:kPlexVLCDeviceName]) {
  153. if ([attributeDict objectForKey:@"name"])
  154. [_dicoInfo setObject:[attributeDict objectForKey:@"name"] forKey:@"name"];
  155. if ([attributeDict objectForKey:@"product"])
  156. [_dicoInfo setObject:[attributeDict objectForKey:@"product"] forKey:@"product"];
  157. if ([attributeDict objectForKey:@"productVersion"])
  158. [_dicoInfo setObject:[attributeDict objectForKey:@"productVersion"] forKey:@"productVersion"];
  159. if ([attributeDict objectForKey:@"platformVersion"])
  160. [_dicoInfo setObject:[attributeDict objectForKey:@"platformVersion"] forKey:@"platformVersion"];
  161. if ([attributeDict objectForKey:@"token"])
  162. [_dicoInfo setObject:[attributeDict objectForKey:@"token"] forKey:@"token"];
  163. if ([attributeDict objectForKey:@"clientIdentifier"])
  164. [_dicoInfo setObject:[attributeDict objectForKey:@"clientIdentifier"] forKey:@"clientIdentifier"];
  165. if ([attributeDict objectForKey:@"id"])
  166. [_dicoInfo setObject:[attributeDict objectForKey:@"id"] forKey:@"id"];
  167. }
  168. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  169. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  170. }
  171. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  172. {
  173. if (([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"] || [elementName isEqualToString:@"Device"]) && [_dicoInfo count] > 0) {
  174. [_containerInfo addObject:_dicoInfo];
  175. _dicoInfo = [[NSMutableDictionary alloc] init];
  176. }
  177. }
  178. @end