import feedparser
import json
import re
import os.path
import gzip
import datetime
import time
import urllib
import jinja2

# Load up my template
loader = jinja2.FileSystemLoader('./index.html.j2')
env = jinja2.Environment(loader=loader)
template = env.get_template('')

# delta
delta=2

# Fuck please let this be as comlicated as date time cversions get!
dt = datetime.datetime.today()
curYear = str(dt.year)
_nDaysAgo = dt - datetime.timedelta(days=delta)                   # n days ago
_s = re.match(r"^(.*)\..*$", str(_nDaysAgo)).group(1)             # Datetime string with out the milliseconds attached
__nDaysAgo = time.strptime ( str(_s), "%Y-%m-%d %H:%M:%S" )       # return struct_time - we'll need to do the same for datetimes in the xml feed also.
# Datetime related debugs
#print ( "DEBUG []: " )
#print ( "DEBUG [_nDaysAgo]: "+str(_nDaysAgo) )
#print ( "DEBUG [_s]: "+str(_s))
#print ( "DEBUG [__nDaysAgo]: "+str(__nDaysAgo) )
#print ( "DEBUG []: " )

# working dir
HOME = '/home/slackbot/jerry/CVE/'
feed = '/home/slackbot/jerry/web/cve/cve.xml'

# cve file for curYear
#CVEFILE = 'nvdcve-2.0-' + curYear + '.xml.gz'
#localFile = HOME + CVEFILE
#CVEURL = 'https://nvd.nist.gov/feeds/xml/cve/' + CVEFILE

CVEFILE = 'nvdcve-2.0-Recent.xml.gz'
localFile = HOME + CVEFILE
CVEURL = 'https://nvd.nist.gov/feeds/xml/cve/nvdcve-2.0-Recent.xml.gz'

####### For testing comment these out ##########
print ("Downloading new CVE data "+CVEURL)
urllib.urlretrieve ( CVEURL, localFile )
################################################

# uncompress the file
if os.path.isdir(localFile) == False:
    f = open( feed, 'w' )

    _compressed = gzip.open(localFile, 'r')

    for _uncompressed in _compressed:
        f.write( _uncompressed )

    f.close()

    ####### For testing comment these out ##########
    os.rename(localFile, localFile + '.processed')
    ################################################

print ("Parsing feed "+feed) 
cveFeed = feedparser.parse( feed )
print ("Done parsing feed "+feed) 
cve = []
for cveData in cveFeed['entries']:

    _score = 'NA'
    _ref = 'NA'

    cveDataString = str(cveData['vuln_published-datetime'])
    modDate, modTime = cveDataString.split('T')
    modYear, modMonth, modDay = modDate.split('-')
    
    # returns struct_time and we can compare it to __nDaysAgo
    cveDateTime = time.strptime( modMonth+"-"+modDay+"-"+modYear+" 00:00:00.00", "%m-%d-%Y 00:00:00.00")

    if cveDateTime > __nDaysAgo:
        if 'cvss_score' in cveData:
            _score = str(cveData['cvss_score']) 

        if 'vuln_reference' in cveData:
            _ref = str( cveData['vuln_reference']['href'] )      
        
	cve.append( dict(CVE=str(cveData['vuln_cve-id']),published=str(cveData['vuln_published-datetime']),
			 modified=str(cveData['vuln_last-modified-datetime']),summary=str(cveData['vuln_summary']),
          		 score=_score,reference=_ref) )        

# Open our local index.html for writing. 
l = open( "./web/cve/index.html", 'w' )
for line in template.render(cve=cve,delta=str(delta)):
    l.write( line )
l.close()
