Beispiel #1
0
    def post(self, request, *args, **kwargs):
        unique_id = request.POST.get('unique_id')
        project_pk = self.kwargs.get('project_pk')

        project_obj = get_object_or_404(Project, pk=project_pk)
        matching_source_obj = None
        for source in PublicationSource.objects.all():
            if source.name == 'doi':
                try:
                    status, bib_str = crossref.get_bib(unique_id)
                    bp = BibTexParser(interpolate_strings=False)
                    bib_database = bp.parse(bib_str)
                    bib_json = bib_database.entries[0]
                    matching_source_obj = source
                    break
                except:
                    continue

            elif source.name == 'adsabs':
                try:
                    url = 'http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode={}&data_type=BIBTEX'.format(
                        unique_id)
                    r = requests.get(url, timeout=5)
                    bp = BibTexParser(interpolate_strings=False)
                    bib_database = bp.parse(r.text)
                    bib_json = bib_database.entries[0]
                    matching_source_obj = source
                    break
                except:
                    continue

        if not matching_source_obj:
            return render(request, self.template_name, {})

        year = as_text(bib_json['year'])
        author = as_text(bib_json['author']).replace(
            '{\\textquotesingle}', "'").replace('{\\textendash}', '-').replace(
                '{\\textemdash}',
                '-').replace('{\\textasciigrave}',
                             ' ').replace('{\\textdaggerdbl}',
                                          ' ').replace('{\\textdagger}', ' ')
        title = as_text(bib_json['title']).replace(
            '{\\textquotesingle}', "'").replace('{\\textendash}', '-').replace(
                '{\\textemdash}',
                '-').replace('{\\textasciigrave}',
                             ' ').replace('{\\textdaggerdbl}',
                                          ' ').replace('{\\textdagger}', ' ')

        author = re.sub("{|}", "", author)
        title = re.sub("{|}", "", title)
        context = {}
        context['author'] = author
        context['year'] = year
        context['title'] = title
        context['unique_id'] = unique_id
        context['source'] = matching_source_obj
        context['project_pk'] = project_obj.pk

        return render(request, self.template_name, context)
Beispiel #2
0
    def _search_id(self, unique_id):
        matching_source_obj = None
        for source in PublicationSource.objects.all():
            if source.name == 'doi':
                try:
                    status, bib_str = crossref.get_bib(unique_id)
                    bp = BibTexParser(interpolate_strings=False)
                    bib_database = bp.parse(bib_str)
                    bib_json = bib_database.entries[0]
                    matching_source_obj = source
                    break
                except:
                    continue

            elif source.name == 'adsabs':
                try:
                    url = 'http://adsabs.harvard.edu/cgi-bin/nph-bib_query?bibcode={}&data_type=BIBTEX'.format(
                        unique_id)
                    r = requests.get(url, timeout=5)
                    bp = BibTexParser(interpolate_strings=False)
                    bib_database = bp.parse(r.text)
                    bib_json = bib_database.entries[0]
                    matching_source_obj = source
                    break
                except:
                    continue

        if not matching_source_obj:
            return False

        year = as_text(bib_json['year'])
        author = as_text(bib_json['author']).replace('{\\textquotesingle}', "'").replace('{\\textendash}', '-').replace(
            '{\\textemdash}', '-').replace('{\\textasciigrave}', ' ').replace('{\\textdaggerdbl}', ' ').replace('{\\textdagger}', ' ')
        title = as_text(bib_json['title']).replace('{\\textquotesingle}', "'").replace('{\\textendash}', '-').replace(
            '{\\textemdash}', '-').replace('{\\textasciigrave}', ' ').replace('{\\textdaggerdbl}', ' ').replace('{\\textdagger}', ' ')

        author = re.sub("{|}", "", author)
        title = re.sub("{|}", "", title)

        # not all bibtex entries will have a journal field
        if 'journal' in bib_json:
            journal = as_text(bib_json['journal']).replace('{\\textquotesingle}', "'").replace('{\\textendash}', '-').replace(
                '{\\textemdash}', '-').replace('{\\textasciigrave}', ' ').replace('{\\textdaggerdbl}', ' ').replace('{\\textdagger}', ' ')
            journal = re.sub("{|}", "", journal)
        else:
            # fallback: clearly indicate that data was absent
            source_name = matching_source_obj.name
            journal = '[no journal info from {}]'.format(source_name.upper())

        pub_dict = {}
        pub_dict['author'] = author
        pub_dict['year'] = year
        pub_dict['title'] = title
        pub_dict['journal'] = journal
        pub_dict['unique_id'] = unique_id
        pub_dict['source_pk'] = matching_source_obj.pk

        return pub_dict
    def _init_expressions(self):
        """
        Defines all parser expressions used internally.
        """
        self._expr = BibtexExpression()

        # Handle string as BibDataString object
        self._expr.set_string_name_parse_action(
            lambda s, l, t: BibDataString(self.bib_database, t[0]))
        if self.interpolate_strings:
            maybe_interpolate = lambda expr: as_text(expr)
        else:
            maybe_interpolate = lambda expr: expr
        self._expr.set_string_expression_parse_action(
            lambda s, l, t: maybe_interpolate(
                BibDataStringExpression.expression_if_needed(t)))

        # Add notice to logger
        self._expr.add_log_function(logger.debug)

        # Set actions
        self._expr.entry.addParseAction(lambda s, l, t: self._add_entry(
            t.get('EntryType'), t.get('Key'), t.get('Fields')))
        self._expr.implicit_comment.addParseAction(
            lambda s, l, t: self._add_comment(t[0]))
        self._expr.explicit_comment.addParseAction(
            lambda s, l, t: self._add_comment(t[0]))
        self._expr.preamble_decl.addParseAction(
            lambda s, l, t: self._add_preamble(t[0]))
        self._expr.string_def.addParseAction(lambda s, l, t: self._add_string(
            t['StringName'].name, t['StringValue']))
    def _clean_val(self, val):
        """ Clean instring before adding to dictionary

        :param val: a value
        :type val: string
        :returns: string -- value
        """
        if not val or val == "{}":
            return ''
        elif self.interpolate_strings:
            return as_text(val)
        else:
            return val
    def _init_expressions(self):
        """
        Defines all parser expressions used internally.
        """
        self._expr = BibtexExpression()

        # Handle string as BibDataString object
        self._expr.set_string_name_parse_action(
            lambda s, l, t:
                BibDataString(self.bib_database, t[0]))
        if self.interpolate_strings:
            maybe_interpolate = lambda expr: as_text(expr)
        else:
            maybe_interpolate = lambda expr: expr
        self._expr.set_string_expression_parse_action(
            lambda s, l, t:
                maybe_interpolate(
                    BibDataStringExpression.expression_if_needed(t)))

        # Add notice to logger
        self._expr.add_log_function(logger.debug)

        # Set actions
        self._expr.entry.addParseAction(
            lambda s, l, t: self._add_entry(
                t.get('EntryType'), t.get('Key'), t.get('Fields'))
            )
        self._expr.implicit_comment.addParseAction(
            lambda s, l, t: self._add_comment(t[0])
            )
        self._expr.explicit_comment.addParseAction(
            lambda s, l, t: self._add_comment(t[0])
            )
        self._expr.preamble_decl.addParseAction(
            lambda s, l, t: self._add_preamble(t[0])
            )
        self._expr.string_def.addParseAction(
            lambda s, l, t: self._add_string(t['StringName'].name,
                                             t['StringValue'])
            )