# Path: pyminer/fetcher.py
# class Fetcher(object):
#   """docstring for Fetcher"""
#   def __init__(self, url, headers = {}):
#     super(Fetcher, self).__init__()
#     self.url = url
#     self.headers = headers
# 
#   def perform(self):
#     try:
#       ua = make_ua()
#       ua.update(self.headers)
#       r = requests.get(self.url, headers = ua)
#       r.raise_for_status()
#     except requests.exceptions.HTTPError:
#       r.raise_for_status()
#     except requests.exceptions.RequestException as e:
#       print(e)
# 
#     type = detect_type(r)
#     path = make_path(r, type)
#     write_disk(r, path)
# 
#     return Fetched(self.url, path, type)


from .crutils import *
from .fetcher import Fetcher

        from pyminer import Miner
        import os
        m = Miner(mailto = os.environ['crossref_email'], tdmkey = os.environ['CROSSREF_TDM'])
        x = m.search(ids = "10.1016/j.funeco.2010.11.003")
        out = x.fetch(type = "xml")
        out
        out[0].path
        out[0].parse()

        x = m.search(ids = "10.1111/apt.13556")
        out = x.fetch(type = "unspecified")
        out
        out[0].path
        out[0].parse()
    '''
    # get crossref member id
    z = self.prep_links()
    dat = [ {'member': w['member'], 'links': w['link']} for w in z ]
    put = []
    for i in dat:
      types = [ v['content-type'] for v in i['links'] ]
      if type not in content_type2type(types):
         raise ValueError('type ' + type + ' not found in links')
      headers = cr_auth(i, type, self.tdm_key)
      link = pluck_type(i['links'], type)[0]
      put.append({'member': i['member'], 'link': link, 'headers': headers})
    
    tt = []
    for x in put:

# Path: pyminer/fetcher.py
# class Fetcher(object):
#   """docstring for Fetcher"""
#   def __init__(self, url, headers = {}):
#     super(Fetcher, self).__init__()
#     self.url = url
#     self.headers = headers
# 
#   def perform(self):
#     try:
#       ua = make_ua()
#       ua.update(self.headers)
#       r = requests.get(self.url, headers = ua)
#       r.raise_for_status()
#     except requests.exceptions.HTTPError:
#       r.raise_for_status()
#     except requests.exceptions.RequestException as e:
#       print(e)
# 
#     type = detect_type(r)
#     path = make_path(r, type)
#     write_disk(r, path)
# 
#     return Fetched(self.url, path, type)


# Path: pyminer/mined.py
from .crutils import *
from .fetcher import Fetcher


        from pyminer import Miner
        import os
        m = Miner(mailto = os.environ['crossref_email'], tdmkey = os.environ['CROSSREF_TDM'])
        x = m.search(ids = "10.1016/j.funeco.2010.11.003")
        out = x.fetch(type = "xml")
        out
        out[0].path
        out[0].parse()

        x = m.search(ids = "10.1111/apt.13556")
        out = x.fetch(type = "unspecified")
        out
        out[0].path
        out[0].parse()
    '''
    # get crossref member id
    z = self.prep_links()
    dat = [ {'member': w['member'], 'links': w['link']} for w in z ]
    put = []
    for i in dat:
      types = [ v['content-type'] for v in i['links'] ]
      if type not in content_type2type(types):
         raise ValueError('type ' + type + ' not found in links')
      headers = cr_auth(i, type, self.tdm_key)
      link = pluck_type(i['links'], type)[0]
      put.append({'member': i['member'], 'link': link, 'headers': headers})
    
    tt = []
    for x in put:

      res = Fetcher(x['link'], x['headers']).perform()