VLCPlexParser.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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"]) {
  55. [_dicoInfo setObject:@"item" forKey:@"container"];
  56. [_dicoInfo setObject:[attributeDict objectForKey:@"key"] forKey:@"key"];
  57. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  58. } else if([elementName isEqualToString:@"Part"]) {
  59. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keyMedia"];
  60. if([attributeDict objectForKey:@"file"])
  61. [_dicoInfo setObject:[[attributeDict objectForKey:@"file"] lastPathComponent] forKey:@"namefile"];
  62. NSString *duration = [self timeFormatted:[[attributeDict objectForKey:@"duration"] intValue]];
  63. [_dicoInfo setObject:duration forKey:@"duration"];
  64. NSString *sizeFile = (NSString *)[attributeDict objectForKey:@"size"];
  65. [_dicoInfo setObject:sizeFile forKey:@"size"];
  66. } else if([elementName isEqualToString:@"Stream"]) {
  67. if([attributeDict objectForKey:@"key"]){
  68. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@",_PlexMediaServerUrl, [attributeDict objectForKey:@"key"]] forKey:@"keySubtitle"];
  69. [_dicoInfo setObject:[attributeDict objectForKey:@"codec"] forKey:@"codecSubtitle"];
  70. [_dicoInfo setObject:[attributeDict objectForKey:@"language"] forKey:@"languageSubtitle"];
  71. }
  72. }
  73. if ([attributeDict objectForKey:@"thumb"] && ([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"Part"]))
  74. [_dicoInfo setObject:[NSString stringWithFormat:@"%@%@", _PlexMediaServerUrl, [attributeDict objectForKey:@"thumb"]] forKey:@"thumb"];
  75. }
  76. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  77. {
  78. if(([elementName isEqualToString:@"Video"] || [elementName isEqualToString:@"Directory"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  79. [_containerInfo addObject:_dicoInfo];
  80. _dicoInfo = [[NSMutableDictionary alloc] init];
  81. }
  82. }
  83. - (NSString *)timeFormatted:(int)mSeconds
  84. {
  85. mSeconds = (int)(mSeconds / 1000);
  86. int seconds = (int)(mSeconds % 60);
  87. int minutes = (int)((mSeconds / 60) % 60);
  88. int hours = (int)(mSeconds / 3600);
  89. return [NSString stringWithFormat:@"%02d:%02d:%02d",hours, minutes, seconds];
  90. }
  91. @end