from __future__ import unicode_literals

import requests

from scrapi import settings
from scrapi.processing.osf.hashing import REPORT_HASH_FUNCTIONS
from scrapi.processing.osf.hashing import RESOURCE_HASH_FUNCTIONS


def detect_collisions(hashlist, additional=''):
    uuids = 'uuid:{}'.format(','.join(hashlist))
    url = '{}?q={}{}'.format(settings.OSF_APP_URL, uuids, additional)

    ret = requests.get(url, auth=settings.OSF_AUTH, verify=settings.VERIFY_SSL).json()

    if ret['total'] > 0:
        return ret['results'][0]['guid']

    return None


def generate_hash_list(normalized, hashes):
    hashlist = []

    for hashfunc in hashes:
        hashlist.append(hashfunc(normalized))

    return hashlist


def generate_resource_hash_list(normalized):
    return generate_hash_list(normalized.attributes, RESOURCE_HASH_FUNCTIONS)


def generate_report_hash_list(normalized):
    return generate_hash_list(normalized.attributes, REPORT_HASH_FUNCTIONS)

@@ -34,16 +34,29 @@
 terals%0A%0A
+import json%0A%0A
 import r
@@ -260,222 +260,959 @@
 st, 
-additional=''):%0A    uuids = 'uuid:%7B%7D'.format(','.join(hashlist))%0A    url = '%7B%7D?q=%7B%7D%7B%7D'.format(settings.OSF_APP_URL, uuids, additional)%0A%0A    ret = requests.get(url, auth=settings.OSF_AUTH, verify=settings.VERIFY_SSL
+is_resource=False):%0A    if is_resource:%0A        _filter = %7B%0A            'terms': %7B%0A                'uuid': hashlist%0A            %7D%0A        %7D%0A    else:%0A        _filter = %7B%0A            'and': %5B%0A                %7B%0A                    'missing': %7B%0A                        'field': 'pid',%0A                        'existence': True,%0A                        'null_value': True%0A                    %7D%0A                %7D,%0A                %7B%0A                    'terms': %7B%0A                        'uuid': hashlist%0A                    %7D%0A                %7D%0A            %5D%0A        %7D%0A%0A    query = %7B%0A        'query': %7B%0A            'filtered': %7B%0A                'filter': _filter%0A            %7D%0A        %7D%0A    %7D%0A%0A    kwargs = %7B%0A        'auth': settings.OSF_AUTH,%0A        'verify': settings.VERIFY_SSL,%0A        'data': json.dumps(query),%0A        'headers': %7B%0A            'Content-Type': 'application/json'%0A        %7D%0A    %7D%0A%0A    ret = requests.post(settings.OSF_APP_URL, **kwargs
 ).js
@@ -1216,17 +1216,16 @@
 .json()%0A
-%0A
     if r
@@ -1279,10 +1279,21 @@
 0%5D%5B'
-gu
+attached'%5D%5B'n
 id'%5D

Update to the latest osf API