#!/usr/bin/env python
# -*- coding: utf-8 -*-
import httplib
#import xml.dom.minidom as minidom
#import urllib
import logging
import requests
import time
import sys
import xml.etree.ElementTree as ET
import os
sys.path.append(os.path.abspath(os.path.dirname(__file__) + '../..'))
from text.document import Document
"""
Get texts from PubMed
"""

class PubmedDocument(Document):
    def __init__(self, pmid, **kwargs):
        title, abstract, status = self.get_pubmed_abs(pmid)
        self.abstract = abstract
        self.pmid = pmid
        self.pmcid = None
        super(PubmedDocument, self).__init__(title + "\n" + abstract, ssplit=True, title=title,
                                             did="PMID" + pmid, **kwargs)

    def get_pubmed_abs(self, pmid):
        logging.info("gettting {}".format(pmid))
        #conn = httplib.HTTPConnection("eutils.ncbi.nlm.nih.gov")
        #conn.request("GET", '/entrez/eutils/efetch.fcgi?db=pubmed&id={}&retmode=xml&rettype=xml'.format(pmid))
        payload = {"db": "pubmed", "id": pmid, "retmode": "xml", "rettype": "xml"}
        try:
            r = requests.get('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi', payload)
        except requests.exceptions.ConnectionError:
            r = requests.get('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi', payload)
        # logging.debug("Request Status: " + str(r.status_code))
        response = r.text
        print response
        # logging.info(response)
        title, abstract = self.parse_pubmed_xml(response, pmid)
        return title, abstract, str(r.status_code)

    
    def parse_pubmed_xml(self, xml, pmid):
        print xml
        if xml.strip() == '':
            print "PMID not found", pmid
            sys.exit()
        else:
            root = ET.fromstring(xml.encode("utf-8"))
            title = root.find('.//ArticleTitle')
            if title is not None:
                title = title.text
            else:
                title = ""
            abstext = root.findall('.//AbstractText')
            if abstext is not None and len(abstext) > 0:
                abstext = [a.text for a in abstext]
                if all([abst is not None for abst in abstext]):
                    abstext = '\n'.join(abstext)
                else:
                    abstext = ""
            else:
                print "Abstract not found:", title, pmid
                print xml[:50]
                abstext = ""
                #print xml
                #sys.exit()
            articleid = root.findall('.//ArticleId')
            for a in articleid:
                if a.get("IdType") == "pmc":
                    self.pmcid = a.text[3:]
        return title, abstext

    
def main():
    pubmeddoc = PubmedDocument(sys.argv[1])
    print pubmeddoc
    
if __name__ == "__main__":
    main()
    
@@ -411,16 +411,118 @@
 wargs):%0A
+        self.pmid = pmid%0A        self.pmcid = None%0A        self.tables = %5B%5D%0A        self.figures = %5B%5D%0A
         
@@ -610,38 +610,72 @@
 act%0A        
+if 
 self.pm
+c
 id
- = pm
+:%0A            print %22pmc%22, self.pmc
 id%0A        s
@@ -673,33 +673,43 @@
         
+  
+  
 self.
-pmcid = None
+get_pmc_captions()
 %0A       
@@ -1574,31 +1574,8 @@
 ext%0A
-        print response%0A
     
@@ -1771,26 +1771,8 @@
 d):%0A
-        print xml%0A
     
@@ -2818,16 +2818,1101 @@
 bstext%0A%0A
+    def get_pmc_captions(self):%0A        payload = %7B%22db%22: %22pmc%22, %22id%22: self.pmcid, %22tool%22: %22ibent%22, %22email%22: %22alamurias@lasige.di.fc.ul.pt%22%7D%0A        try:%0A            r = requests.get('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi', payload)%0A        except requests.exceptions.ConnectionError:%0A            r = requests.get('http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi', payload)%0A        # logging.debug(%22Request Status: %22 + str(r.status_code))%0A        response = r.text%0A        root = ET.fromstring(response.encode(%22utf-8%22))%0A        tables = root.findall('.//table-wrap')%0A        figures = root.findall('.//fig')%0A        for t in tables:%0A            caption = ''.join(ET.tostring(e) for e in t.find(%22caption%22))%0A            content = ''.join(ET.tostring(e) for e in t.find(%22table%22))%0A            table = %7B%22caption%22: caption, %22content%22: content%7D%0A            self.tables.append(table)%0A        for f in figures:%0A            caption = ''.join(ET.tostring(e) for e in f.find(%22caption%22))%0A            figure = %7B%22caption%22: caption%7D%0A            self.figures.append(figure)%0A%0A
     %0Adef
@@ -3984,16 +3984,17 @@
 bmeddoc%0A
+%0A
     %0Aif 

Add table and figure caption extraction