VLCPlexParser.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 "VLCConstants.h"
  14. #define kPlexMediaServerDirInit @"library/sections"
  15. #define 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:(NSString *)port navigationPath:(NSString *)path authentification:(NSString *)auth
  25. {
  26. _containerInfo = [[NSMutableArray alloc] init];
  27. _dicoInfo = [[NSMutableDictionary alloc] init];
  28. _PlexMediaServerUrl = [NSString stringWithFormat:@"http://%@%@",address, port];
  29. NSString *mediaServerUrl;
  30. if ([path isEqualToString:@""])
  31. mediaServerUrl = [NSString stringWithFormat:@"%@/%@",_PlexMediaServerUrl, kPlexMediaServerDirInit];
  32. else {
  33. if ([path rangeOfString:@"library"].location != NSNotFound)
  34. mediaServerUrl = [NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, path];
  35. else
  36. mediaServerUrl = [NSString stringWithFormat:@"%@/%@/%@",_PlexMediaServerUrl, kPlexMediaServerDirInit, path];
  37. }
  38. VLCPlexWebAPI *PlexWebAPI = [[VLCPlexWebAPI alloc] init];
  39. NSURL *url = [[NSURL alloc] initWithString:[PlexWebAPI urlAuth:mediaServerUrl autentification:auth]];
  40. NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  41. NSHTTPURLResponse *response = nil;
  42. NSError *error = nil;
  43. NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  44. if ([response statusCode] != 200) {
  45. NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  46. if([responseString rangeOfString:@"Unauthorized"].location != NSNotFound) {
  47. NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
  48. NSString *username = [prefs stringForKey:kVLCPLEXLogin];
  49. NSString *password = [prefs stringForKey:kVLCPLEXPassword];
  50. if ((username && password) && ((![username isEqualToString:@""]) && (![password isEqualToString:@""]))) {
  51. auth = [PlexWebAPI PlexAuthentification:username password:password];
  52. url = [NSURL URLWithString:[PlexWebAPI urlAuth:mediaServerUrl autentification:auth]];
  53. request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  54. response = nil;
  55. error = nil;
  56. data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  57. if ([response statusCode] != 200) {
  58. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"PLEX_ERROR_ACCOUNT", nil) message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil) cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  59. [alertView show];
  60. }
  61. [_containerInfo removeAllObjects];
  62. [_dicoInfo removeAllObjects];
  63. } else {
  64. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"UNAUTHORIZED", nil) message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil) cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  65. [alertView show];
  66. }
  67. } else
  68. APLog(@"PlexParser url Errors : %ld", (long)[response statusCode]);
  69. }
  70. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  71. [xmlparser setDelegate:self];
  72. if (![xmlparser parse])
  73. APLog(@"PlexParser url Errors : %@", url);
  74. [_containerInfo setValue:auth forKey:@"authentification"];
  75. return [NSArray arrayWithArray:_containerInfo];
  76. }
  77. - (NSArray *)PlexExtractDeviceInfo:(NSData *)data
  78. {
  79. _containerInfo = [[NSMutableArray alloc] init];
  80. _dicoInfo = [[NSMutableDictionary alloc] init];
  81. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  82. [xmlparser setDelegate:self];
  83. if (![xmlparser parse])
  84. APLog(@"PlexParser data Errors : %@", data);
  85. return [NSArray arrayWithArray:_containerInfo];
  86. }
  87. #pragma mark - Parser
  88. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  89. {
  90. if ([elementName isEqualToString:@"MediaContainer"]) {
  91. if ([attributeDict objectForKey:@"friendlyName"])
  92. [_dicoInfo setObject:[attributeDict objectForKey:@"friendlyName"] forKey:@"libTitle"];
  93. else if ([attributeDict objectForKey:@"title1"])
  94. [_dicoInfo setObject:[attributeDict objectForKey:@"title1"] forKey:@"libTitle"];
  95. if ([attributeDict objectForKey:@"title2"])
  96. [_dicoInfo setObject:[attributeDict objectForKey:@"title2"] forKey:@"libTitle"];
  97. if ([attributeDict objectForKey:@"grandparentTitle"])
  98. [_dicoInfo setObject:[attributeDict objectForKey:@"grandparentTitle"] forKey:@"grandparentTitle"];
  99. } else if ([elementName isEqualToString:@"Directory"]) {
  100. [_dicoInfo setObject:@"directory" forKey:@"container"];
  101. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  102. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  103. } else if ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"]) {
  104. [_dicoInfo setObject:@"item" forKey:@"container"];
  105. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  106. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  107. [_dicoInfo setObject:[attributeDict objectForKey:@"ratingKey"] forKey:@"ratingKey"];
  108. if ([attributeDict objectForKey:@"summary"])
  109. [_dicoInfo setObject:[attributeDict objectForKey:@"summary"] forKey:@"summary"];
  110. if ([attributeDict objectForKey:@"viewCount"])
  111. [_dicoInfo setObject:@"watched" forKey:@"state"];
  112. else
  113. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  114. } else if ([elementName isEqualToString:@"Media"]) {
  115. if ([attributeDict objectForKey:@"audioCodec"])
  116. [_dicoInfo setObject:[attributeDict objectForKey:@"audioCodec"] forKey:@"audioCodec"];
  117. if ([attributeDict objectForKey:@"videoCodec"])
  118. [_dicoInfo setObject:[attributeDict objectForKey:@"videoCodec"] forKey:@"videoCodec"];
  119. } else if ([elementName isEqualToString:@"Part"]) {
  120. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  121. if([attributeDict objectForKey:@"file"])
  122. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  123. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  124. [_dicoInfo setObject:duration forKey:@"duration"];
  125. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  126. if (sizeFile)
  127. [_dicoInfo setObject:sizeFile forKey:@"size"];
  128. } else if ([elementName isEqualToString:@"Stream"]) {
  129. if ([attributeDict objectForKey:@"key"])
  130. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  131. if ([attributeDict objectForKey:@"codec"])
  132. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  133. if ([attributeDict objectForKey:@"language"])
  134. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  135. if ([attributeDict objectForKey:@"languageCode"])
  136. [_dicoInfo setObject:[attributeDict objectForKey:@"languageCode"] forKey:@"languageCode"];
  137. } else if ([elementName isEqualToString:@"Device"] && [[attributeDict objectForKey:@"name"] isEqualToString:kPlexVLCDeviceName]) {
  138. if ([attributeDict objectForKey:@"name"])
  139. [_dicoInfo setObject:[attributeDict objectForKey:@"name"] forKey:@"name"];
  140. if ([attributeDict objectForKey:@"product"])
  141. [_dicoInfo setObject:[attributeDict objectForKey:@"product"] forKey:@"product"];
  142. if ([attributeDict objectForKey:@"productVersion"])
  143. [_dicoInfo setObject:[attributeDict objectForKey:@"productVersion"] forKey:@"productVersion"];
  144. if ([attributeDict objectForKey:@"platformVersion"])
  145. [_dicoInfo setObject:[attributeDict objectForKey:@"platformVersion"] forKey:@"platformVersion"];
  146. if ([attributeDict objectForKey:@"token"])
  147. [_dicoInfo setObject:[attributeDict objectForKey:@"token"] forKey:@"token"];
  148. if ([attributeDict objectForKey:@"clientIdentifier"])
  149. [_dicoInfo setObject:[attributeDict objectForKey:@"clientIdentifier"] forKey:@"clientIdentifier"];
  150. if ([attributeDict objectForKey:@"id"])
  151. [_dicoInfo setObject:[attributeDict objectForKey:@"id"] forKey:@"id"];
  152. }
  153. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  154. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  155. }
  156. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  157. {
  158. if (([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"] || [elementName isEqualToString:@"Device"]) && [_dicoInfo count] > 0) {
  159. [_containerInfo addObject:_dicoInfo];
  160. _dicoInfo = [[NSMutableDictionary alloc] init];
  161. }
  162. }
  163. @end