
import re
import random
import json
import requests

# from get_refsets.py
def make_collection(api_url, namespace, nids, title, refset_metadata):
    aliases = [(namespace, nid) for nid in nids]
    url = api_url + "/collection"
    resp = requests.post(
        url,
        data=json.dumps({
            "aliases": aliases,
            "title": title,
            "refset_metadata": refset_metadata
        }),
        headers={'Content-type': 'application/json'}
    )
    collection_id = json.loads(resp.text)["collection"]["_id"]
    return collection_id

# cat */*.bib > all.bib
filename = "/Users/hpiwowar/Dropbox/ti/wos-refset/all.bib"
contents = open(filename, "r").read()
doi_pattern = re.compile("DOI = {{(.+)}},")
all_dois = doi_pattern.findall(contents)

year_pattern = re.compile("Year = {{(.+)}}")
all_years = year_pattern.findall(contents)

doi_years = zip(all_dois, all_years)

title_template = "REFSET {name}, {year} ({genre}) n={sample_size}"
api_url = "http://total-impact-core-staging.herokuapp.com"
#api_url = "http://total-impact-core.herokuapp.com"

for refset_year in range(2002, 2012):
    print refset_year
    dois = [doi for (doi, year) in doi_years if year==unicode(refset_year)]
    print len(dois)
    seed = 42
    random.seed(seed)
    sample_size = 100
    selected = random.sample(dois, min(sample_size, len(dois)))
    refset_metadata = {
        "genre": "article", 
        "version": 0.1,
        "name": "WoS",
        "year": refset_year, 
        "seed": seed,
        "sample_size": sample_size
    }
    title = title_template.format(**refset_metadata)
    make_collection(api_url, "doi", selected, title, refset_metadata)


@@ -1,17 +1,16 @@
-%0A
 import re%0Aimport
@@ -578,16 +578,17 @@
 all.bib%0A
+#
 filename
@@ -638,16 +638,84 @@
 ll.bib%22%0A
+filename = %22/Users/hpiwowar/Dropbox/ti/wos-refset/2012/all2012.bib%22%0A
 contents
@@ -1024,16 +1024,17 @@
 _size%7D%22%0A
+#
 api_url 
@@ -1084,17 +1084,16 @@
 pp.com%22%0A
-#
 api_url 
@@ -1167,16 +1167,16 @@
 e(20
-0
+1
 2, 201
-2
+3
 ):%0A 
@@ -1663,16 +1663,32 @@
 ata)%0A   
+ collection_id =
  make_co
@@ -1745,10 +1745,34 @@
 etadata)
+%0A    print collection_id
 %0A%0A

update to support 2012 wos refset generation