from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import sys
import requests
import os
import time



def getACMBibTeX():
	cookies = {
	    '__atssc': 'google%3B4',
	    'AK': 'expires%3D1491882135%7Eaccess%3D%2F10%2E1145%2F1350000%2F1348248%2F%2A%7Emd5%3D7588bec54ebd697d657408e93ac9febb',
	    'cffp_mm': '4',
	    'CFID': '749514123',
	    'CFTOKEN': '79742520',
	    'IP_CLIENT': '9941550',
	    'SITE_CLIENT': '5598578',
	    '__atuvc': '23%7C14%2C5%7C15',
	    '__atuvs': '58ef12e243097221003',
	    '_ga': 'GA1.2.1946224287.1491122454',
	}

	headers = {
	    'Accept-Encoding': 'gzip, deflate, sdch',
	    'Accept-Language': 'en-US,en;q=0.8',
	    'Upgrade-Insecure-Requests': '1',
	    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
	    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
	    'Cache-Control': 'max-age=0',
	    'Connection': 'keep-alive',
	}

	URL = sys.argv[1]
	# URL = 'http://dl.acm.org/citation.cfm?id=339421'

	r = requests.get(URL, headers=headers, cookies=cookies)


	########### FOR SAFARI USERS ###########
	driver = webdriver.Safari()

	########## UNCOMMENT TO RUN FIREFOX (Version 50+) IF YOU DON'T HAVE SAFARI ##########
	# profile = webdriver.FirefoxProfile()
	# profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'text/plain')
	# profile.set_preference("browser.download.folderList", 1)
	# # profile.set_preference("browser.download.dir", os.getcwd())
	# driver = webdriver.Firefox(profile)

	driver.get(r.url)

	driver.execute_script("window.scrollBy(300, 0)")

	time.sleep(1)

	bibtex_link = driver.find_element_by_link_text('BibTeX')

	bibtex_link.click()

	time.sleep(1)

	bibtex_text = driver.find_element_by_id('theformats_body')
	while len(bibtex_text.text.encode('utf-8')) < 12:
		bibtex_text = driver.find_element_by_id('theformats_body')

	time.sleep(1)

	driver.find_element_by_link_text('download').click()

	time.sleep(1)

	g()

	driver.quit()

def g():
	path = os.path.expanduser('~/Downloads')
	mylist = os.listdir(path)
	mylist = [os.path.join(path, file) for file in mylist]
	abstractfile = max(mylist, key=os.path.getmtime)
	print open(abstractfile).read()


getACMBibTeX()

a9e02c776f8fb96f258d2fc861d5c07813ae50e5
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
import sys
import requests
import os
import time



def getACMBibTeX():
	cookies = {
	    '__atssc': 'google%3B4',
	    'AK': 'expires%3D1491882135%7Eaccess%3D%2F10%2E1145%2F1350000%2F1348248%2F%2A%7Emd5%3D7588bec54ebd697d657408e93ac9febb',
	    'cffp_mm': '4',
	    'CFID': '749514123',
	    'CFTOKEN': '79742520',
	    'IP_CLIENT': '9941550',
	    'SITE_CLIENT': '5598578',
	    '__atuvc': '23%7C14%2C5%7C15',
	    '__atuvs': '58ef12e243097221003',
	    '_ga': 'GA1.2.1946224287.1491122454',
	}

	headers = {
	    'Accept-Encoding': 'gzip, deflate, sdch',
	    'Accept-Language': 'en-US,en;q=0.8',
	    'Upgrade-Insecure-Requests': '1',
	    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
	    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
	    'Cache-Control': 'max-age=0',
	    'Connection': 'keep-alive',
	}

	URL = sys.argv[1]
	# URL = 'http://dl.acm.org/citation.cfm?id=339421'

	r = requests.get(URL, headers=headers, cookies=cookies)


	########### FOR SAFARI USERS ###########
	driver = webdriver.Safari()

	########## UNCOMMENT TO RUN FIREFOX (Version 50+) IF YOU DON'T HAVE SAFARI ##########
	# profile = webdriver.FirefoxProfile()
	# profile.set_preference("browser.helperApps.neverAsk.saveToDisk", 'text/plain')
	# profile.set_preference("browser.download.folderList", 1)
	# # profile.set_preference("browser.download.dir", os.getcwd())
	# driver = webdriver.Firefox(profile)

	driver.get(r.url)

	driver.execute_script("window.scrollBy(300, 0)")

	time.sleep(1)

	bibtex_link = driver.find_element_by_link_text('BibTeX')

	bibtex_link.click()

	time.sleep(1)

	bibtex_text = driver.find_element_by_id('theformats_body')
	while len(bibtex_text.text.encode('utf-8')) < 12:
		bibtex_text = driver.find_element_by_id('theformats_body')

	time.sleep(1)

	driver.find_element_by_link_text('download').click()

	time.sleep(1)

	g()

	driver.quit()

def g():
	path = os.path.expanduser('~/Downloads')
	mylist = os.listdir(path)
	mylist = [os.path.join(path, file) for file in mylist]
	abstractfile = max(mylist, key=os.path.getmtime)
	print open(abstractfile).read()


getACMBibTeX()
