VLCPlexParser.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if ([attributeDict objectForKey:@"summary"])
  65. [_dicoInfo setObject:[attributeDict objectForKey:@"summary"] forKey:@"summary"];
  66. if ([attributeDict objectForKey:@"viewCount"])
  67. [_dicoInfo setObject:@"watched" forKey:@"state"];
  68. else
  69. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  70. } else if ([elementName isEqualToString:@"Media"]) {
  71. if ([attributeDict objectForKey:@"audioCodec"])
  72. [_dicoInfo setObject:[attributeDict objectForKey:@"audioCodec"] forKey:@"audioCodec"];
  73. if ([attributeDict objectForKey:@"videoCodec"])
  74. [_dicoInfo setObject:[attributeDict objectForKey:@"videoCodec"] forKey:@"videoCodec"];
  75. } else if ([elementName isEqualToString:@"Part"]) {
  76. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  77. if([attributeDict objectForKey:@"file"])
  78. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  79. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  80. [_dicoInfo setObject:duration forKey:@"duration"];
  81. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  82. if (sizeFile)
  83. [_dicoInfo setObject:sizeFile forKey:@"size"];
  84. } else if ([elementName isEqualToString:@"Stream"]) {
  85. if ([attributeDict objectForKey:@"key"])
  86. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  87. if ([attributeDict objectForKey:@"codec"])
  88. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  89. if ([attributeDict objectForKey:@"language"])
  90. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  91. if ([attributeDict objectForKey:@"languageCode"])
  92. [_dicoInfo setObject:[attributeDict objectForKey:@"languageCode"] forKey:@"languageCode"];
  93. }
  94. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  95. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  96. }
  97. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  98. {
  99. if (([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  100. [_containerInfo addObject:_dicoInfo];
  101. _dicoInfo = [[NSMutableDictionary alloc] init];
  102. }
  103. }
  104. - (NSInteger)MarkWatchedUnwatchedMedia:(NSString *)adress port:(NSString *)port videoRatingKey:(NSString *)ratingKey state:(NSString *)state
  105. {
  106. NSString *url = nil;
  107. if ([state isEqualToString:@"watched"])
  108. url = [NSString stringWithFormat:@"http://%@%@/:/unscrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  109. else
  110. url = [NSString stringWithFormat:@"http://%@%@/:/scrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  111. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:20];
  112. NSURLResponse *response = nil;
  113. NSError *error = nil;
  114. [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  115. NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
  116. if (httpStatus != 200)
  117. APLog(@"Mark Watched Unwatched Media Error status: %ld at URL : %@", (long)httpStatus, url);
  118. return httpStatus;
  119. }
  120. - (NSString *)getFileSubtitleFromPlexServer:(NSMutableArray *)mutableMediaObject modeStream:(BOOL)modeStream
  121. {
  122. NSString *FileSubtitlePath = nil;
  123. NSString *fileName = [[[[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"] stringByDeletingPathExtension] stringByAppendingPathExtension:[[mutableMediaObject objectAtIndex:0] objectForKey:@"codecSubtitle"]];
  124. NSURL *url = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"]];
  125. NSData *receivedSub = [NSData dataWithContentsOfURL:url];
  126. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  127. NSArray *searchPaths = nil;
  128. if (modeStream)
  129. searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  130. else
  131. searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  132. NSString *directoryPath = [searchPaths objectAtIndex:0];
  133. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
  134. NSFileManager *fileManager = [NSFileManager defaultManager];
  135. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  136. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  137. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  138. APLog(@"file creation failed, no data was saved");
  139. }
  140. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  141. } else {
  142. 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];
  143. [alert show];
  144. }
  145. return FileSubtitlePath;
  146. }
  147. @end