예제 #1
0
def cite_it(bot, chat_id, doi):
    """Returns citation for given DOI"""
    # headers = {"content-type":"application/x-bibtex"}
    # resp = requests.get("https://doi.org/" + DOI, headers=headers)
    # return resp.content
    works = Works()
    if not works.agency(doi):
        bot.send_message(
            chat_id=chat_id,
            text="Этот документ не входит в базу цитирования CrossRef...")
        return SEARCH_RESULTLS
    else:
        record = works.doi(doi)
        found, meta_bib = get_bib(doi)
        if not found:
            bot.send_message(chat_id=chat_id, text="Документ не найден...")
            return SEARCH_RESULTLS
        bot.send_message(chat_id=chat_id, text="Цитирование по CrossRef:")
        filename = doi.replace('/', '-')
        with open(os.path.join('downloads', filename + '.bib'),
                  'w+') as downloaded_file:
            downloaded_file.write(meta_bib)
        bot.send_document(
            chat_id=chat_id,
            document=open(os.path.join('downloads', filename + '.bib'), 'rb'),
        )
        return SEARCH_RESULTLS
예제 #2
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)
예제 #3
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
예제 #4
0
    def post(self, request, *args, **kwargs):
        project_obj = get_object_or_404(Project,
                                        pk=self.kwargs.get('project_pk'))

        publications_do_export = self.get_publications_to_export(project_obj)
        context = {}

        formset = formset_factory(PublicationExportForm,
                                  max_num=len(publications_do_export))
        formset = formset(request.POST,
                          initial=publications_do_export,
                          prefix='publicationform')

        publications_deleted_count = 0
        bib_text = ''
        if formset.is_valid():
            for form in formset:
                publication_form_data = form.cleaned_data
                if publication_form_data['selected']:

                    publication_obj = Publication.objects.get(
                        project=project_obj,
                        title=publication_form_data.get('title'),
                        year=publication_form_data.get('year'),
                        unique_id=publication_form_data.get('unique_id'),
                    )
                    print("id is" + publication_obj.display_uid())
                    temp_id = publication_obj.display_uid()
                    status, bib_str = crossref.get_bib(
                        publication_obj.display_uid())
                    bp = BibTexParser(interpolate_strings=False)
                    bib_database = bp.parse(bib_str)
                    bib_text += bib_str
            response = HttpResponse(content_type='text/plain')
            response['Content-Disposition'] = 'attachment; filename=refs.bib'
            buffer = io.StringIO()
            buffer.write(bib_text)
            output = buffer.getvalue()
            buffer.close()
            response.write(output)
            return response
        else:
            for error in formset.errors:
                messages.error(request, error)

        return HttpResponseRedirect(
            reverse('project-detail', kwargs={'pk': project_obj.pk}))
예제 #5
0
def make_literature_entry():
    """Read a literature entry, with bibtex."""
    title = input("Title: ")
    authors = get_authors()
    found, bibtex = crossref.get_bib(input("DOI: "))
    print('-' * 40)
    msg = FORMAT.format(title=title, authors=authors, bibtex=bibtex)
    print(msg)
    pyperclip.copy(msg)
    print('-' * 40)
    print('...copied entry to clipboard.')
    print('-' * 40)
    print('Hit <Enter> to copy filename to clipboard')
    filename = title.lower().replace(' ', '-') + ".pdf"
    print('Filename:', filename)
    input()
    pyperclip.copy(filename)
    print('...copied filename to clipboard')
예제 #6
0
    def get_data(self, output=False):
        url = 'http://satellite.mpic.de/spectral_atlas/' + self.spurl
        r = requests.get(url)
        html = r.text
        ind1 = html.find('Data Sets:')
        ind2 = html[ind1:].find('</table>') + ind1
        parsed = BeautifulSoup(html[ind1:ind2], "html.parser")

        i = 0
        files = []
        papers = []
        yr = []
        temp = []
        for i in range(1, len(parsed.table.find_all('tr'))):
            details = parsed.table.find_all('tr')[i].find_all('td')

            # only consider data that is a function of wavelength
            if details[3].text.find('-')<0 and \
               details[-1].text.find('-')>0:
                try:
                    tempp = float(details[3].text[:-1])
                except ValueError:
                    tempp = 300
                if tempp > self.T_low and tempp < self.T_high:
                    try:
                        paper = parsed.table.find_all('tr')[i].find_all(
                            'td')[2].text
                        yr.append(
                            float(paper[paper.find('(') + 1:paper.find(')')]))
                        files.append(
                            parsed.table.find_all('tr')[i].td.a['href'])
                        papers.append(paper)
                        temp.append(tempp)
                    except:
                        yr = yr[:len(temp)]
                        files = files[:len(temp)]
                        papers = papers[:len(temp)]
                        pass

        pre = 'http://joseba.mpch-mainz.mpg.de/spectral_atlas_data/'

        yr1 = []
        files1 = []
        papers1 = []
        refs = []
        temp1 = []
        wv = []
        xs = []
        for i in range(len(files)):
            if self.verbose:
                print(i, ' files parsed', end='\r')
            r = requests.get(pre + files[i])
            data1 = r.text.replace('\t', ' ').replace('\r', '').split('\n')
            wvv1 = []
            xss1 = []
            for dat in data1:
                try:
                    wvv, xss = [float(a) for a in dat.split()]
                    wvv1.append(wvv)
                    xss1.append(xss)
                except:
                    pass
            if len(wvv1) == 0 or np.min(xss1) <= 0:
                pass
            else:
                wv.append(wvv1)
                xs.append(xss1)
                yr1.append(yr[i])
                files1.append(files[i])
                papers1.append(papers[i])
                temp1.append(temp[i])

                # get a bibtex citation
                try:
                    pre_details = 'http://satellite.mpic.de/spectral_atlas/'
                    rr = requests.get(pre_details + files[i])
                    ind1 = [m.start() for m in re.finditer('DOI', rr.text)][1]
                    ind2 = rr.text[ind1:].find('</a>') + ind1 + 4
                    soup = BeautifulSoup(rr.text[ind1:ind2], "html.parser")
                    out = get_bib(soup.a['href'])
                    refs.append(out[1])
                except:
                    refs.append('Resource not found.')

                if len(wv) > self.max_studies:
                    break

        # build dict
        out = {}
        out['wavelength'] = wv
        out['cross section'] = xs
        out['temperature'] = temp1
        out['year'] = yr1
        out['papers'] = papers1
        out['bibtex'] = refs

        self.all_data = out

        # flatten data
        self.all_wv = np.array([item for w in wv for item in w])
        self.all_xs = np.array([item for w in xs for item in w])
        temps = np.array([])
        for i in range(len(temp1)):
            temps = np.append(temps, np.ones(len(wv[i])) * temp1[i])
        self.all_temps = temps

        if output:
            return out