npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

parse-torrent-title

v1.3.0

Published

Parse torrent title to extract useful information

Downloads

484

Readme

parse-torrent-title

Continuous integration

This package helps you extract information from a torrent name such as language, resolution and codec.

Installation

You can install it using npm:

npm install parse-torrent-title

You should use Node 8.0 or higher to use this package.

Usage

A simple usage is as follows:

const ptt = require("parse-torrent-title");
const information = ptt.parse("Game.of.Thrones.S01E01.720p.HDTV.x264-CTU");

console.log(information.title);      // Game of Thrones
console.log(information.season);     // 1
console.log(information.episode);    // 1
console.log(information.resolution); // 720p
console.log(information.codec);      // x264
console.log(information.source);     // HDTV
console.log(information.group);      // CTU

Advanced usage

This module is configurable and extendable using handlers and regular expressions.

Regular expressions

If you want an extra field to be populated, you can use a regular expression as follow:

const ptt = require("parse-torrent-title");
ptt.addHandler("part", /Part[. ]([0-9])/i, { type: "integer" });
const information = ptt.parse("Silent.Witness.S18E03.Falling.Angels.Part.1.720p.HDTV.x264-FTP");

console.log(information.part); // 1

If you want to keep only a part of the matched regular expression, you should use capturing groups explained here.

For regular expressions, the following options are available:

  • skipIfAlreadyFound (default to true) which will skip the regular expression if a previous handler for the same information already found something.
  • type (default to string) which indicates what is the expected output of the regular expression. It can be:
    • string: does nothing
    • integer: convert the matching part into an integer
    • lowercase: convert the matching part to lowercase
    • boolean: convert to true if there is a matching part
  • value (default to undefined) which, if defined, set the specified value instead of the matching part as the result

Handlers

A handler is a function with the title and the resulting information as parameters. It can modify the result in any wanted way. If the matched string is not part of the title, it should return the beginning of it.

const ptt = require("parse-torrent-title");
const information = ptt.parse("[REQ] Harry Potter And The Prisoner Of Azkaban 2004 1080p BluRay DTS x264-hV");
console.log(information.isHarryPotterRelated); // undefined

ptt.addHandler(({ title, result }) => {
    const match = title.match(/harry.potter/i);
    if (match) {
        result.isHarryPotterRelated = true;
    }
});

const information2 = ptt.parse("[REQ] Harry Potter And The Prisoner Of Azkaban 2004 1080p BluRay DTS x264-hV");
console.log(information2.isHarryPotterRelated); // true

Multiple parsers

You may want several parsers within the same project. To do that, you can simply create new parsers:

const { Parser } = require("parse-torrent-title");
const parser = new Parser();
const anotherParser = new Parser();

By default, a freshly created parser has no handler. If you want to add default handlers to a parser, you can do so using the specific method:

const { Parser, addDefaults } = require("parse-torrent-title");
const parser = new Parser();
addDefaults(parser); // parser is now ready

Usage with TypeScript

If you add new properties with addHandler in TypeScript, the result type DefaultParserResult will not be updated, and you will encounter a TS2339 error.

To prevent this error and have autocomplete, you should create a ParserResult interface with the expected properties. It is possible to extend the DefaultParserResult interface to get all the default properties. Then, create a new Parser object using the above interface. Finally, use the parse function as usual.

Example:

import { Parser, DefaultParserResult, addDefaults } from "parse-torrent-title";

interface ParserResult extends DefaultParserResult {
    part?: number;
}

const parser = new Parser<ParserResult>();
addDefaults(parser);
parser.addHandler('part', /(?:Part|CD)[. ]?([0-9])/i, { type: 'integer' })
const parse = parser.parse;

const result = parse('Watergate.2018.Part1.DOC.SUBFRENCH.1080p.HDTV.H264-ELEARNiNG');
console.log(result.year); // 2018 - it works as before
console.log(result.part); // 1 - `part` is now a known property of the `result` object