Skip to content

Instantly share code, notes, and snippets.

@wplong11
Last active October 8, 2018 15:56
Show Gist options
  • Select an option

  • Save wplong11/9267885104f89a0c40ec4d65bbdc55b2 to your computer and use it in GitHub Desktop.

Select an option

Save wplong11/9267885104f89a0c40ec4d65bbdc55b2 to your computer and use it in GitHub Desktop.
i love mashup
<html lang="ko">
<head>
<meta charset="UTF-8">
<title>Load movie list in RxJS!!</title>
</head>
<body>
<div id="app-container">
<div id="form">
<label>Limit:</label>
<input type="text" id="limit-input">
<button id="query-button">Query</button>
<p id="movies"></p>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.1.0/rx.all.min.js"></script>
<script>
const appContainer = document.getElementById('app-container');
const limitInput = document.getElementById('limit-input');
const queryButton = document.getElementById('query-button');
const btnClickStream
= Rx.Observable
.fromEvent(queryButton, 'click')
.map(() => true)
const limitInputStream
= Rx.Observable
.fromEvent(limitInput, 'input')
.map(e => e.target.value)
const limitStream
= btnClickStream
.withLatestFrom(limitInputStream, (click, zip) => zip)
.distinct()
const getMovieListAsync
= limit =>
fetch(`https://yts.ag/api/v2/list_movies.json?limit=${limit}`)
.then(res => res.json());
const createMovieStream
= limit =>
Rx.Observable
.fromPromise(getMovieListAsync(limit))
.map(result => result.data.movies)
.flatMap(Rx.Observable.from)
.map(movie =>
movie.torrents
.map(torrent => {
let comparableSize
= torrent.size.slice(-2, -1) == "G"
? torrent.size.slice(0, -2) * 1024
: torrent.size.slice(0, -2) * 1
return {
title: movie.title,
quality: torrent.quality,
comparableSize,
size: comparableSize + " MB"
}
})
.sort((a, b) => {
if (a.comparableSize > b.comparableSize) {
return 1;
}
if (b.comparableSize > a.comparableSize) {
return -1;
}
return 0;
})
)
.flatMap(x => Rx.Observable.from(x))
limitStream
.switchMap(entry =>
createMovieStream(entry)
.reduce((acc, x) => acc + "<p>" + JSON.stringify(x) + "</p>", ""))
.subscribe((html) => {
const movieContainer = document.getElementById('movies');
movieContainer.innerHTML = html;
limitInput.value = '';
});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment