# Path: pytaxize/refactor.py
# class Refactor:
#     def __init__(self, url, payload={}, request="get"):
#         self.url = url
#         self.payload = payload
#         self.request = request
# 
#     def return_requests(self, **kwargs):
#         if self.request == "get":
#             return requests.get(self.url, params=self.payload, **kwargs)
#         else:
#             return requests.post(self.url, params=self.payload, **kwargs)
# 
#     def xml(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             try:
#                 # If entrez api 'X-RateLimit-Remaining' header is 1 or below, pause for a second to allow rate limit to reset
#                 if (int(out.headers['X-RateLimit-Remaining'])<=1):
#                     time.sleep(1)
#             except:
#                 pass 
#             return tt
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             return tt
# 
#     def json(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
# 
#     def raw(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text
# 
# Path: pytaxize/itis/itis.py
# def _df(x, as_dataframe=False):
#     if as_dataframe and pd:
#         if isinstance(x, dict):
#             x = [x]
#         df = pd.DataFrame.from_records(x)
#         return df
#     else:
#         return x


import sys
import warnings
import requests
import re
import json
import csv
    import pandas as pd
    import doctest
from lxml import etree
from pkg_resources import resource_filename
from pytaxize.refactor import Refactor
from pytaxize.itis.itis import _df
      pytaxize.scrapenames(url = 'http://www.mapress.com/zootaxa/2012/f/z03372p265f.pdf', unique=True)
      pytaxize.scrapenames(url = 'http://www.mapress.com/zootaxa/2012/f/z03372p265f.pdf', all_data_sources=True)

      # Get data from text string as an R object
      pytaxize.scrapenames(text='A spider named Pardosa moesta Banks, 1892')
  """
    method = {"url": url, "file": file, "text": text}
    method = {key: value for key, value in method.items() if value != None}
    if len(method) > 1:
        sys.exit("Only one of url, file, or text can be used")

    base = "http://gnrd.globalnames.org/name_finder.json"
    payload = {
        "url": url,
        "text": text,
        "engine": engine,
        "unique": unique,
        "verbatim": verbatim,
        "detect_language": detect_language,
        "all_data_sources": all_data_sources,
        "data_source_ids": data_source_ids,
    }
    payload = {key: value for key, value in payload.items() if value != None}
    out = requests.get(base, params=payload)
    out.raise_for_status()
    res = out.json()
    data = res["names"]
    meta = res
    meta.pop("names")
    if as_dataframe:

# Path: pytaxize/refactor.py
# class Refactor:
#     def __init__(self, url, payload={}, request="get"):
#         self.url = url
#         self.payload = payload
#         self.request = request
# 
#     def return_requests(self, **kwargs):
#         if self.request == "get":
#             return requests.get(self.url, params=self.payload, **kwargs)
#         else:
#             return requests.post(self.url, params=self.payload, **kwargs)
# 
#     def xml(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             try:
#                 # If entrez api 'X-RateLimit-Remaining' header is 1 or below, pause for a second to allow rate limit to reset
#                 if (int(out.headers['X-RateLimit-Remaining'])<=1):
#                     time.sleep(1)
#             except:
#                 pass 
#             return tt
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             xmlparser = etree.XMLParser()
#             tt = etree.fromstring(out.content, xmlparser)
#             return tt
# 
#     def json(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.json()
# 
#     def raw(self, **kwargs):
#         if self.request == "get":
#             out = requests.get(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text
#         else:
#             out = requests.post(self.url, params=self.payload, **kwargs)
#             out.raise_for_status()
#             return out.text
# 
# Path: pytaxize/itis/itis.py
# def _df(x, as_dataframe=False):
#     if as_dataframe and pd:
#         if isinstance(x, dict):
#             x = [x]
#         df = pd.DataFrame.from_records(x)
#         return df
#     else:
#         return x


# Path: pytaxize/tax.py
import sys
import warnings
import requests
import re
import json
import csv
    import pandas as pd
    import doctest
from lxml import etree
from pkg_resources import resource_filename
from pytaxize.refactor import Refactor
from pytaxize.itis.itis import _df

      pytaxize.scrapenames(url = 'http://www.mapress.com/zootaxa/2012/f/z03372p265f.pdf', unique=True)
      pytaxize.scrapenames(url = 'http://www.mapress.com/zootaxa/2012/f/z03372p265f.pdf', all_data_sources=True)

      # Get data from text string as an R object
      pytaxize.scrapenames(text='A spider named Pardosa moesta Banks, 1892')
  """
    method = {"url": url, "file": file, "text": text}
    method = {key: value for key, value in method.items() if value != None}
    if len(method) > 1:
        sys.exit("Only one of url, file, or text can be used")

    base = "http://gnrd.globalnames.org/name_finder.json"
    payload = {
        "url": url,
        "text": text,
        "engine": engine,
        "unique": unique,
        "verbatim": verbatim,
        "detect_language": detect_language,
        "all_data_sources": all_data_sources,
        "data_source_ids": data_source_ids,
    }
    payload = {key: value for key, value in payload.items() if value != None}
    out = requests.get(base, params=payload)
    out.raise_for_status()
    res = out.json()
    data = res["names"]
    meta = res
    meta.pop("names")
    if as_dataframe:

        data = _df(data, True)