#!/usr/local/bin/python
# -*- encoding: iso-8859-15 -*-

#import scraperwiki
import requests
from requests.adapters import HTTPAdapter
import lxml.html
import urllib2
from  urllib2  import HTTPError , URLError
import uuid
import re
import os
import sys
#print sys.path

sys.stdout = open("/var/www/vhosts/thecatchment.org/html/oircal/oircwoutfile.txt","w")
sys.stderr = open("/var/www/vhosts/thecatchment.org/html/oircal/oircerrfile.txt","w")

s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=5))
html = requests.get("http://www.oireachtas.ie/eventsCalendar/conf/en/events/").content
dom = lxml.html.fromstring(html)

def split_lines(text):
	lines = text.split('\r\n')
	regex = re.compile(r'.{1,75}(?:\s+|$)')
	return '\r\n  '.join(s.rstrip() for line in lines for s in regex.findall(line))



newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalurls.txt', 'w')
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalurlswork.txt', 'w')
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerror.txt', 'w')
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerrorpm.txt', 'w')
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerrorhttp.txt', 'w')
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerrorurllib.txt', 'w')


top = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//Microsoft Corporation//Outlook 11.0 MIMEDIR//EN\r\nMETHOD:PUBLISH\r\n"
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'w')	 
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(top)

j = 0;


for entry in dom.cssselect('tbody'):
    #post = {
	title = entry.cssselect('.odd')[0].text_content(); #,
	title = title.strip()
        #'venuetext' : entry.cssselect('.odd')[2].text_content(),
	#i = j;
##'venue' : venuetext.split(' ', 1 ),
	url = "http://www.oireachtas.ie/eventsCalendar/conf/en/events/" + entry.cssselect('a')[1].get('href'); #, #,
   #     'category': entry.cssselect('table a')[0].text_content()
        #'venue' = venue.split(' ', 1 );
        #'venue': entry.cssselect('a')[0].get('href') #,

    ##}
#for testing code
##urls = ["http://www.thecatchment.org/oircal/event-21120s.ics",
##	"http://www.thecatchment.org/oircal/event-20404d.ics","http://www.thecatchment.org/oircal/event-20408d.ics",
##	"http://www.thecatchment.org/oircal/event-19821c.ics","http://www.thecatchment.org/oircal/event-20929c.ics",
##	"http://www.thecatchment.org/oircal/event-20939c.ics","http://www.thecatchment.org/oircal/event-18330c.ics",
##	"http://www.thecatchment.org/oircal/event-18553s.ics","http://www.thecatchment.org/oircal/event-18553s.ics",
##	"http://www.thecatchment.org/oircal/event-20926s.ics","http://www.thecatchment.org/oircal/event-20177d.ics",
##	"http://www.thecatchment.org/oircal/event-20685d.ics","http://www.thecatchment.org/oircal/event-20170d.ics",
##	"http://www.thecatchment.org/oircal/event-19820d.ics","http://www.thecatchment.org/oircal/event-20475d.ics"]	
	
# for testing urls list	
##for url in urls:	
        #venue, rest = venuestring.split(' ', 1 ); 


	urlnewline = url + "\r\n"
	newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalurls.txt', 'a').writelines(urlnewline)
	
	try:	
		eventlines = urllib2.urlopen(url).readlines() #.decode('utf-8')
		print eventlines
		print url
		
		#print i

		

	except urllib2.HTTPError, e:
		print e.code
		print url
		#write http error vars
		httpeargserror = "e.args2http" + str(e.args) + "\r\n"
		httperror = "url2:" + url + "\r\n"
		#writehttperror error checking files
		newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerrorhttp.txt', 'a').writelines(httpeargserror)
		newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerrorhttp.txt', 'a').writelines(httperror)
		
		pass
	#errorcheck url url error	
	except urllib2.URLError, e: #ValueError: 
		print e.args
		print url
		#writeurlerror vars
		urllibeargserror = "e.args2urllib" + str(e.args) + "\r\n"
		urlliberror = "url2:" + url + "\r\n"
		#write url error error checking files
		newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerrorurllib.txt', 'a').writelines(urllibeargserror)
		newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalerrorurlib.txt', 'a').writelines(urlliberror)

     
	urlnote = "onlyloop "
	newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalurlswork.txt', 'a').writelines(urlnote)
	newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircalurlswork.txt', 'a').writelines(urlnewline)
	
	open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(eventlines[4:-4])
	
	description = eventlines[9]
	
	print "description"
	print description
	
#DESCRIPTION;LANGUAGE=en-ie:


	
	description = description.rstrip()
	description = description.strip('DESCRIPTION;LANGUAGE=en-ie:')	
	description = re.escape(description).replace("\ "," ")

						
	
	description = "DESCRIPTION;LANGUAGE=en-ie:" + description
	
	description = split_lines(description) 
						
	print "description"
	print description	
	
	description = description.rstrip()
	description += "\r\n"
	
	
	
	print "description"
	print description	
	open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(description)	
	
	summary = eventlines[10]
	
	
	summary = summary.rstrip()
	summary = summary.strip('SUMMARY;LANGUAGE=en-ie:')	
	summary = re.escape(summary).replace("\ "," ")

						
	
	summary = "SUMMARY;LANGUAGE=en-ie:" + summary
	
	summary = split_lines(summary) 
						
	print "summary"
	print summary	
	
	summary = summary.rstrip()
	summary += "\r\n"
	open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(summary)
	#end
	
	#find which are committees
	
	locationline = eventlines[8] #+ "\r\n"\n
	langlocation = locationline.split(':')
	print "langlocation"
	print langlocation
		
	##locationevent = langlocation[1].strip() #+ "\r\n"
	##locationtag = langlocation[0]
	##locationevent = re.escape(locationevent).replace("\ "," ").replace("\r","")
	##shortlocation = langlocation[1].split(' ')
	##print "shortlocation"
	##print shortlocation
	##venue = shortlocation[0]
	##locationstart = langlocation[0].split(' ')
	##print "locationstart"
	##print locationstart
	##location = locationtag + ":" +  locationevent + "\r\n"		
	##langlocation = location.split(':')
	print "langlocation"
	print langlocation
			
	locationevent = langlocation[1].strip() #+ "\r\n"
	locationtag = langlocation[0]
	langlocat = locationevent.split(' ')
	langlocat = langlocat[0]
	
	
	if langlocat == "Audio" or langlocat == "Committee":
		categories = "CATEGORIES:" + "Committee" + "\r\n"
	###
	elif langlocat == "Seanad":
		categories = "CATEGORIES:" + "Seanad" + "\r\n"
	else:
		categories = "CATEGORIES:" + "Dail" + "\r\n"
	
	##catsum = eventlines[10]
	
	
	##cattagresult = catsum.split(':')
	##catresult = cattagresult.split(' ')
	##print "catresult"
	##print catresult
	#resultevent = catresult[1].strip() #+ "\r\n"
	##categories = catresult[0]
	##print "categories"
	##print categories
	
	open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(categories)
	
	open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(eventlines[11:-2])
	print "eventlines"
	print eventlines[11:-2]
	#print newfile
	ocuuid = str(uuid.uuid4())
	print ocuuid
	ocuuidline = "UID:" + ocuuid + "\r\n"
	open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(ocuuidline)
	urlline = "URL:" + "http://bit.ly/oir-tv" + "\r\n"
	newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(urlline)
	open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(eventlines[11:-1])
	
	
        #i+= 1
	j += 1

bottom = "END:VCALENDAR"
newfile = open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'a').writelines(bottom)
newfilename = "/var/www/vhosts/thecatchment.org/html/oircal/newfilename.ics" #'newfilename.ics'

with open('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics', 'rb') as f:
	data = f.read()
with open('/var/www/vhosts/thecatchment.org/html/oircal/newfilename.ics', 'wb') as f:
	f.write(data.replace('\n\n', '\r\n'))
os.unlink('/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics')
os.rename('/var/www/vhosts/thecatchment.org/html/oircal/newfilename.ics', '/var/www/vhosts/thecatchment.org/html/oircal/oircal.ics')


        #scraperwiki.sql.save(['url'], post)
