#!/usr/local/cpython-3.7/bin/python

"""Adding rating-reason files to movies."""

import os
import re
# port pprint
import difflib
# port collections

film_names = []


def magic_dir(dirs):
    """Return True if this is a directory that signals a preceeding movie name in the path."""
    for magic_subdir in ['handbrake', '1', 'downloaded', 'youtube']:
        if magic_subdir in dirs:
            return True
    return False


def poison_dir(root):
    """Return True if this is a directory to ignore."""
    regex = re.compile('^.*/Dis[ck] \d+.*$')
    match_object = regex.search(root)
    return bool(match_object)


def gen_movie_names(directory):
    """Generate a list of movie names."""
    seen = set()

    for root, dirs, files in os.walk('.'):
        if magic_dir(dirs) and not poison_dir(root):
            if root.startswith('./'):
                doctored_root = root[2:]
            else:
                doctored_root = root
            if doctored_root not in seen:
                yield doctored_root
                seen.add(doctored_root)


def get_ratings(filename):
    """Read ratings file."""
    # -------------------------------------------------------------------------------
    # MV: $5 a Day (2008)
    # RE: Rated PG-13 for sexual content, brief nudity and language
    #
    # -------------------------------------------------------------------------------
    # MV: $9.99 (2008)
    # RE: Rated R for language and brief sexuality and nudity
    ratings_dict = {}
    movie_name = ''
    ratings_reason_list = []
    # chardet belives the file is windows 1255, but that tracebacks
    # ISO-8859-1 seems to work fine, though the file isn't necessarily
    # ISO-8859-1.
    # with open(filename, 'r', encoding='windows-1255') as ratings_file:
    with open(filename, 'r', encoding='ISO-8859-1') as ratings_file:
        for line in ratings_file:
            if line.startswith('------------------'):
                ratings_dict[movie_name] = ''.join(ratings_reason_list)
                movie_name = ''
                ratings_reason_list = []
            elif line.startswith('MV: '):
                movie_name = line[4:]
            elif line.startswith('RE: '):
                ratings_reason_list.append(line[4:])
    return ratings_dict


def main():
    """Add movie ratings to movies in collection."""
    ratings = get_ratings('/home/dstromberg/src/home-svn/movie-ratings/trunk/mpaa-ratings-reasons.list')

    ratings_movie_names = list(ratings)

    directory = '/mymount/movie'

    os.chdir(os.path.expanduser(directory))

    for disk_movie_name in gen_movie_names(directory):
        print(disk_movie_name)
        matches = difflib.get_close_matches(disk_movie_name, ratings_movie_names)
        if matches:
            best_match = matches[0]
            with open(os.path.join(directory, disk_movie_name, 'rating-reason'), 'w') as file_:
                file_.write('{}\n{}\n{}'.format(disk_movie_name, best_match, ratings[best_match]))


main()