<commit_before>#!/usr/bin/env python
'''
Test of code for looking up bibliographic info from DOI.

Coded based on guidelines in http://crosscite.org/cn/
using the requests package for python

'''
import sys
import requests
RETURNS_OBJECT = False
if RETURNS_OBJECT:
    import json

def normalize_doi_for_url(raw):
    '''Take a DOI which may start with doi or doi: and return the DOI needed
    for URL construction.
    '''
    if raw.startswith('doi:'):
        raw = raw[4:]
    elif raw.startswith('doi'):
        raw = raw[3:]
    return raw


DOMAIN = 'http://dx.doi.org'

doi = normalize_doi_for_url(sys.argv[1])

SUBMIT_URI = DOMAIN + '/' + doi

payload = {
}
headers = {
    }
if RETURNS_OBJECT:
    headers['content-type'] = 'application/json'
    headers['Accept'] = 'application/vnd.citationstyles.csl+json, application/rdf+json'
else:
    headers['content-type'] = 'application/tex'
    headers['Accept'] = 'text/x-bibliography; style=apa'

sys.stderr.write('Sending GET to "%s"\n' % (SUBMIT_URI))
resp = requests.get(SUBMIT_URI,
                    params=payload,
                    headers=headers,
                    allow_redirects=True)
sys.stderr.write('Sent GET to %s\n' %(resp.url))
resp.raise_for_status()
if RETURNS_OBJECT:
    results = resp.json
    print json.dumps(results, sort_keys=True, indent=4)
else:
    print resp.text<commit_msg>handling 404, 406, and 204 error codes<commit_after>#!/usr/bin/env python
'''
Test of code for looking up bibliographic info from DOI.

Coded based on guidelines in http://crosscite.org/cn/
using the requests package for python

'''
import sys
import requests
RETURNS_OBJECT = False
if RETURNS_OBJECT:
    import json

def normalize_doi_for_url(raw):
    '''Take a DOI which may start with doi or doi: and return the DOI needed
    for URL construction.
    '''
    if raw.startswith('doi:'):
        raw = raw[4:]
    elif raw.startswith('doi'):
        raw = raw[3:]
    return raw


DOMAIN = 'http://dx.doi.org'

doi = normalize_doi_for_url(sys.argv[1])

SUBMIT_URI = DOMAIN + '/' + doi

payload = {
}
headers = {
    }
if RETURNS_OBJECT:
    headers['content-type'] = 'application/json'
    headers['Accept'] = 'application/vnd.citationstyles.csl+json, application/rdf+json'
    requested_formats = "JSON citeproc or RDF"
else:
    headers['content-type'] = 'application/tex'
    headers['Accept'] = 'text/x-bibliography; style=apa'
    requested_formats = "Bibliographic citation (text; style=APA)"

sys.stderr.write('Sending GET to "%s"\n' % (SUBMIT_URI))
resp = requests.get(SUBMIT_URI,
                    params=payload,
                    headers=headers,
                    allow_redirects=True)
sys.stderr.write('Sent GET to %s\n' %(resp.url))
if resp.status_code == 404:
    sys.stderr.write('Requested DOI, "%s", does not exist\n' % doi)
    sys.exit(1)
if resp.status_code == 406:
    sys.stderr.write('DOI found, but unavailable in the requested format(s): %s\n' % requested_formats)
    sys.exit(2)
if resp.status_code == 204:
    sys.stderr.write('DOI found, but no bibliographic information was available')
    sys.exit(3)
resp.raise_for_status()
if RETURNS_OBJECT:
    results = resp.json
    print json.dumps(results, sort_keys=True, indent=4)
else:
    print resp.text
{'fraction_non_alphanumeric': 0.12826020251610923, 'fraction_numerical': 0.008898435102792267, 'mean_word_length': 3.340878828229028, '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': '1396243', 'n_tokens_mistral': 1140, 'n_tokens_neox': 1068, 'n_words': 278}