'''
Harvester for Dryad for the SHARE project

Example API call: http://www.datadryad.org/oai/request?verb=ListRecords&metadataPrefix=oai_dc
'''
from __future__ import unicode_literals

import logging
from lxml import etree

from scrapi.base import schemas
from scrapi.base import helpers
from scrapi.base import OAIHarvester

logger = logging.getLogger(__name__)


def format_dois_dryad(relations):
    return ['http://dx.doi.org/{}'.format(url.replace('doi:', '')) for url in relations if 'doi:' in url]


class DryadHarvester(OAIHarvester):
    short_name = 'dryad'
    long_name = 'Dryad Data Repository'
    url = 'http://www.datadryad.org/oai/request'

    base_url = 'http://www.datadryad.org/oai/request'
    property_list = ['rights', 'format', 'relation', 'date',
                     'identifier', 'type', 'setSpec']
    timezone_granularity = True

    schema = helpers.updated_schema(
        schemas.OAISCHEMA,
        {
            "uris": {
                "objectUris": ('//dc:relation/node()', format_dois_dryad)
            }
        }
    )

    def normalize(self, raw_doc):
        result = etree.XML(raw_doc['doc'])

        status = (result.xpath('//dc:status/node()', namespaces=self.namespaces) or [''])[0]
        if str(status).lower() in ['deleted', 'item is not available']:
            logger.info('Not normalizing record with ID {}, status {}'.format(raw_doc['docID'], status))
            return None

        return super(OAIHarvester, self).normalize(raw_doc)

@@ -385,24 +385,20 @@
 yad(
-relation
+*arg
 s):%0A    
 retu
@@ -397,16 +397,17 @@
     
+p
 re
-turn %5B
+fix = 
 'htt
@@ -428,79 +428,343 @@
 /%7B%7D'
-.format(url.replace('doi:', '')) for url in relations if 'doi:' i
+%0A    urls = %5B%5D%0A    for arg in args:%0A        if isinstance(arg, list):%0A            for url in arg:%0A                if 'doi:' in url:%0A                    urls.append(prefix.format(url.replace('doi:', '')))%0A        elif arg:%0A            if 'doi:' in arg:%0A                urls.append(prefix.format(arg.replace('doi:', '')))%0A    retur
 n url
-%5D
+s
 %0A%0A%0Ac
@@ -1265,16 +1265,42 @@
 node()',
+ '//dc:identifier/node()',
  format_

Fix DOI finder to include those in identifiers