VLCPlexParser.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if([attributeDict objectForKey:@"viewCount"])
  62. [_dicoInfo setObject:@"watched" forKey:@"state"];
  63. else
  64. [_dicoInfo setObject:@"unwatched" forKey:@"state"];
  65. } else if([elementName isEqualToString:@"Part"]) {
  66. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  67. if([attributeDict objectForKey:@"file"])
  68. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  69. NSString *duration = [[VLCTime timeWithNumber:[attributeDict objectForKey:@"duration"]] stringValue];
  70. [_dicoInfo setObject:duration forKey:@"duration"];
  71. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  72. [_dicoInfo setObject:sizeFile forKey:@"size"];
  73. } else if([elementName isEqualToString:@"Stream"]) {
  74. if([attributeDict objectForKey:@"key"]){
  75. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  76. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  77. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  78. }
  79. }
  80. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"] || [elementName isEqualToString:@"Track"]))
  81. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  82. }
  83. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  84. {
  85. if(([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Track"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  86. [_containerInfo addObject:_dicoInfo];
  87. _dicoInfo = [[NSMutableDictionary alloc] init];
  88. }
  89. }
  90. - (NSInteger)MarkWatchedUnwatchedMedia:(NSString *)adress port:(NSString *)port videoRatingKey:(NSString *)ratingKey state:(NSString *)state
  91. {
  92. NSString *url = nil;
  93. if ([state isEqualToString:@"watched"])
  94. url = [NSString stringWithFormat:@"http://%@%@/:/unscrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  95. else
  96. url = [NSString stringWithFormat:@"http://%@%@/:/scrobble?identifier=com.plexapp.plugins.library&key=%@", adress, port, ratingKey];
  97. NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:20];
  98. NSURLResponse *response = nil;
  99. NSError *error = nil;
  100. [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
  101. NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
  102. if (httpStatus != 200)
  103. APLog(@"Mark Watched Unwatched Media Error status: %ld at URL : %@", (long)httpStatus, url);
  104. return httpStatus;
  105. }
  106. @end