VLCPlexParser.m 6.5 KB

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