Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
Move to YTS API v2 for movies
Browse files Browse the repository at this point in the history
  • Loading branch information
danylokos committed Jun 22, 2015
1 parent dd5270e commit 6f8e65f
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 66 deletions.
24 changes: 1 addition & 23 deletions PopcornTime/Controllers/MoviesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,9 @@ class MoviesViewController: PagedViewController {
}
}

func unify(newItems: [BasicInfo]) -> [BasicInfo] {
var ids = newItems.map( { $0.identifier } )

var existingUniqueIds: [String] = self.items.map( { $0.identifier } )
var uniqueIds = NSSet(array: ids).allObjects as! [String]

var filteredUniqueIds = [String]()
for uniqueId in uniqueIds {
if !contains(existingUniqueIds, uniqueId) {
filteredUniqueIds.append(uniqueId)
}
}

uniqueIds = filteredUniqueIds
var uniqueItems = [BasicInfo]()
for uniqueId in uniqueIds {
var item = newItems.filter( {$0.identifier == uniqueId} ).first
uniqueItems.append(item!)
}
return uniqueItems
}

override func map(response: [AnyObject]) -> [BasicInfo] {
var items = response.map({ Movie(dictionary: $0 as! NSDictionary) }) as [BasicInfo]
return unify(items)
return items
}

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
Expand Down
43 changes: 26 additions & 17 deletions PopcornTime/Models/Movie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,24 @@ class Movie: BasicInfo {
required init(dictionary: NSDictionary) {
super.init(dictionary: dictionary)

identifier = dictionary["ImdbCode"] as! String
title = dictionary["MovieTitleClean"] as? String
year = dictionary["MovieYear"] as? String
let id = dictionary["id"] as! Int
identifier = "\(id)"
title = dictionary["title"] as? String
year = dictionary["year"] as? String

if let cover = dictionary["CoverImage"] as? String {
images = [Image]()

var URL = NSURL(string: cover)
var image = Image(URL: URL!, type: .Poster)
images = [Image]()
if let cover = dictionary["medium_cover_image"] as? String {
var image = Image(URL: NSURL(string: cover)!, type: .Poster)
images.append(image)
}

if let cover = dictionary["background_image"] as? String {
var image = Image(URL: NSURL(string: cover)!, type: .Banner)
images.append(image)
}

smallImage = self.images.filter({$0.type == ImageType.Poster}).first
bigImage = smallImage
bigImage = self.images.filter({$0.type == ImageType.Banner}).first
}

required init(coder aDecoder: NSCoder) {
Expand All @@ -37,14 +41,19 @@ class Movie: BasicInfo {
override func update(dictionary: NSDictionary) {
videos.removeAll(keepCapacity: true)

let movieList = dictionary["MovieList"] as! NSArray
for movieDict in movieList {
let quality = movieDict["Quality"] as! String
let title = movieDict["MovieTitleClean"]as! String
let magnetLink = movieDict["TorrentMagnetUrl"]as! String

var video = Video(name: title, quality: quality, size: 0, duration: 0, subGroup: nil, magnetLink: magnetLink)
videos.append(video)
let title = dictionary["title"] as! String
// let runtime = dictionary["runtime"] as! UInt

if let movieList = dictionary["torrents"] as? NSArray {
for movieDict in movieList {
let quality = movieDict["quality"] as! String
let hash = movieDict["hash"] as! String
let magnetLink = "magnet:?xt=urn:btih:\(hash)&tr=udp://open.demonii.com:1337&tr=udp://tracker.coppersurfer.tk:6969"
// let size = movieDict["size_bytes"] as! UInt

var video = Video(name: title, quality: quality, size: 0, duration: 0, subGroup: nil, magnetLink: magnetLink)
videos.append(video)
}
}
}
}
56 changes: 37 additions & 19 deletions PopcornTime/Models/PTAPIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@
#import <UIKit/UIKit.h>

NSUInteger const PTAPIManagerResultsLimit = 30;
NSString *const PTAPIManagerMoviesEndPoint = @"http://ytspt.re/api";
NSString *const PTAPIManagerMoviesEndPoint = @"http://cloudflare.com/api/v2";
NSString *const PTAPIManagerShowsEndPoint = @"http://eztvapi.re";
NSString *const PTAPIManagerAnimeEndPoint = @"http://ptp.haruhichan.com";

@implementation PTAPIManager

static NSDictionary *YTSHTTPHeaders;

#pragma mark - Public API

+ (void)initialize
{
YTSHTTPHeaders = @{@"Host": @"eqwww.image.yt"};
}

+ (instancetype)sharedManager
{
static dispatch_once_t onceToken;
Expand Down Expand Up @@ -72,11 +79,26 @@ - (void)topShowsWithType:(PTItemType)type
- (void)dataFromURL:(NSURL *)URL
success:(void(^)(id JSONObject))success
failure:(PTAPIManagerFailure)failure
{
return [self dataFromURL:URL HTTPheaders:nil success:success failure:failure];
}

- (void)dataFromURL:(NSURL *)URL
HTTPheaders:(NSDictionary *)HTTPheaders
success:(void(^)(id JSONObject))success
failure:(PTAPIManagerFailure)failure
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

[[[NSURLSession sharedSession] dataTaskWithURL:URL
completionHandler:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
if (HTTPheaders) {
for (NSString *key in HTTPheaders.allKeys) {
[request addValue:HTTPheaders[key] forHTTPHeaderField:key];
}
}

[[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:
^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{

Expand All @@ -103,18 +125,13 @@ - (void)topMoviesWithPage:(NSUInteger)page
success:(PTAPIManagerSuccessItems)success
failure:(PTAPIManagerFailure)failure
{
NSString *path = [NSString stringWithFormat:@"list.json?limit=%lu&order=desc&sort=seeds&set=%ld", (long)PTAPIManagerResultsLimit, (long)page + 1];
NSString *path = [NSString stringWithFormat:@"list_movies.json?"
"page=%ld&limit=%ld&order_by=desc&sort_by=seeds", (long)page + 1, (long)PTAPIManagerResultsLimit];

NSString *URLString = [PTAPIManagerMoviesEndPoint stringByAppendingPathComponent:path];
[self dataFromURL:[NSURL URLWithString:URLString] success:^(id JSONObject) {
[self dataFromURL:[NSURL URLWithString:URLString] HTTPheaders:YTSHTTPHeaders success:^(id JSONObject) {
if (success) {
NSArray *items = [((NSDictionary *)JSONObject) objectForKey:@"MovieList"];
NSArray *uniqueIds = [items valueForKeyPath:@"@distinctUnionOfObjects.ImdbCode"];
NSMutableArray *uniqueItems = [NSMutableArray array];
for (NSString *uniqueId in uniqueIds) {
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ImdbCode LIKE %@", uniqueId];
id item = [[items filteredArrayUsingPredicate:predicate] firstObject];
[uniqueItems addObject:item];
}
NSArray *items = [[((NSDictionary *)JSONObject) objectForKey:@"data"] objectForKey:@"movies"];
success(items);
}
} failure:failure];
Expand All @@ -123,11 +140,12 @@ - (void)movieInfoWithId:(NSString *)imdbId
success:(PTAPIManagerSuccessItem)success
failure:(PTAPIManagerFailure)failure
{
NSString *path = [NSString stringWithFormat:@"listimdb.json?imdb_id=%@", imdbId];
NSString *path = [NSString stringWithFormat:@"movie_details.json?movie_id=%@", imdbId];
NSString *URLString = [PTAPIManagerMoviesEndPoint stringByAppendingPathComponent:path];
[self dataFromURL:[NSURL URLWithString:URLString]success:^(id JSONObject) {
[self dataFromURL:[NSURL URLWithString:URLString] HTTPheaders:YTSHTTPHeaders success:^(id JSONObject) {
if (success) {
success(JSONObject);
NSDictionary *item = [((NSDictionary *)JSONObject) objectForKey:@"data"];
success(item);
}
} failure:failure];
}
Expand All @@ -136,12 +154,12 @@ - (void)searchForMovieWithName:(NSString *)name
success:(PTAPIManagerSuccessItems)success
failure:(PTAPIManagerFailure)failure
{
NSString *path = [[NSString stringWithFormat:@"list.json?limit=%ld&keywords=%@&order=desc&sort=seeds&set=1", (long)PTAPIManagerResultsLimit, name]
NSString *path = [[NSString stringWithFormat:@"list_movies.json?limit=%ld&query_term=%@", (long)PTAPIManagerResultsLimit, name]
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *URLString = [PTAPIManagerMoviesEndPoint stringByAppendingPathComponent:path];
[self dataFromURL:[NSURL URLWithString:URLString]success:^(id JSONObject) {
[self dataFromURL:[NSURL URLWithString:URLString] HTTPheaders:YTSHTTPHeaders success:^(id JSONObject) {
if (success) {
NSArray *items = [((NSDictionary *)JSONObject) objectForKey:@"MovieList"];
NSArray *items = [[((NSDictionary *)JSONObject) objectForKey:@"data"] objectForKey:@"movies"];
success(items);
}
} failure:failure];
Expand Down
50 changes: 43 additions & 7 deletions popcorntime_api.paw
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<databaseInfo>
<version>134481920</version>
<UUID>8F8C1300-C155-4119-86D2-01F70529817A</UUID>
<nextObjectID>139</nextObjectID>
<nextObjectID>142</nextObjectID>
<metadata>
<plist version="1.0">
<dict>
Expand Down Expand Up @@ -369,7 +369,7 @@
</object>
<object type="LMREQUEST" id="z130">
<attribute name="uuid" type="string">BD8350BF-4631-44DF-9BDC-77A5458F2E4F</attribute>
<attribute name="url" type="string"> http://ytspt.re/api/v2/list_movies.json?page=1\u2600limit=20\u2600order_by=desc\u2600sort=date_added\u2600sort=year\u2600sort=title\u2600sort=rating</attribute>
<attribute name="url" type="string">http://cloudflare.com/api/v2/list_movies.json?page=1\u2600limit=20\u2600order_by=desc\u2600sort_by=seeds</attribute>
<attribute name="storecookies" type="bool">1</attribute>
<attribute name="sendcookies" type="bool">1</attribute>
<attribute name="redirectmethod" type="bool">0</attribute>
Expand All @@ -380,9 +380,11 @@
<attribute name="name" type="string">top</attribute>
<relationship name="parent" type="0/1" destination="LMREQUESTTREEITEM" idrefs="z137"></relationship>
<relationship name="children" type="0/0" destination="LMREQUESTTREEITEM"></relationship>
<relationship name="headers" type="0/0" destination="LMKEYVALUE" idrefs="z131"></relationship>
<relationship name="headers" type="0/0" destination="LMKEYVALUE" idrefs="z142 z131"></relationship>
</object>
<object type="LMKEYVALUE" id="z131">
<attribute name="value" type="string">eqwww.image.yt</attribute>
<attribute name="name" type="string">Host</attribute>
<attribute name="order" type="int64">0</attribute>
<attribute name="enabled" type="bool">1</attribute>
<relationship name="groupforbodyparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
Expand All @@ -401,6 +403,8 @@
<relationship name="urlparameters" type="0/0" destination="LMKEYVALUE"></relationship>
</object>
<object type="LMKEYVALUE" id="z133">
<attribute name="value" type="string">eqwww.image.yt</attribute>
<attribute name="name" type="string">Host</attribute>
<attribute name="order" type="int64">0</attribute>
<attribute name="enabled" type="bool">1</attribute>
<relationship name="groupforbodyparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
Expand All @@ -410,7 +414,7 @@
</object>
<object type="LMREQUEST" id="z134">
<attribute name="uuid" type="string">43436530-B6BD-466C-A1A8-12F57EBAD1BE</attribute>
<attribute name="url" type="string"> http://ytspt.re/api/v2/list_movies.json?query_term=terminator</attribute>
<attribute name="url" type="string">http://cloudflare.com/api/v2/list_movies.json?query_term=terminator</attribute>
<attribute name="storecookies" type="bool">1</attribute>
<attribute name="sendcookies" type="bool">1</attribute>
<attribute name="redirectmethod" type="bool">0</attribute>
Expand All @@ -421,11 +425,11 @@
<attribute name="name" type="string">search</attribute>
<relationship name="parent" type="0/1" destination="LMREQUESTTREEITEM" idrefs="z137"></relationship>
<relationship name="children" type="0/0" destination="LMREQUESTTREEITEM"></relationship>
<relationship name="headers" type="0/0" destination="LMKEYVALUE" idrefs="z133"></relationship>
<relationship name="headers" type="0/0" destination="LMKEYVALUE" idrefs="z140 z133"></relationship>
</object>
<object type="LMREQUEST" id="z135">
<attribute name="uuid" type="string">42E5E5DA-21B7-4077-93AE-B5EF84E238F8</attribute>
<attribute name="url" type="string"> http://ytspt.re/api/v2/movie_details.json?movie_id=4132</attribute>
<attribute name="url" type="string">http://cloudflare.com/api/v2/movie_details.json?movie_id=4132</attribute>
<attribute name="storecookies" type="bool">1</attribute>
<attribute name="sendcookies" type="bool">1</attribute>
<attribute name="redirectmethod" type="bool">0</attribute>
Expand All @@ -436,9 +440,11 @@
<attribute name="name" type="string">desc</attribute>
<relationship name="parent" type="0/1" destination="LMREQUESTTREEITEM" idrefs="z137"></relationship>
<relationship name="children" type="0/0" destination="LMREQUESTTREEITEM"></relationship>
<relationship name="headers" type="0/0" destination="LMKEYVALUE" idrefs="z136"></relationship>
<relationship name="headers" type="0/0" destination="LMKEYVALUE" idrefs="z136 z141"></relationship>
</object>
<object type="LMKEYVALUE" id="z136">
<attribute name="value" type="string">eqwww.image.yt</attribute>
<attribute name="name" type="string">Host</attribute>
<attribute name="order" type="int64">0</attribute>
<attribute name="enabled" type="bool">1</attribute>
<relationship name="groupforbodyparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
Expand All @@ -456,4 +462,34 @@
<relationship name="headers" type="0/0" destination="LMKEYVALUE"></relationship>
<relationship name="urlparameters" type="0/0" destination="LMKEYVALUE"></relationship>
</object>
<object type="LMKEYVALUE" id="z140">
<attribute name="value" type="string"></attribute>
<attribute name="order" type="int64">1</attribute>
<attribute name="name" type="string"></attribute>
<attribute name="enabled" type="bool">1</attribute>
<relationship name="groupforbodyparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="groupforheaders" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="groupforurlparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="request" type="0/1" destination="LMREQUEST" idrefs="z134"></relationship>
</object>
<object type="LMKEYVALUE" id="z141">
<attribute name="value" type="string"></attribute>
<attribute name="order" type="int64">1</attribute>
<attribute name="name" type="string"></attribute>
<attribute name="enabled" type="bool">1</attribute>
<relationship name="groupforbodyparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="groupforheaders" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="groupforurlparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="request" type="0/1" destination="LMREQUEST" idrefs="z135"></relationship>
</object>
<object type="LMKEYVALUE" id="z142">
<attribute name="value" type="string"></attribute>
<attribute name="order" type="int64">1</attribute>
<attribute name="name" type="string"></attribute>
<attribute name="enabled" type="bool">1</attribute>
<relationship name="groupforbodyparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="groupforheaders" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="groupforurlparameters" type="0/1" destination="LMREQUESTGROUP"></relationship>
<relationship name="request" type="0/1" destination="LMREQUEST" idrefs="z130"></relationship>
</object>
</database>

0 comments on commit 6f8e65f

Please sign in to comment.