# coding: utf-8
import json
import urllib2
import urllib
import dbutil
import time
from test.sortperf import doit
import traceback
from utils import *

def citationtext2doi(fullref):
    asciiquery = fullref.encode('ascii', 'replace')
    asciiquery = asciiquery.replace(":"," ").replace("["," ").replace("]"," ")
    asciiquery = asciiquery.replace("("," ").replace(")"," ").replace("-"," ")
    asciiquery = asciiquery.replace('"', '').replace("/"," ").replace("\\", " ")
    asciiquery = asciiquery.replace('}', '').replace('{','').replace('*','')
    query = urllib.quote_plus(asciiquery)
    print query
    queryresult = urllib2.urlopen("http://api.crossref.org/works?rows=1&query=" + query).read()
    result = json.loads(queryresult)
    print json.dumps(result, indent=4)
    if len(result["message"]["items"]) > 0:
        return result["message"]["items"][0]
    else:
        return dict()
    
def createSyntheticCitations(conn):
    needCites = conn.execute("select name, authors, title from packages where name not in (select distinct(package_name) from citations);")
    conn.executemany("insert into citations (package_name, citation, canonical) values (?,?,?);",
        [(cite["packages.name"], stripParentheticals(cite["packages.authors"].replace("Author:","")) + 
          " " + justAlphabetics(cite["packages.title"]), "synthetic") for cite in needCites
            ])
    conn.commit()
    
def fillInDois(conn):
    unannotated = conn.execute("select rowid, * from citations where doi = '' or doi is null;")
    toupdate = dict()
    import pdb
    try:
        for cite in unannotated:
            print "Looking up DOI for", cite["citations.package_name"], ": ", cite["citations.citation"]
            time.sleep(2)
            refinfo = citationtext2doi(cite["citations.citation"])
            print "  --->", refinfo["DOI"], refinfo["score"], refinfo["title"][0] if refinfo["title"] else "none", "\n"
            toupdate[cite["citations.rowid"]] = refinfo
    except Exception, e:
        print e
        import pdb
        pdb.set_trace()
    finally:
        To do: add authors (toupdate[r]["author"][x]["family"], given) to title, just to be sure
        conn.executemany("update citations set doi = ?, doi_title=?, doi_confidence=? where rowid=?;", 
                         [(toupdate[r]["DOI"], toupdate[r]["title"][0] if toupdate[r]["title"] else "none", toupdate[r]["score"], r) for r in toupdate])
        conn.commit()
    
if __name__ == '__main__':
    fullref = u'Carvalho BS, Louis TA and Irizarry RA (2010). “Quantifying uncertainty in genotype calls.” Bioinformatics, 15;26(2), pp. 242-9.'
    print citationtext2doi(fullref)
    fr2 = u'J S, T N, K S and K K (2013). “TCC: an R package for comparing tag count data with robust normalization strategies.” BMC Bioinformatics, 14, pp. 219.';
    print citationtext2doi(fr2)

@@ -739,47 +739,8 @@
 lt)%0A
-    print json.dumps(result, indent=4)%0A
     
@@ -1365,24 +1365,312 @@
 mmit()%0A    %0A
+def flattenJson(jsonobj):%0A    if isinstance(jsonobj, list):%0A        return %22 %22.join(%5BflattenJson(l) for l in jsonobj%5D)%0A    elif isinstance(jsonobj, dict):%0A        return %22 %22.join(%5BflattenJson(jsonobj%5Bk%5D) for k in jsonobj%5D)%0A    else:%0A        return jsonobj.encode('ascii', 'replace')%0A    %0A
 def fillInDo
@@ -1771,16 +1771,25 @@
  is null
+ limit 50
 ;%22)%0A    
@@ -1863,16 +1863,98 @@
 otated:%0A
+            if cite%5B%22citations.citation%22%5D.strip() == %22%22:%0A                continue%0A
         
@@ -2428,28 +2428,83 @@
     
-To do: add 
+for r in toupdate:%0A            auth = toupdate%5Br%5D%5B%22author%22%5D if %22
 author
-s (
+%22 in 
 toup
@@ -2514,66 +2514,175 @@
 e%5Br%5D
-%5B%22author%22%5D%5Bx%5D%5B%22family%22%5D, given) to title, just to be sure%0A
+ else %22no author%22%0A            title = toupdate%5Br%5D%5B%22title%22%5D%5B0%5D if toupdate%5Br%5D%5B%22title%22%5D else %22no title%22%0A            citationinfo = flattenJson(auth) + %22, %22 + title %0A    
     
@@ -2697,20 +2697,16 @@
 .execute
-many
 (%22update
@@ -2802,17 +2802,16 @@
         
-%5B
 (toupdat
@@ -2827,67 +2827,20 @@
 %22%5D, 
-toupdate%5Br%5D%5B%22title%22%5D%5B0%5D if toupdate%5Br%5D%5B%22title%22%5D else %22none%22
+citationinfo
 , to
@@ -2865,27 +2865,8 @@
 , r)
- for r in toupdate%5D
 )%0A  

Save authorship and title of retrieved doc along with searched-for doc in citations table, as a way of checking how good the match is.