Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
TriPSs committed Sep 25, 2018
0 parents commit e3a6d1a
Show file tree
Hide file tree
Showing 14 changed files with 2,332 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"presets": [
"es2015",
"stage-0"
]
}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*

!src
!src/**
!.babelrc
!.gitignore
!.npmignore
!package.json
!README.md
!yarn.lock
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.babelrc
node_modules
.idea
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Popcorn SDK

// TODO
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "popcorn-sdk",
"version": "0.0.11",
"description": "Popcorn SDK",
"homepage": "https://github.com/tripss/popcorn-sdk",
"bugs": {
"url": "https://github.com/popcorn-sdk/issues"
},
"author": {
"name": "Tycho Bokdam",
"email": "[email protected]"
},
"main": "./index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/popcorn-sdk.git"
},
"scripts": {
"build": "NODE_ENV=production babel src -d ./ --copy-files --ignore __tests__",
"build:watch": "yarn build --watch",
"prepublish": "yarn build"
},
"dependencies": {
"axios": "^0.18.0"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-es2015": "^6.24.1"
}
}
92 changes: 92 additions & 0 deletions src/PctAdapter/PctAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import axios from 'axios'

import * as PctHelpers from './PctHelpers'
import formatRuntime from '../utils/formatRuntime'
import * as Constants from '../constants'

export default class PctAdapter {

defaultFilters = {
sort: 'trending',
day : new Date().getDate(),
}

popcornAPI = axios.create({
baseURL: 'https://movies-v2.api-fetch.website/',
})

getMovies = (page = 1, filters = {}) => (
this.popcornAPI.get(`movies/${page}`, { params: { ...this.defaultFilters, ...filters } })
.then(({ data: movies }) => this.formatMovies(movies))
)

getMovie = itemId => (
this.popcornAPI.get(`movie/${itemId}`)
.then(({ data: movie }) => this.formatMovie(movie, { params: { day: this.defaultFilters.day } }))
)

getShows = (page = 1, filters = {}) => (
this.popcornAPI.get(`shows/${page}`, { params: { ...this.defaultFilters, ...filters } })
.then(({ data: shows }) => this.formatShows(shows))
)

getShow = itemId => (
this.popcornAPI.get(`show/${itemId}`, { params: { day: this.defaultFilters.day } })
.then(({ data: show }) => this.formatShow(show, true))
)

formatMovies = movies => (movies.map(movie => this.formatMovie(movie)))

formatMovie = (movie) => ({
id : movie.imdb_id,
title : movie.title,
year : movie.year,
certification: movie.certification,
summary : movie.synopsis,
runtime : formatRuntime(movie.runtime),
trailer : movie.trailer,
images : PctHelpers.formatImages(movie.images),
genres : movie.genres,
rating : PctHelpers.formatRating(movie.rating),
torrents : PctHelpers.formatTorrents(movie.torrents.en),
type : Constants.TYPE_MOVIE,
watched : {
complete: false,
progress: 0,
},
})

formatShows = shows => shows.map(show => this.formatShow(show))

formatShow = (show, isDetail = false) => {
let formattedShow = {
id : show.imdb_id,
title : show.title,
year : show.year,
images : PctHelpers.formatImages(show.images),
rating : PctHelpers.formatRating(show.rating),
num_seasons: show.num_seasons,
type : Constants.TYPE_SHOW,
watched : {
complete: false,
progress: 0,
},
}

if (isDetail) {
formattedShow = {
...formattedShow,
runtime: formatRuntime(show.runtime),
seasons: PctHelpers.formatShowEpisodes(show.episodes),
summary: show.synopsis,
genres : show.genres,
status : show.status,
}
}

return formattedShow
}

getStatus = () => this.popcornAPI.get().then(res => res.ok).catch(() => false)

}
85 changes: 85 additions & 0 deletions src/PctAdapter/PctHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import getHealth from '../utils/getHealth'

export const formatImages = (images) => {
const replaceWidthPart = (uri, replaceWith) => uri.replace('w500', replaceWith)

if (!images.poster || !images.fanart) {
return {
poster: {
full : null,
high : null,
medium: null,
thumb : null,
},
fanart: {
full : null,
high : null,
medium: null,
thumb : null,
},
}
}

return {
poster: {
full : replaceWidthPart(images.poster, 'original'),
high : replaceWidthPart(images.poster, 'w1280'),
medium: replaceWidthPart(images.poster, 'w780'),
thumb : replaceWidthPart(images.poster, 'w342'),
},
fanart: {
full : replaceWidthPart(images.fanart, 'original'),
high : replaceWidthPart(images.fanart, 'w1280'),
medium: replaceWidthPart(images.fanart, 'w780'),
thumb : replaceWidthPart(images.fanart, 'w342'),
},
}
}

export const formatTorrents = (torrents, type = 'movie') => {
const formatTorrent = (torrent, quality) => ({
...torrent,
quality,
health: getHealth(torrent.seed || torrent.seeds, torrent.peer || torrent.peers),
seeds : torrent.seed || torrent.seeds,
peers : torrent.peer || torrent.peers,
})

if (type === 'movie') {
return {
'1080p': !torrents['1080p'] ? null : formatTorrent(torrents['1080p'], '1080p'),
'720p' : !torrents['720p'] ? null : formatTorrent(torrents['720p'], '720p'),
}
}

return {
'1080p': !torrents['1080p'] ? null : formatTorrent(torrents['1080p'], '1080p'),
'720p' : !torrents['720p'] ? null : formatTorrent(torrents['720p'], '720p'),
'480p' : !torrents['480p'] ? null : formatTorrent(torrents['480p'], '480p'),
}
}

export const formatRating = rating => ({
stars: (rating.percentage / 100) * 5,
...rating,
})

export const formatShowEpisodes = (episodes) => {
const seasons = []

episodes.forEach((episode) => {
if (!seasons[episode.season]) {
seasons[episode.season] = []
}

seasons[episode.season][episode.episode] = {
summary : episode.overview,
season : episode.season,
number : episode.season,
episode : episode.episode,
torrents: formatTorrents(episode.torrents, 'show'),
}
})

return seasons
}
1 change: 1 addition & 0 deletions src/PctAdapter/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './PctAdapter'
23 changes: 23 additions & 0 deletions src/SDK.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import PctAdapter from './PctAdapter'

export default new (class SDK {

pctAdapter

constructor() {
this.pctAdapter = new PctAdapter()
}

getMovies = (page = 1, filters = {}) => (
this.pctAdapter.getMovies(page, filters)
)

getShows = () => ([])

getShow = (itemId) => ({})

searchEpisode = (...args) => ({})

search = (...args) => ([])

})()
9 changes: 9 additions & 0 deletions src/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const TYPE_MOVIE = 'movie'
export const TYPE_SHOW = 'show'
export const TYPE_SHOW_SEASON = 'show.season'
export const TYPE_SHOW_EPISODE = 'show.episode'

export const PLURALS = {
[TYPE_SHOW] : `${TYPE_SHOW}s`,
[TYPE_MOVIE]: `${TYPE_MOVIE}s`,
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { default } from './SDK'

export * as Constants from './constants'
22 changes: 22 additions & 0 deletions src/utils/formatRuntime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export default (runtimeInMinutes) => {
const hours = runtimeInMinutes >= 60 ? Math.round(runtimeInMinutes / 60) : 0
const minutes = runtimeInMinutes % 60

return {
full: hours > 0
? `${hours} ${hours > 1 ? 'hours' : 'hour'}${minutes > 0
? ` ${minutes} minutes`
: ''}`
: `${minutes} minutes`,

short: hours > 0
? `${hours} ${hours > 1 ? 'hrs' : 'hr'}${minutes > 0
? ` ${minutes} min`
: ''}`
: `${minutes} min`,

hours,
minutes,
inMinutes: parseInt(runtimeInMinutes, 10),
}
}
42 changes: 42 additions & 0 deletions src/utils/getHealth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
export default (seeds, peers) => {
const ratio = seeds && !!peers ? seeds / peers : seeds

// Normalize the data. Convert each to a percentage
// Ratio: Anything above a ratio of 5 is good
const normalizedRatio = Math.min((ratio / 5) * 100, 100)
// Seeds: Anything above 30 seeds is good
const normalizedSeeds = Math.min((seeds / 30) * 100, 100)

// Weight the above metrics differently
// Ratio is weighted 60% whilst seeders is 40%
const weightedRatio = normalizedRatio * 0.6
const weightedSeeds = normalizedSeeds * 0.4
const weightedTotal = weightedRatio + weightedSeeds

// Scale from [0, 100] to [0, 3]. Drops the decimal places
const scaledTotal = ((weightedTotal * 3) / 100) || 0

if (scaledTotal === 1) {
return {
text : 'decent',
color : '#FF9800',
number: 1,
ratio,
}

} else if (scaledTotal >= 2) {
return {
text : 'healthy',
color : '#4CAF50',
number: 2,
ratio,
}
}

return {
text : 'poor',
color : '#F44336',
number: 0,
ratio,
}
}
Loading

0 comments on commit e3a6d1a

Please sign in to comment.