VLCPlexParser.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*****************************************************************************
  2. * VLCPlexParser.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014 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. #define kPlexMediaServerDirInit @"library/sections"
  13. @interface VLCPlexParser () <NSXMLParserDelegate>
  14. {
  15. NSMutableArray *_containerInfo;
  16. NSMutableDictionary *_dicoInfo;
  17. NSString *_PlexMediaServerUrl;
  18. }
  19. @end
  20. @implementation VLCPlexParser
  21. - (NSMutableArray *)PlexMediaServerParser:(NSString *)adress port:(NSString *)port navigationPath:(NSString *)path
  22. {
  23. _containerInfo = [[NSMutableArray alloc] init];
  24. [_containerInfo removeAllObjects];
  25. _dicoInfo = [[NSMutableDictionary alloc] init];
  26. _PlexMediaServerUrl = [NSString stringWithFormat:@"http://%@%@",adress, port];
  27. NSString *mediaServerUrl;
  28. if ([path isEqualToString:@""])
  29. mediaServerUrl = [NSString stringWithFormat:@"%@/%@",_PlexMediaServerUrl, kPlexMediaServerDirInit];
  30. else {
  31. if ([path rangeOfString:@"library"].location != NSNotFound)
  32. mediaServerUrl = [NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, path];
  33. else
  34. mediaServerUrl = [NSString stringWithFormat:@"%@/%@/%@",_PlexMediaServerUrl, kPlexMediaServerDirInit, path];
  35. }
  36. NSURL *url = [[NSURL alloc] initWithString:mediaServerUrl];
  37. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
  38. [xmlparser setDelegate:self];
  39. if (![xmlparser parse])
  40. APLog(@"PlexParser url Errors : %@", url);
  41. return _containerInfo;
  42. }
  43. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  44. {
  45. if([elementName isEqualToString:@"MediaContainer"]) {
  46. if ([attributeDict objectForKey:@"friendlyName"])
  47. [_dicoInfo setObject:[attributeDict objectForKey:@"friendlyName"] forKey:@"libTitle"];
  48. else if ([attributeDict objectForKey:@"title1"])
  49. [_dicoInfo setObject:[attributeDict objectForKey:@"title1"] forKey:@"libTitle"];
  50. if ([attributeDict objectForKey:@"title2"])
  51. [_dicoInfo setObject:[attributeDict objectForKey:@"title2"] forKey:@"libTitle"];
  52. } else if([elementName isEqualToString:@"Directory"]) {
  53. [_dicoInfo setObject:@"directory" forKey:@"container"];
  54. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  55. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  56. } else if([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"]) {
  57. [_dicoInfo setObject:@"item" forKey:@"container"];
  58. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  59. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  60. [_dicoInfo setObject:[attributeDict objectForKey:@"ratingKey"] forKey:@"ratingKey"];
  61. [_dicoInfo setObject:[attributeDict objectForKey:@"summary"] forKey:@"summary"];
  62. if([attributeDict objectForKey:@"viewCount"])
  63. [_dicoInfo setObject:@"watched" forKey:@"state"];
  64. else
  65. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  66. } else if([elementName isEqualToString:@"Media"]) {
  67. if([attributeDict objectForKey:@"audioCodec"])
  68. [_dicoInfo setObject:[attributeDict objectForKey:@"audioCodec"] forKey:@"audioCodec"];
  69. if([attributeDict objectForKey:@"videoCodec"])
  70. [_dicoInfo setObject:[attributeDict objectForKey:@"videoCodec"] forKey:@"videoCodec"];
  71. } else if([elementName isEqualToString:@"Part"]) {
  72. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  73. if([attributeDict objectForKey:@"file"])
  74. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  75. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  76. [_dicoInfo setObject:duration forKey:@"duration"];
  77. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  78. [_dicoInfo setObject:sizeFile forKey:@"size"];
  79. } else if([elementName isEqualToString:@"Stream"]) {
  80. if([attributeDict objectForKey:@"key"]){
  81. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  82. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  83. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  84. }
  85. }
  86. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  87. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  88. }
  89. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  90. {
  91. if(([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  92. [_containerInfo addObject:_dicoInfo];
  93. _dicoInfo = [[NSMutableDictionary alloc] init];
  94. }
  95. }
  96. - (NSInteger)MarkWatchedUnwatchedMedia:(NSString *)adress port:(NSString *)port videoRatingKey:(NSString *)ratingKey state:(NSString *)state
  97. {
  98. NSString *url = nil;
  99. if ([state isEqualToString:@"watched"])
  100. url = [NSString stringWithFormat:@"http://%@%@/:/unscrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  101. else
  102. url = [NSString stringWithFormat:@"http://%@%@/:/scrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  103. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:20];
  104. NSURLResponse *response = nil;
  105. NSError *error = nil;
  106. [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  107. NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
  108. if (httpStatus != 200)
  109. APLog(@"Mark Watched Unwatched Media Error status: %ld at URL : %@", (long)httpStatus, url);
  110. return httpStatus;
  111. }
  112. @end