/Classes/TorrentzParser/TorrentzParser.m

https://github.com/dustinrue/TVShows · Objective C · 142 lines · 83 code · 24 blank · 35 comment · 20 complexity · aee936889892ff6db3ebd59c1bc6aeb2 MD5 · raw file

  1. /*
  2. * This file is part of the TVShows 2 ("Phoenix") source code.
  3. * http://github.com/victorpimentel/TVShows/
  4. *
  5. * TVShows is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * You should have received a copy of the GNU General Public License
  11. * along with TVShows. If not, see <http://www.gnu.org/licenses/>.
  12. *
  13. */
  14. #import "AppInfoConstants.h"
  15. #import "TorrentzParser.h"
  16. #import "RegexKitLite.h"
  17. #import "WebsiteFunctions.h"
  18. @implementation TorrentzParser
  19. - (id)init
  20. {
  21. if((self = [super init])) {
  22. // Initialization code here.
  23. }
  24. return self;
  25. }
  26. + (NSString *) getAlternateTorrentForEpisode:(NSString *)episodeName
  27. {
  28. NSString *torrentzURLFormat;
  29. // Decide if quotes are needed (for late nights) or not
  30. // Because I'm lazy, this piece of code will be useless in 2020
  31. // Sorry late nights viewers from the next decade
  32. if ([episodeName rangeOfString:@" 201"].location == NSNotFound) {
  33. torrentzURLFormat = @"http://torrentz.eu/feed_any?q=%@+720p";
  34. } else {
  35. torrentzURLFormat = @"http://torrentz.eu/feed_any?q=%%22%@%%22+720p";
  36. }
  37. // Now let's grab the search results
  38. NSString *torrentzURL = [NSString stringWithFormat:torrentzURLFormat,
  39. [episodeName stringByReplacingOccurrencesOfString:@" "
  40. withString:@"+"]];
  41. NSString *searchResults = [WebsiteFunctions downloadStringFrom:torrentzURL];
  42. // Regex fun...
  43. NSString *regex = @"<guid>[^<]+</guid>";
  44. NSArray *tempValues = [searchResults componentsMatchedByRegex:regex];
  45. // Check the first result
  46. if ([tempValues count] >= 1) {
  47. // Get the torrentz result page
  48. torrentzURL = [[tempValues objectAtIndex:0] stringByReplacingOccurrencesOfRegex:@"<[^<]+>" withString:@""];
  49. // Get all possible torrent files
  50. tempValues = [TorrentzParser getAllTorrentsFromTorrenzURL:torrentzURL];
  51. // And choose one randomly
  52. if ([tempValues count] >= 1) {
  53. return [tempValues objectAtIndex:arc4random()%[tempValues count]];
  54. } else {
  55. return nil;
  56. }
  57. } else {
  58. return nil;
  59. }
  60. }
  61. + (NSArray *) getAllTorrentsFromTorrenzURL:(NSString *)aTorrentzURL
  62. {
  63. // Now let's grab the meta search results
  64. NSString *searchResults = [WebsiteFunctions downloadStringFrom:aTorrentzURL];
  65. // Get all the external links (the trackers)
  66. NSString *regex = @"<a href=\"http[^\"]+";
  67. NSArray *tempValues = [searchResults componentsMatchedByRegex:regex];
  68. NSMutableArray *torrents = [NSMutableArray arrayWithCapacity:10];
  69. // And process one by one
  70. for (NSString *tracker in tempValues) {
  71. tracker = [tracker stringByReplacingOccurrencesOfRegex:@"<a href=\"" withString:@""];
  72. NSString *torrent = nil;
  73. // Process only known trackers
  74. if([tracker hasPrefix:@"http://www.vertor.com"]) {
  75. torrent = [TorrentzParser getTorrentFromTracker:tracker
  76. withLinkMatcher:@"http://www.vertor.com/([^\"]*)mod=download([^\"]*)id=([^\"]*)"
  77. appending:@""];
  78. torrent = [torrent stringByReplacingOccurrencesOfString:@"amp;" withString:@""];
  79. } else if([tracker hasPrefix:@"http://thepiratebay.org"]) {
  80. torrent = [TorrentzParser getTorrentFromTracker:tracker
  81. withLinkMatcher:@"http://torrents.thepiratebay.org([^\"]*)"
  82. appending:@""];
  83. } else if([tracker hasPrefix:@"http://btjunkie.org"]) {
  84. torrent = [TorrentzParser getTorrentFromTracker:tracker
  85. withLinkMatcher:@"http://dl.btjunkie.org/torrent/([^\"]*)\\.torrent"
  86. appending:@""];
  87. } else if([tracker hasPrefix:@"http://www.btmon.com"]) {
  88. torrent = [tracker stringByReplacingOccurrencesOfString:@".html" withString:@""];
  89. // Not trustworthy enough
  90. // } else if([tracker hasPrefix:@"http://www.torrenthound.com"]) {
  91. // torrent = [TorrentzParser getTorrentFromTracker:tracker
  92. // withLinkMatcher:@"/torrent/([^\"]*)"
  93. // appending:@"http://www.torrenthound.com"];
  94. }
  95. if (torrent != nil) {
  96. [torrents addObject:torrent];
  97. }
  98. }
  99. return torrents;
  100. }
  101. + (NSString *) getTorrentFromTracker:(NSString*)theURL withLinkMatcher:(NSString*)theLinkMatcher appending:(NSString*)aString
  102. {
  103. // Let's grab this URL content
  104. NSString *content = [WebsiteFunctions downloadStringFrom:theURL];
  105. // Get all the URLs
  106. NSArray *tempValues = [content componentsMatchedByRegex:theLinkMatcher];
  107. // Return the first result
  108. if ([tempValues count] >= 1) {
  109. return [aString stringByAppendingString:[tempValues objectAtIndex:0]];
  110. } else {
  111. return nil;
  112. }
  113. }
  114. - (void)dealloc
  115. {
  116. [super dealloc];
  117. }
  118. @end