VLCPlexParser.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. #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. NSString *serviceString = [NSString stringWithFormat:@"plex://%@%@", address, port];
  48. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  49. if (!accounts) {
  50. serviceString = @"plex://Account";
  51. accounts = [SSKeychain accountsForService:serviceString];
  52. }
  53. if (accounts) {
  54. NSDictionary *account = [accounts firstObject];
  55. NSString *username = [account objectForKey:@"acct"];
  56. NSString *password = [SSKeychain passwordForService:serviceString account:username];
  57. auth = [PlexWebAPI PlexAuthentification:username password:password];
  58. url = [NSURL URLWithString:[PlexWebAPI urlAuth:mediaServerUrl autentification:auth]];
  59. request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  60. response = nil;
  61. error = nil;
  62. data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  63. if ([response statusCode] != 200) {
  64. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"PLEX_ERROR_ACCOUNT", nil) message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil) cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  65. [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
  66. }
  67. [_containerInfo removeAllObjects];
  68. [_dicoInfo removeAllObjects];
  69. } else {
  70. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"UNAUTHORIZED", 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. } else
  74. APLog(@"PlexParser url Errors : %ld", (long)[response statusCode]);
  75. }
  76. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  77. [xmlparser setDelegate:self];
  78. if (![xmlparser parse])
  79. APLog(@"PlexParser url Errors : %@", url);
  80. [_containerInfo setValue:auth forKey:@"authentification"];
  81. return [NSArray arrayWithArray:_containerInfo];
  82. }
  83. - (NSArray *)PlexExtractDeviceInfo:(NSData *)data
  84. {
  85. _containerInfo = [[NSMutableArray alloc] init];
  86. _dicoInfo = [[NSMutableDictionary alloc] init];
  87. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithData:data];
  88. [xmlparser setDelegate:self];
  89. if (![xmlparser parse])
  90. APLog(@"PlexParser data Errors : %@", data);
  91. return [NSArray arrayWithArray:_containerInfo];
  92. }
  93. #pragma mark - Parser
  94. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  95. {
  96. if ([elementName isEqualToString:@"MediaContainer"]) {
  97. if ([attributeDict objectForKey:@"friendlyName"])
  98. [_dicoInfo setObject:[attributeDict objectForKey:@"friendlyName"] forKey:@"libTitle"];
  99. else if ([attributeDict objectForKey:@"title1"])
  100. [_dicoInfo setObject:[attributeDict objectForKey:@"title1"] forKey:@"libTitle"];
  101. if ([attributeDict objectForKey:@"title2"])
  102. [_dicoInfo setObject:[attributeDict objectForKey:@"title2"] forKey:@"libTitle"];
  103. if ([attributeDict objectForKey:@"grandparentTitle"])
  104. [_dicoInfo setObject:[attributeDict objectForKey:@"grandparentTitle"] forKey:@"grandparentTitle"];
  105. } else if ([elementName isEqualToString:@"Directory"]) {
  106. [_dicoInfo setObject:@"directory" forKey:@"container"];
  107. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  108. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  109. } else if ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"]) {
  110. [_dicoInfo setObject:@"item" forKey:@"container"];
  111. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  112. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  113. [_dicoInfo setObject:[attributeDict objectForKey:@"ratingKey"] forKey:@"ratingKey"];
  114. if ([attributeDict objectForKey:@"summary"])
  115. [_dicoInfo setObject:[attributeDict objectForKey:@"summary"] forKey:@"summary"];
  116. if ([attributeDict objectForKey:@"viewCount"])
  117. [_dicoInfo setObject:@"watched" forKey:@"state"];
  118. else
  119. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  120. } else if ([elementName isEqualToString:@"Media"]) {
  121. if ([attributeDict objectForKey:@"audioCodec"])
  122. [_dicoInfo setObject:[attributeDict objectForKey:@"audioCodec"] forKey:@"audioCodec"];
  123. if ([attributeDict objectForKey:@"videoCodec"])
  124. [_dicoInfo setObject:[attributeDict objectForKey:@"videoCodec"] forKey:@"videoCodec"];
  125. } else if ([elementName isEqualToString:@"Part"]) {
  126. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  127. if([attributeDict objectForKey:@"file"])
  128. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  129. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  130. [_dicoInfo setObject:duration forKey:@"duration"];
  131. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  132. if (sizeFile)
  133. [_dicoInfo setObject:sizeFile forKey:@"size"];
  134. } else if ([elementName isEqualToString:@"Stream"]) {
  135. if ([attributeDict objectForKey:@"key"])
  136. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  137. if ([attributeDict objectForKey:@"codec"])
  138. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  139. if ([attributeDict objectForKey:@"language"])
  140. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  141. if ([attributeDict objectForKey:@"languageCode"])
  142. [_dicoInfo setObject:[attributeDict objectForKey:@"languageCode"] forKey:@"languageCode"];
  143. } else if ([elementName isEqualToString:@"Device"] && [[attributeDict objectForKey:@"name"] isEqualToString:kPlexVLCDeviceName]) {
  144. if ([attributeDict objectForKey:@"name"])
  145. [_dicoInfo setObject:[attributeDict objectForKey:@"name"] forKey:@"name"];
  146. if ([attributeDict objectForKey:@"product"])
  147. [_dicoInfo setObject:[attributeDict objectForKey:@"product"] forKey:@"product"];
  148. if ([attributeDict objectForKey:@"productVersion"])
  149. [_dicoInfo setObject:[attributeDict objectForKey:@"productVersion"] forKey:@"productVersion"];
  150. if ([attributeDict objectForKey:@"platformVersion"])
  151. [_dicoInfo setObject:[attributeDict objectForKey:@"platformVersion"] forKey:@"platformVersion"];
  152. if ([attributeDict objectForKey:@"token"])
  153. [_dicoInfo setObject:[attributeDict objectForKey:@"token"] forKey:@"token"];
  154. if ([attributeDict objectForKey:@"clientIdentifier"])
  155. [_dicoInfo setObject:[attributeDict objectForKey:@"clientIdentifier"] forKey:@"clientIdentifier"];
  156. if ([attributeDict objectForKey:@"id"])
  157. [_dicoInfo setObject:[attributeDict objectForKey:@"id"] forKey:@"id"];
  158. }
  159. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  160. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  161. }
  162. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  163. {
  164. if (([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"] || [elementName isEqualToString:@"Device"]) && [_dicoInfo count] > 0) {
  165. [_containerInfo addObject:_dicoInfo];
  166. _dicoInfo = [[NSMutableDictionary alloc] init];
  167. }
  168. }
  169. @end