VLCPlexParser.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "UIDevice+VLC.h"
  13. #define kPlexMediaServerDirInit @"library/sections"
  14. @interface VLCPlexParser () <NSXMLParserDelegate>
  15. {
  16. NSMutableArray *_containerInfo;
  17. NSMutableDictionary *_dicoInfo;
  18. NSString *_PlexMediaServerUrl;
  19. }
  20. @end
  21. @implementation VLCPlexParser
  22. - (NSMutableArray *)PlexMediaServerParser:(NSString *)adress port:(NSString *)port navigationPath:(NSString *)path
  23. {
  24. _containerInfo = [[NSMutableArray alloc] init];
  25. [_containerInfo removeAllObjects];
  26. _dicoInfo = [[NSMutableDictionary alloc] init];
  27. _PlexMediaServerUrl = [NSString stringWithFormat:@"http://%@%@",adress, port];
  28. NSString *mediaServerUrl;
  29. if ([path isEqualToString:@""])
  30. mediaServerUrl = [NSString stringWithFormat:@"%@/%@",_PlexMediaServerUrl, kPlexMediaServerDirInit];
  31. else {
  32. if ([path rangeOfString:@"library"].location != NSNotFound)
  33. mediaServerUrl = [NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, path];
  34. else
  35. mediaServerUrl = [NSString stringWithFormat:@"%@/%@/%@",_PlexMediaServerUrl, kPlexMediaServerDirInit, path];
  36. }
  37. NSURL *url = [[NSURL alloc] initWithString:mediaServerUrl];
  38. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
  39. [xmlparser setDelegate:self];
  40. if (![xmlparser parse])
  41. APLog(@"PlexParser url Errors : %@", url);
  42. return _containerInfo;
  43. }
  44. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  45. {
  46. if ([elementName isEqualToString:@"MediaContainer"]) {
  47. if ([attributeDict objectForKey:@"friendlyName"])
  48. [_dicoInfo setObject:[attributeDict objectForKey:@"friendlyName"] forKey:@"libTitle"];
  49. else if ([attributeDict objectForKey:@"title1"])
  50. [_dicoInfo setObject:[attributeDict objectForKey:@"title1"] forKey:@"libTitle"];
  51. if ([attributeDict objectForKey:@"title2"])
  52. [_dicoInfo setObject:[attributeDict objectForKey:@"title2"] forKey:@"libTitle"];
  53. if ([attributeDict objectForKey:@"grandparentTitle"])
  54. [_dicoInfo setObject:[attributeDict objectForKey:@"grandparentTitle"] forKey:@"grandparentTitle"];
  55. } else if ([elementName isEqualToString:@"Directory"]) {
  56. [_dicoInfo setObject:@"directory" forKey:@"container"];
  57. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  58. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  59. } else if ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"]) {
  60. [_dicoInfo setObject:@"item" forKey:@"container"];
  61. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  62. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  63. [_dicoInfo setObject:[attributeDict objectForKey:@"ratingKey"] forKey:@"ratingKey"];
  64. [_dicoInfo setObject:[attributeDict objectForKey:@"summary"] forKey:@"summary"];
  65. if ([attributeDict objectForKey:@"viewCount"])
  66. [_dicoInfo setObject:@"watched" forKey:@"state"];
  67. else
  68. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  69. } else if ([elementName isEqualToString:@"Media"]) {
  70. if ([attributeDict objectForKey:@"audioCodec"])
  71. [_dicoInfo setObject:[attributeDict objectForKey:@"audioCodec"] forKey:@"audioCodec"];
  72. if ([attributeDict objectForKey:@"videoCodec"])
  73. [_dicoInfo setObject:[attributeDict objectForKey:@"videoCodec"] forKey:@"videoCodec"];
  74. } else if ([elementName isEqualToString:@"Part"]) {
  75. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  76. if([attributeDict objectForKey:@"file"])
  77. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  78. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  79. [_dicoInfo setObject:duration forKey:@"duration"];
  80. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  81. [_dicoInfo setObject:sizeFile forKey:@"size"];
  82. } else if ([elementName isEqualToString:@"Stream"]) {
  83. if ([attributeDict objectForKey:@"key"])
  84. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  85. if ([attributeDict objectForKey:@"codec"])
  86. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  87. if ([attributeDict objectForKey:@"language"])
  88. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  89. if ([attributeDict objectForKey:@"languageCode"])
  90. [_dicoInfo setObject:[attributeDict objectForKey:@"languageCode"] forKey:@"languageCode"];
  91. }
  92. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  93. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  94. }
  95. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  96. {
  97. if (([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  98. [_containerInfo addObject:_dicoInfo];
  99. _dicoInfo = [[NSMutableDictionary alloc] init];
  100. }
  101. }
  102. - (NSInteger)MarkWatchedUnwatchedMedia:(NSString *)adress port:(NSString *)port videoRatingKey:(NSString *)ratingKey state:(NSString *)state
  103. {
  104. NSString *url = nil;
  105. if ([state isEqualToString:@"watched"])
  106. url = [NSString stringWithFormat:@"http://%@%@/:/unscrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  107. else
  108. url = [NSString stringWithFormat:@"http://%@%@/:/scrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  109. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:20];
  110. NSURLResponse *response = nil;
  111. NSError *error = nil;
  112. [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  113. NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
  114. if (httpStatus != 200)
  115. APLog(@"Mark Watched Unwatched Media Error status: %ld at URL : %@", (long)httpStatus, url);
  116. return httpStatus;
  117. }
  118. - (NSString *)getFileSubtitleFromPlexServer:(NSMutableArray *)mutableMediaObject modeStream:(BOOL)modeStream
  119. {
  120. NSString *FileSubtitlePath = nil;
  121. NSString *fileName = [[[[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"] stringByDeletingPathExtension] stringByAppendingPathExtension:[[mutableMediaObject objectAtIndex:0] objectForKey:@"codecSubtitle"]];
  122. NSURL *url = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"]];
  123. NSData *receivedSub = [NSData dataWithContentsOfURL:url];
  124. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  125. NSArray *searchPaths = nil;
  126. if (modeStream)
  127. searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  128. else
  129. searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  130. NSString *directoryPath = [searchPaths objectAtIndex:0];
  131. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
  132. NSFileManager *fileManager = [NSFileManager defaultManager];
  133. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  134. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  135. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  136. APLog(@"file creation failed, no data was saved");
  137. }
  138. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  139. } else {
  140. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), fileName, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  141. [alert show];
  142. }
  143. return FileSubtitlePath;
  144. }
  145. @end