#!/usr/bin/env python
# coding: utf-8

# In[1]:

from config import *
import requests
import os
from os import path
from glob import glob 


def find_ext(dr, ext):
    return glob(path.join(dr,"*.{}".format(ext)))

# In[2]:



def get_xml(pdf,dir):
	#GROBID_URL = "http://localhost:8070"
	#url = '%s/api/processFulltextDocument' % GROBID_URL
	url = '%s/api/processHeaderDocument' % GROBID_URL
	xml = requests.post(url, files = {'input' : open(pdf, 'rb')}).text
	return xml
	


def write_xml(base_path,xml,dir):
	f = open(base_path+dir+"/"+dir+".xml", "w",encoding='utf-8')
	f.write(xml)
	f.close()

#base_path='C:/Users/hp/Downloads/ETDSummarizationSourceCode.tar/ETDSummarizationSourceCode/pipeline/Etd Data/test_sub/'

#open and read the file after the appending:
def extractXML(base_path):
		y= os.listdir(base_path)
		for i in range(len(y)):
			directory=y[i]
			#print(directory)
			#print(base_path+directory,'pdf')
			paths=find_ext(base_path+'/' +directory,'pdf')
			#print(paths)
			#print(paths[0])
			if len(paths):
				xml=get_xml(paths[0],directory)
				write_xml(base_path,xml,directory)





if __name__ == '__main__':
    extractXML('/home/kkk/Documents/Courses/InfoStorageAndRetrieval/IRDataPreprocessingScripts/')
    







