Skip to content

Instantly share code, notes, and snippets.

@rbezerra
Created October 17, 2015 22:41
Show Gist options
  • Select an option

  • Save rbezerra/c3794196fad7203fee22 to your computer and use it in GitHub Desktop.

Select an option

Save rbezerra/c3794196fad7203fee22 to your computer and use it in GitHub Desktop.
(function(){
'use strict';
angular.module('roulette-movie',['ui.router'])
.config(function($stateProvider, $urlRouterProvider) {
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
controller: 'HomeController as home',
templateUrl: 'partials/home.html'
})
.state('about', {
url: '/about'
});
})
.config(['$compileProvider', function($compileProvider){
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?|ftp|mailto|magnet):/);
}])
.controller('HomeController', HomeController)
.service('YifyService', YifyService)
.service('MagnetService', MagnetService);
function HomeController(YifyService, MagnetService){
var vm = this;
vm.message = "Choose a Movie";
vm.loading = false;
YifyService.getCountMovie()
.then(function(response){
vm.count_movies = response.data.data.movie_count;
},
function(err){
console.log('Erro: ', err);
})
.then(function(){
vm.okGo = true;
});
vm.goRoullete = function(){
vm.loading = !vm.loading;
var random_movie = Math.floor((Math.random() * vm.count_movies) + 1);
YifyService.getMovie(random_movie)
.then(function(response){
var movie = response.data.data;
vm.movie = {
title: movie.title,
year: movie.year,
genres: movie.genres.toString(),
rating: movie.rating,
poster: movie.images.medium_cover_image,
links_torrent: movie.torrents,
links_magnet: MagnetService.createMagnets(movie),
};
})
.catch(function(err){
console.log('Erro: ', err);
})
.then(function(){
vm.loading = !vm.loading;
});
};
}
function YifyService($http){
this.getCountMovie = function(){
var REQ = {
url: 'https://yts.to/api/v2/list_movies.json',
method: 'GET',
cache: true
};
return $http(REQ);
};
this.getMovie = function(movieId){
var REQ = {
url: 'https://yts.to/api/v2/movie_details.json?movie_id='+movieId+'&with_images=true',
method: 'GET'
};
return $http(REQ);
};
}
function MagnetService(){
this.createMagnets = function(movie){
var trackers = [
'udp://open.demonii.com:1337',
'udp://tracker.istole.it:80',
'http://tracker.yify-torrents.com/announce',
'udp://tracker.publicbt.com:80',
'udp://tracker.openbittorrent.com:80',
'udp://tracker.coppersurfer.tk:6969',
'udp://exodus.desync.com:6969',
'http://exodus.desync.com:6969/announce'
];
var magnetURI = (function () {
var template = 'magnet:?xt=urn:btih:{HASH}&dn={TITLE}&tr=' + trackers.join('&tr=');
return function (hash, title) {
return template
.replace('{HASH}', hash)
.replace('{TITLE}', encodeURIComponent(title));
};
}());
var title = movie.title_long;
var torrents = movie.torrents;
var magnets = [];
torrents.forEach(function(element, index){
magnets.push({
quality: element.quality,
link: magnetURI(element.hash, title)
});
});
return magnets;
};
}
HomeController.$inject = ['YifyService', 'MagnetService'];
YifyService.$inject = ['$http'];
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment