Exemple #1
0
def citation_new(cite_type):
    if cite_type == GAME_CITE_REF:
        cite = generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION)
    elif cite_type == PERF_CITE_REF:
        cite = generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION)
    return render_template('citation_new.html', cite_ref=cite, action_url=url_for('citation_add',
                                                                                  cite_type=cite.ref_type))
Exemple #2
0
def citation_add(cite_type):
    clean_params = dict([(k, v) for k, v in request.form.items() if not v or v != 'None'])
    if cite_type == GAME_CITE_REF:
        cite = generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION, **clean_params)
    elif cite_type == PERF_CITE_REF:
        cite = generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION, **clean_params)
    dbm.add_to_citation_table(cite, fts=True)
    return redirect(url_for('citation_page', uuid=cite['uuid']))
Exemple #3
0
def search():
    search_string = request.args.get('search_query', '')
    search_type = request.args.get('search_type', '')
    if search_string:
        search_json = json.dumps({'start_index':0, 'description':{'title': search_string}})
        if not search_type or search_type == 'all':
            proc_args = ['citetool_editor', '--no_prompts', 'search', search_json]
        elif search_type == 'game':
            proc_args = ['citetool_editor', '--no_prompts', 'search', '--game_only', search_json]
        elif search_type == 'performance':
            proc_args = ['citetool_editor', '--no_prompts', 'search', '--perf_only', search_json]
        elif search_type == 'state':
            proc_args = ['citetool_editor', '--no_prompts', 'search', '--state_only', search_json]

        try:
            output = subprocess.check_output(proc_args)
        except subprocess.CalledProcessError as e:
            print(e.message)
            print(e.output)
            game_results = []
            performance_results =[]
            state_results =[]
        else:
            results = json.loads(output)
            results['games'] = map(lambda d: dict((i, d[i]) for i in d if i != 'schema_version'), results['games'])
            results['performances'] = map(lambda d: dict((i, d['performance'][i]) for i in d['performance'] if i != 'schema_version'), results['performances'])
            game_results = map(lambda x: generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION, **x), results['games'])
            performance_results = map(lambda x: generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION, **x), results['performances'])
            state_results = results['states']
    else:
        game_results = []
        performance_results = []
        state_results = []

    return render_template('search.html',
                           game_results=game_results,
                           performance_results=performance_results,
                           state_results=state_results,
                           source_type=search_type,
                           prev_query=search_string,
                           total_results=len(game_results) + len(performance_results) + len(state_results))
    def create_citation(self):
        if not self.extracted_info:
            return []

        element_dict = {'start_datetime':datetime.strptime(self.extracted_info['upload_date'], '%Y%m%d'),
                        'replay_source_purl': self.extracted_info['source_uri'],
                        'replay_source_file_ref': self.extracted_info['source_file_hash'],
                        'replay_source_file_name': self.extracted_info['source_file_name'],
                        'recording_agent': self.extracted_info['uploader'],
                        'title': self.extracted_info['fulltitle'],
                        'description': self.extracted_info['description']}

        citation = generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION, **element_dict)
        return citation, {}
    def create_citation(self):
        citation = generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION)
        cite_map = (('title', 'title'),
                    ('publisher', 'publisher'),
                    ('localization_region', 'localization_region'),
                    ('platform', 'platform'),
                    ('data_image_checksum', 'data_image_checksum'),
                    ('data_image_checksum_type', 'data_image_checksum_type'),
                    ('data_image_source', 'data_image_source'),
                    ('source_data', 'source_data'))
        for extract_key, schema_key in cite_map:
            citation[schema_key] = self.extracted_info[extract_key]

        return citation, {}
    def create_citation(self, ignore_options=False):
        if not self.extracted_info:
            return []

        def add_to_cite_or_options(cite, extracted, e_options, source_key, target_key):
            values = extracted[source_key] if source_key in extracted else None
            if values:
                if isinstance(values, list):
                    if not ignore_options and len(values) > 1:
                        cite[target_key] = None
                        e_options[target_key] = values
                    else:
                        cite[target_key] = values[0]
                else:
                    cite[target_key] = values
            return cite, e_options

        cite_ref = generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION)
        extracted_options = dict()
        for s, t in [
            ('developer', 'developer'),
            ('publisher', 'publisher'),
            ('distributor', 'distributor'),
            ('title', 'title'),
            ('release_date', 'date_published'),
            ('platform', 'platform'),
            ('source_uri', 'source_url'),
            ('source_file_hash', 'source_data')
        ]:
            cite_ref, extracted_options = add_to_cite_or_options(cite_ref, self.extracted_info, extracted_options, s, t)

        if cite_ref['date_published']:
            year = re.search(r"[0-9]{4}", cite_ref['date_published'])
            if year:
                cite_ref['copyright_year'] = year.group()

        return cite_ref, extracted_options
Exemple #7
0
def performance_add(uuid):
    record = json.loads(request.form.get('record'))
    perf_ref = generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION, game_uuid=uuid, **record)
    dbm.add_to_citation_table(perf_ref, fts=True)
    return jsonify({'record': perf_ref.elements})
 def create_citation(self):
     citation = generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION)
     citation['title'] = self.extracted_info['title']
     citation['replay_source_file_ref'] = self.extracted_info['source_file_hash']
     citation['start_datetime'] = self.extracted_info['creation_date']
     return citation, {}
def cite_performance(ctx, export, file_path, url, partial, schema_version):
    verbose = ctx.obj['VERBOSE']
    no_prompts = ctx.obj['NO_PROMPTS']
    alternate_citation = None

    # Make sure that only one input flag is used
    if sum(map(lambda x: 1 if x else 0, [file_path, url, partial])) > 1:
        click.echo('Please use only one option for citation source data.')
        click.echo('Usage:\n\t{} --file_path PATH_TO_FILE\n\t{} --url URL\n\t{} --partial JSON_OBJECT'.format('cite_performance'))
        sys.exit(1)

    if not file_path and not url and not partial:
        if no_prompts:
            cond_print(verbose, 'Cannot create a blank citation with no-prompts flag active.')
        cond_print(verbose, 'Creating a new citation...')
        citation = get_citation_user_input(generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION))
        cond_print(verbose, 'Searching for similar citations...')
        alternate_citation = choose_performance_citation(search_locally_with_citation(citation))

    if file_path:
        extractor = get_extractor_for_file(os.path.expanduser(file_path))
        try:
            extractor.extract()
        except ExtractorError as e:
            click.echo(e.message)
            sys.exit(1)
        citation, extracted_options = extractor.create_citation()
        if not no_prompts:
            citation = get_citation_user_input(citation, extracted_options)
            alternate_citation = search_locally_with_citation(citation)
    elif url:
        try:
            source = get_url_source(url)
        except SourceError as e:
            click.echo(e.message)
            sys.exit(1)

        extractor = get_extractor_for_uri(url, source)
        try:
            extractor.extract()
        except ExtractorError as e:
            click.echo(e.message)
            sys.exit(1)

        #   Block while waiting for extraction to finish, necessary for video downloads
        while not extractor.extracted_info:
            pass
        citation, extracted_options = extractor.create_citation()
        if not no_prompts:
            citation = get_citation_user_input(citation, extracted_options)
            alternate_citation = choose_game_citation(search_locally_with_citation(citation))
    elif partial:
        partial_json = json.loads(partial)
        # Create citation based on partial description
        citation = generate_cite_ref(PERF_CITE_REF, PERF_SCHEMA_VERSION, **partial_json['description'])
        # Search locally based on partial description
        if not no_prompts:
            alternate_citation = choose_performance_citation(search_locally_with_citation(citation))

    if alternate_citation:
        citation = alternate_citation
    else:
        if citation:
            dbm.add_to_citation_table(citation, fts=True)

    if export and citation:
        click.echo(citation.to_json_string())
def cite_game(ctx, file_path, directory, executable, url, title, partial, export, schema_version):
    verbose = ctx.obj['VERBOSE']
    no_prompts = ctx.obj['NO_PROMPTS']
    alternate_citation = None

    # Make sure that only one input flag is used
    if sum(map(lambda x: 1 if x else 0, [file_path, url, title, partial])) > 1:
        click.echo('Please use only one option for citation source data.')
        click.echo('Usage:\n\tcite --file_path PATH_TO_FILE\n\tcite --url URL\n\tcite --title TITLE\n\tcite --partial JSON_OBJECT')
        sys.exit(1)

    # if no input flag
    if not file_path and not url and not title and not partial and not directory:
        # Make a brand new citation
        if no_prompts:
            cond_print(verbose, 'Cannot create a blank citation with no-prompts flag active.')
            sys.exit(1)
        cond_print('Creating new citation...\n', verbose)
        citation = get_citation_user_input(generate_cite_ref(GAME_CITE_REF, schema_version))
        cond_print('Searching for similar citations...\n', verbose)
        # Check for similar citations
        alternate_citation = choose_game_citation(search_locally_with_citation(citation))

    #   FILE PATH citation
    if file_path:
        extractor = get_extractor_for_file(os.path.expanduser(file_path))
        try:
            extractor.extract()
        except ExtractorError as e:
            click.echo(e.message)
            sys.exit(1)
        citation, extracted_options = extractor.create_citation()
        if not no_prompts:
            citation = get_citation_user_input(citation, extracted_options)
            alternate_citation = choose_game_citation(search_locally_with_citation(citation))

    #   DIRECTORY citation
    elif directory:
        extractor = get_extractor_for_directory(os.path.abspath(os.path.expanduser(directory)))
        alternate_citation = None   #   Needed to make sure name is present for check below

        #   Make sure this is a directory
        if not extractor.validate():
            click.echo('{} is not a valid directory.'.format(directory))
            sys.exit(1)

        #   Add in the executable path if provided
        options = {}
        if executable:
            options['main_executable'] = executable

        #   If there's additional information from a partial, load it
        if partial:
            partial_json = json.loads(partial)
            citation = generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION, **partial_json['description'])
        else:
            citation = generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION, title=directory.split('/')[-1])

        #   Get input if prompts are allowed
        if not no_prompts:
            citation = get_citation_user_input(citation)
            alternate_citation = choose_citation(search_locally_with_citation(citation))

        #   Extract all the files and paths
        try:
            extractor.extract(options=options)
        except ExtractorError as e:
            click.echo(e.message)
            sys.exit(1)

        #   Add file_paths to game data store if this is a new citation
        file_info = extractor.extracted_info['file_info']
        if not alternate_citation:
            for fd in file_info:
                fd['game_uuid'] = citation['uuid']
                dbm.insert_into_table(dbm.GAME_FILE_PATH_TABLE, fd.keys(), fd.values())
        else:
        #   Clean up extracted data if alternate citation found (only for prompts call)
            for fd in file_info:
                shutil.rmtree(os.path.join(LOCAL_GAME_DATA_STORE, fd['source_data'], fd['file_path'].split('/')[-1]))

    #   URL citation
    elif url:
        try:
            source = get_url_source(url)
        except SourceError as e:
            click.echo(e.message)
            sys.exit(1)

        extractor = get_extractor_for_uri(url, source)
        try:
            extractor.extract()
        except ExtractorError as e:
            click.echo(e.message)
            sys.exit(1)
        # Block if this is a link to a video or other extractor process
        while not extractor.extracted_info:
            pass
        citation, extracted_options = extractor.create_citation()
        if not no_prompts:
            citation = get_citation_user_input(citation, extracted_options)
            alternate_citation = choose_game_citation(search_locally_with_citation(citation))

    #   TITLE citation
    elif title:
        if no_prompts:
            cond_print(verbose, 'Cannot do citation by title with no-prompts flag active.')
            sys.exit(1)
        # Try a local citation search
        alternate_citation = choose_game_citation(search_locally_with_game_title(title))
        citation = None

        # If that didn't work try to search internet
        # Current hard-coded limit as 10, may allow flag for future
        if not alternate_citation:
            citation = choose_game_citation(search_globally_with_game_title(title, limit=10))

        # If that didn't work, make a new citation
        if not citation and not alternate_citation:
            citation = generate_cite_ref(GAME_CITE_REF,
                                         GAME_SCHEMA_VERSION,
                                         title=title)
            # Edit the citation if needed
            citation = get_citation_user_input(citation)

    #   PARTIAL citation
    elif partial:
        partial_json = json.loads(partial)
        # Create citation based on partial description
        citation = generate_cite_ref(GAME_CITE_REF, GAME_SCHEMA_VERSION, **partial_json['description'])

        if not no_prompts:
            # Search locally based on partial description
            alternate_citation = choose_game_citation(search_locally_with_citation(citation))
            # Search globally if that didn't work
            if not alternate_citation:
                citation = choose_game_citation(search_globally_with_game_partial(partial_json))

    # If an alternate was found, don't do anything
    if alternate_citation:
        citation = alternate_citation
    else:
        # Add the new citation to the database
        if citation:
            dbm.add_to_citation_table(citation, fts=True)

    if export and citation:
        click.echo(citation.to_json_string())
Exemple #11
0
 def create_cite_ref_from_db(cls, ref_type, db_tuple):
     if ref_type == GAME_CITE_REF:
         db_row_dict = dict(zip(cls.headers[cls.GAME_CITATION_TABLE], db_tuple))
     elif ref_type == PERF_CITE_REF:
         db_row_dict = dict(zip(cls.headers[cls.PERFORMANCE_CITATION_TABLE], db_tuple))
     return generate_cite_ref(ref_type, **db_row_dict)  # Schema version is already present