#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright (c) 2020,
# Arcangelo Massari <arcangelo.massari@studio.unibo.it>
# Cristian Santini <cristian.santini2@studio.unibo.it>
#
# Permission to use, copy, modify, and/or distribute this software for any purpose
# with or without fee is hereby granted, provided that the above copyright notice
# and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
import csv, requests, json
from os import sep

URL = "http://api.crossref.org/prefixes/"

lista_doi_non_ripetuti = set()

with open(".." + sep + "data" + sep + "dois_no_ref.csv") as csvfile:
    reader = csv.reader(csvfile)
    for row in reader:
        editor = row[0].split("/")
        lista_doi_non_ripetuti.add(editor[0])

for doi in lista_doi_non_ripetuti:
    r = requests.get(url=URL+doi)
    try:
        data = r.json()
    except json.decoder.JSONDecodeError:
        pass
    nome = data["message"]["name"]
    cw = csv.writer(open(".." + sep + "data" + sep + "prefix_publisher.csv", 'a'))
    cw.writerow([doi, nome])











