Raspberry Pi: yts.re python wrapper

A YIFY-Torrents API wrapper written in python 3.41, which combined with btstream from my previous post serves as a command-line media center.

 Dependencies

 Usage

Options can be found here and must be separated with the & character.

python3 yts.py list <options>
python3 yts.py search <keywords> <options>

 Example

> python3 yts.py list "quality=720p&rating=7"
#   Rating  Title
1.  7.1     Across 110th Street (1972)
2.  7.0     The Spy in Black (1939)
3.  7.1     Wendy and Lucy (2008)
4.  8.2     The Fault in Our Stars (2014)
5.  7.0     Without a Clue (1988)
6.  8.1     Out of the Past (1947)
7.  7.1     Godzilla (2014)
8.  7.8     Night on Earth (1991)
9.  7.0     A Funny Thing Happened on the Way to the Forum (1966)
10. 7.4     Generation Iron (2013)
11. 8.2     The Normal Heart (2014)
12. 7.1     Roger Dodger (2002)
13. 8.0     Love Streams (1984)
14. 7.7     Love Actually (2003)
15. 7.4     Only Lovers Left Alive (2013)
16. 7.0     World's Greatest Dad (2009)
17. 7.4     Home Alone (1990)
18. 7.8     The Children's Hour (1961)
19. 7.0     Mr. Peabody and Sherman (2014)
20. 7.3     The Amazing Spider-Man 2 (2014)

0. Exit.
>  

 Source

To install just copy the code below into a file in the home directory of your Raspberry Pi and name it yts.py.

import subprocess as sp
import sys
import json
import urllib.request

def play(movie):
    try:
        sp.call(["./btstream", movie["TorrentUrl"]])
    except OSError:
        print("Could not find btstream.")

def search(keywords, options):
    request = "https://yts.re/api/list.json?keywords=" + keywords + "&" + options
    data = load(request)
    movies = data["MovieList"]

    print("#\tRating\tTitle")

    for (i, movie) in enumerate(movies):
        print("%d.\t%s\t%s" % (i + 1, movie["MovieRating"], movie["MovieTitle"]))

    print()
    print("0. Exit.")

    try:
        get = int(input("> "))

        if get <= 0 or get > len(movies):
            return

        play(movies[get - 1])
    except ValueError:
        search(keywords, options)

def list(options):
    request = "https://yts.re/api/list.json?" + options
    data = load(request)
    movies = data["MovieList"]

    print("#\tRating\tTitle")

    for (i, movie) in enumerate(movies):
        print("%d.\t%s\t%s" % (i + 1, movie["MovieRating"], movie["MovieTitle"]))

    print()
    print("0. Exit.")

    try:
        get = int(input("> "))

        if get <= 0 or get > len(movies):
            return

        play(movies[get - 1])
    except ValueError:
        list(options)

def load(request):
    response = urllib.request.urlopen(request).readall().decode('utf-8')
    return json.loads(response)

def main():
    args = sys.argv
    if len(args) < 2:
        return;

    action = args[1]
    if action == "list":
        if len(args) > 2:
            list(args[2])
        else:
            list("")
    elif action == "search":
        if len(args) > 3:
            search(args[2], args[3])
        else:
            search(args[2], "")

if __name__ == '__main__':
    main()
 
5
Kudos
 
5
Kudos

Now read this

Haskell: Project Euler 6-10

Due to the popularity of my previous post on the same subject (click here if you haven’t read it), I decided to write a sequel tackling problems 6 through 10. It will be the last one about Project Euler and I hope this pair of articles... Continue →