# to run, type python3.x academic_ranking.py
from __future__ import print_function
import http.client
import datetime
import json

last_publication_year_diff = 5  #5 years

#pubcount_min = 120
current_year = datetime.datetime.now().year

areas = {
  'General': 0,
  'Algorithms and Theory': 1,
  'Security and Privacy': 2,
  'Hardware and Architecture' : 3,
  'Software Engineering': 4,
  'Artificial Intelligence': 5,
  'Machine Learning and Pattern Recognition': 6,
  'Data Mining': 7,
  'Information Retrieval': 8,
  'Natural Language Processing': 9,
  'Graphics': 10,
  'Computer Vision': 11,
  'Human-Computer Interaction': 12,
  'Multimedia': 13,
  'Networks and Communication' : 14,
  'World Wide Web' : 15,
  'Distributed and Parallel Computing': 16,
  'Operating Systems': 17,
  'Databases': 18,
  'Real-Time and Embedded Systems' : 19,
  'Simulation' : 20,
  'Bioinformatics and Computational Biology' : 21,
  'Scientific Computing' : 22,
  'Computer Education' : 23,
  'Programming Languages': 24
}

connection = http.client.HTTPConnection('academic.research.microsoft.com')

headers = {'Content-type': 'application/json'}
tmpfile=open('academic_search.raw.data', 'w+')
infoarr = {}

def process_area(a, pubtype, pubtype_caps):
	subdomainid = areas[a]
	connection.request('GET', '/json.svc/search?AppId=4817591c-57f7-4a5f-94d5-ce95565b5a28&DomainID=2&SubDomainID='+str(subdomainid)+'&ResultObjects='+pubtype+'&StartIdx=1&EndIdx=1000&OrderBy=CitationCount', None, headers)
	response = connection.getresponse()
	stringoutput = response.read().decode()
	print(stringoutput.encode('utf-8'), file=tmpfile)
	j = json.loads(stringoutput)
	global infoarr
	#disqualified_arr = {}
	for entry in j['d'][pubtype_caps]['Result']:
		fullname = entry['FullName']
		shortname = entry['ShortName']
		pubcount = entry['PublicationCount']
		endyear = entry['EndYear']
		startyear = entry['StartYear']
		citecount = entry['CitationCount']
		rank = float("%.2f" % (citecount/pubcount))
		infoarr[fullname] = {'shortname' : shortname, 'pubcount' : pubcount, 'citecount' : citecount, 'startyear' : startyear, 'endyear' : endyear, 'rank' : rank, 'area' : a}
		
def print_sorted_infoarr(ofile, disfile):
	global infoarr
	rankarr = {}
	for e in infoarr:
		d = infoarr[e]
		rankarr[e] = d['rank']
	sorted_rankarr = sorted(rankarr, key=rankarr.get, reverse=True);
	ocount = 0
	dis_count = 0
	for a in sorted(areas, key=areas.get):
		print("<h2>"+a+"</h2>", file=ofile)
		print("<h2>"+a+"</h2>", file=disfile)
		print("<table>", file=ofile)
		print("<table>", file=disfile)
		print('<table border="1"><tr bgcolor=yellow>\n', file=ofile);
		print('<table border="1"><tr bgcolor=yellow>\n', file=disfile);
		print("<th></th><th></th><th>Name</th><th>Publication Years</th><th>Publication Count</th><th>Citation Count</th><th>Average Citations per Paper</th>\n", file=ofile);
		print("<th></th><th></th><th>Name</th><th>Publication Years</th><th>Publication Count</th><th>Citation Count</th><th>Average Citations per Paper</th>\n", file=disfile);
		print("</tr>\n", file=ofile);
		print("</tr>\n", file=disfile);
		for e in sorted_rankarr:
			d = infoarr[e]
			endyear = d['endyear']
			rank = d['rank']
			area = d['area']
			if not (area == a):
				continue
			if (endyear >= current_year - last_publication_year_diff) and (rank >= 10):
				print("<tr>", file=ofile);
				rank = rankarr[e]
				print ('<td>'+str(ocount)+'</td><td>'+d['shortname']+'</td><td>'+str(e)+'</td><td>'+str(d['startyear'])+'-'+str(d['endyear'])+'</td><td>'+str(d['pubcount'])+'</td><td>'+str(d['citecount'])+'</td><td>'+str(rank)+'</td>', file=ofile)
				ocount += 1
				print("</tr>", file=ofile);
			else:
				print("<tr>", file=disfile);
				rank = rankarr[e]
				print ('<td>'+str(dis_count)+'</td><td>'+d['shortname']+'</td><td>'+str(e)+'</td><td>'+str(d['startyear'])+'-'+str(d['endyear'])+'</td><td>'+str(d['pubcount'])+'</td><td>'+str(d['citecount'])+'</td><td>'+str(rank)+'</td>', file=disfile)
				dis_count += 1
				print("</tr>", file=disfile);
#				print(d['shortname']+'\t'+e+'\t'+str(endyear)+'\t'+str(rank)+'<br>', file=disfile)
		print("</table>", file=ofile)
		print("</table>", file=disfile)


def process_pubtype(pubtype):
	pubtype_caps = ''
	global infoarr
	infoarr = {}
	ofile = open('cs_'+pubtype+'.html', 'w+');
	disfile = open('cs_'+pubtype+'_low_citation_count.html', 'w+');
	if pubtype == 'conference':
		pubtype_caps = 'Conference'
	else:
		pubtype_caps = 'Journal'
	print("<h1>"+pubtype_caps+"s</h1>", file=ofile)
	print("<h1>"+pubtype_caps+"s (do not meet eligibility criteria of being published in last " + str(last_publication_year_diff) + " years, or have average citation count per paper < 10.0)</h1>", file=disfile)
	for a in sorted(areas, key=areas.get):
		process_area(a, pubtype, pubtype_caps)
	print_sorted_infoarr(ofile, disfile)


process_pubtype('conference')
process_pubtype('journal')
