VLCPlexParser.m 10 KB

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