Example #1
0
def snippet_mp3(request):
    repo, commit = repos.repo_from_request(request)
    # Get information needed from settings and repository.
    settings = app_settings()
    full_mp3 = os.path.join(
        settings['fanscribed.audio'],
        '{0}.mp3'.format(request.host),
    )
    transcription_info, _ = repos.json_file_at_commit(
        repo, 'transcription.json', commit, required=True)
    duration = transcription_info['duration']
    snippet_cache = settings['fanscribed.snippet_cache']
    snippet_url_prefix = settings['fanscribed.snippet_url_prefix']
    # Get information needed from GET params.
    starting_point = int(request.GET.getone('starting_point'))
    length = int(request.GET.getone('length'))
    padding = int(request.GET.getone('padding'))
    snippet_path = mp3.snippet_path(
        full_mp3=full_mp3,
        duration=duration,
        output_path=snippet_cache,
        starting_point=starting_point,
        length=length,
        padding=padding,
    )
    relative_path = os.path.relpath(snippet_path, snippet_cache)
    snippet_url = urlparse.urljoin(snippet_url_prefix, relative_path)
    raise HTTPFound(location=snippet_url)
Example #2
0
def transcription_json(request):
    # No rendering or processing, no need to cache.
    repo, commit = repos.repo_from_request(request)
    info, mtime = repos.json_file_at_commit(
        repo, 'transcription.json', commit, required=True)
    # Inject additional information into the info dict.
    settings = app_settings()
    info['snippet_ms'] = int(settings['fanscribed.snippet_seconds']) * 1000
    info['snippet_padding_ms'] = int(float(settings['fanscribed.snippet_padding_seconds']) * 1000)
    return Response(body=json.dumps(info), content_type='application/json')
Example #3
0
def _banned_message(request):
    """Returns a reason why you're banned, or None if you're not banned."""
    ip_address = request.headers.get('X-Forwarded-For', request.remote_addr).strip()
    ip_bans_filename = app_settings().get('fanscribed.ip_address_bans')
    email = request.POST.get('identity_email')
    if ip_bans_filename:
        with open(ip_bans_filename, 'rU') as f:
            for line in f.readlines():
                if ';' in line:
                    candidate_ip_address, reason = line.strip().split(';', 1)
                    if ip_address == candidate_ip_address:
                        return reason.strip()
    if email:
        email_bans_filename = app_settings().get('fanscribed.email_bans')
        if email_bans_filename:
            with open(email_bans_filename, 'rU') as f:
                for line in f.readlines():
                    if ';' in line:
                        candidate_email, reason = line.strip().split(';', 1)
                        if email == candidate_email:
                            return reason.strip()
Example #4
0
def repo_from_request(request, rev=None):
    """Return the repository and commit based on the request.

    The host of the request is inspected to determine the repository.
    The 'rev' GET param is used to determine the commit (default: master).
    """
    repos_path = app_settings()['fanscribed.repos']
    repo_path = os.path.join(repos_path, request.host)
    # Make sure repo path is underneath outer repos path.
    assert '..' not in os.path.relpath(repo_path, repos_path)
    repo = git.Repo(repo_path)
    # Only get rev from user if not specified in function call.
    if rev is None:
        rev = request.GET.get('rev', 'master')
    commit = repo.commit(rev)
    return (repo, commit)
Example #5
0
def _snippet_ms():
    snippet_seconds = int(app_settings()['fanscribed.snippet_seconds'])
    return snippet_seconds * 1000
Example #6
0
def _cache_path():
    path = app_settings()['fanscribed.cache']
    if not os.path.isdir(path):
        os.makedirs(path)
    return path