VLCPlexParser.m 10 KB

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