-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
64da4d1
commit 3a82bc0
Showing
10 changed files
with
306 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import got from 'got' | ||
|
||
import * as types from '../types' | ||
|
||
const BASE_URL = 'https://yts.mx/api' | ||
|
||
export type YTSSortBy = | ||
| 'title' | ||
| 'year' | ||
| 'rating' | ||
| 'peers' | ||
| 'seeds' | ||
| 'download_count' | ||
| 'like_count' | ||
| 'date_added' | ||
|
||
export type YTSOrderBy = 'desc' | 'asc' | ||
export type YTSQuality = '720p' | '1080p' | '2160p' | '3D' | 'All' | ||
|
||
export class YTS { | ||
public async getMovies({ | ||
limit = 10, | ||
page = 1, | ||
quality, | ||
minimumIMDBRating, | ||
query, | ||
genre, | ||
sortBy = 'date_added', | ||
orderBy = 'desc', | ||
withRTRatings // RT ratings don't seem to work reliably | ||
}: { | ||
limit?: number | ||
page?: number | ||
quality?: YTSQuality | ||
minimumIMDBRating?: number | ||
query?: string | ||
genre?: string | ||
sortBy?: YTSSortBy | ||
orderBy?: YTSOrderBy | ||
withRTRatings?: boolean | ||
} = {}): Promise<types.yts.Movie[]> { | ||
const url = `${BASE_URL}/v2/list_movies.json` | ||
|
||
const res = await got(url, { | ||
searchParams: { | ||
limit, | ||
page, | ||
quality, | ||
minimum_rating: minimumIMDBRating, | ||
query_term: query, | ||
genre, | ||
sort_by: sortBy, | ||
order_by: orderBy, | ||
with_rt_ratings: withRTRatings | ||
} | ||
}).json<types.yts.APIResponseListMovies>() | ||
|
||
return res.data.movies | ||
} | ||
|
||
public async getMovie({ | ||
imdbId, | ||
ytsId, | ||
images = false, | ||
cast = false | ||
}: { | ||
imdbId?: string | ||
ytsId?: number | ||
images?: boolean | ||
cast?: boolean | ||
}): Promise<types.yts.Movie> { | ||
const url = `${BASE_URL}/v2/movie_details.json` | ||
|
||
if (!imdbId && !ytsId) { | ||
throw new Error(`imdbId or ytsId must be provided`) | ||
} | ||
|
||
const res = await got(url, { | ||
searchParams: { | ||
imdb_id: imdbId, | ||
movie_id: ytsId, | ||
with_images: images, | ||
with_cast: cast | ||
} | ||
}).json<types.yts.APIResponseGetMovie>() | ||
|
||
return res.data.movie | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -108,7 +108,7 @@ async function main() { | |
} | ||
}, | ||
{ | ||
concurrency: 16 | ||
concurrency: 32 | ||
} | ||
) | ||
).filter(Boolean) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import fs from 'node:fs/promises' | ||
|
||
import delay from 'delay' | ||
import makeDir from 'make-dir' | ||
import pMap from 'p-map' | ||
|
||
import * as config from './lib/config' | ||
import * as types from './types' | ||
import { getNumBatches } from './lib/utils' | ||
import { YTS } from './lib/yts' | ||
|
||
/** | ||
* Fetches info on all previously downloaded movies from YTS. | ||
* | ||
* @example | ||
* ``` | ||
* npx tsx src/populate-yts-movies.ts | ||
* ``` | ||
*/ | ||
async function main() { | ||
const yts = new YTS() | ||
await makeDir(config.outDir) | ||
|
||
const numBatches = await getNumBatches() | ||
|
||
let batchNum = 0 | ||
let numMoviesTotal = 0 | ||
let numYTSMoviesDownloadedTotal = 0 | ||
|
||
do { | ||
const srcFile = `${config.outDir}/movies-${batchNum}.json` | ||
const movies: types.Movie[] = JSON.parse( | ||
await fs.readFile(srcFile, { encoding: 'utf-8' }) | ||
) | ||
|
||
console.log( | ||
`\npopulating ${movies.length} movies in batch ${batchNum} (${srcFile})\n` | ||
) | ||
|
||
let numDownloaded = 0 | ||
|
||
await pMap( | ||
movies, | ||
async (movie, index): Promise<types.yts.Movie | null> => { | ||
if (!movie.imdbId) { | ||
return null | ||
} | ||
|
||
try { | ||
console.log( | ||
`${batchNum}:${index} yts ${movie.imdbId} (${movie.releaseYear}) ${movie.title}` | ||
) | ||
|
||
const ytsMovie = await yts.getMovie({ imdbId: movie.imdbId }) | ||
|
||
++numDownloaded | ||
|
||
if (numDownloaded === 1 || numDownloaded % 50 === 0) { | ||
console.log() | ||
console.log(JSON.stringify(ytsMovie, null, 2)) | ||
console.log() | ||
} | ||
|
||
return | ||
} catch (err) { | ||
console.error('yts error', movie.imdbId, movie.title, err.toString()) | ||
|
||
const statusCode = err.response?.statusCode | ||
if (statusCode === 404) { | ||
return null | ||
} | ||
} | ||
}, | ||
{ | ||
concurrency: 16 | ||
} | ||
) | ||
|
||
const numMovies = movies.length | ||
const numYTSMoviesDownloaded = numDownloaded | ||
|
||
numMoviesTotal += numMovies | ||
numYTSMoviesDownloadedTotal += numYTSMoviesDownloaded | ||
|
||
console.log() | ||
console.log(`batch ${batchNum} done`, { | ||
numMovies, | ||
numYTSMoviesDownloaded, | ||
numYTSMoviesDownloadedTotal | ||
}) | ||
|
||
++batchNum | ||
} while (batchNum < numBatches) | ||
|
||
console.log() | ||
console.log('done', { | ||
numMoviesTotal, | ||
numYTSMoviesDownloadedTotal | ||
}) | ||
} | ||
|
||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.