<commit_before># -*- coding: utf-8 -*-

from __future__ import absolute_import

from os import path
import tempfile

from django.core.files import File

from django.conf import settings

import requests


def _set_from_url(film, url):
    print "Retrieving from", url
    request = requests.get(url, stream=True)
    if request.status_code != requests.codes.ok:
        raise KeyError(url)
    lf = tempfile.NamedTemporaryFile()
    for block in request.iter_content(1024 * 8):
        if not block:
            break
        lf.write(block)
    filename = path.join('posters', str(film.film_id) + ".jpg")
    film.poster_file.save(name=filename, content=File(lf))


def _get_trakt_url(imdb_id):
    summary_url = "http://api.trakt.tv/movie/summary.json/{apikey}/tt{imdb_id}".format(
        apikey=settings.TRAKT_APIKEY,
        imdb_id=imdb_id)
    summary = requests.get(summary_url)
    if summary.status_code != requests.codes.ok:
        raise KeyError(imdb_id)

    poster_url = summary.json()['images']['poster']

    return poster_url[:poster_url.rindex('.jpg')] + "-300.jpg"


def retrieve(film):
    print film
    if film.poster:
        try:
            return _set_from_url(film, film.poster)
        except KeyError:
            pass

    try:
        url = _get_trakt_url(film.imdb_id)
        _set_from_url(film, url)
        print "\tBut in Trakt"
    except KeyError:
        film.poster_file = path.join('posters', 'noposter.png')
        film.save()
<commit_msg>Remove debug messages<commit_after># -*- coding: utf-8 -*-

from __future__ import absolute_import

from os import path
import tempfile

from django.core.files import File

from django.conf import settings

import requests


def _set_from_url(film, url):
    request = requests.get(url, stream=True)
    if request.status_code != requests.codes.ok:
        raise KeyError(url)
    lf = tempfile.NamedTemporaryFile()
    for block in request.iter_content(1024 * 8):
        if not block:
            break
        lf.write(block)
    filename = path.join('posters', str(film.film_id) + ".jpg")
    film.poster_file.save(name=filename, content=File(lf))


def _get_trakt_url(imdb_id):
    summary_url = "http://api.trakt.tv/movie/summary.json/{apikey}/tt{imdb_id}".format(
        apikey=settings.TRAKT_APIKEY,
        imdb_id=imdb_id)
    summary = requests.get(summary_url)
    if summary.status_code != requests.codes.ok:
        raise KeyError(imdb_id)

    poster_url = summary.json()['images']['poster']

    return poster_url[:poster_url.rindex('.jpg')] + "-300.jpg"


def retrieve(film):
    if film.poster:
        try:
            return _set_from_url(film, film.poster)
        except KeyError:
            pass

    try:
        url = _get_trakt_url(film.imdb_id)
        _set_from_url(film, url)
    except KeyError:
        film.poster_file = path.join('posters', 'noposter.png')
        film.save()

{'fraction_non_alphanumeric': 0.11813471502590674, 'fraction_numerical': 0.0062176165803108805, 'mean_word_length': 2.9029649595687332, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '629185', 'n_tokens_mistral': 997, 'n_tokens_neox': 962, 'n_words': 196}