# -*- coding: utf-8 -*-
"""
Created on  Jul 24 21:42:05 2016-2017

@author: Ankit Singh
"""
import requests
from configReader import ConfigurationReader
from Extractor import *
import textwrap
import os, sys, dateutil

from dateutil.parser import parse

class ExtractMainContent(object):
    def __init__(self,source,articleurl):
        self.extractorlist=[HuffingtonPost(),NYT(),BBC(),BloomBerg(),Guardian(),TheHindu(),TimesOfIndia()]
        websites=ConfigurationReader().GetWebsiteSupported()
        self.Mapping={}
        for index,website in enumerate(websites):
            self.Mapping[website]=self.extractorlist[index]
        self.Source=source
        self.url=articleurl
        self.textWrap=textwrap.TextWrapper(initial_indent='\t',subsequent_indent='\t',width=100)
        
        
    def DownloadContent(self):
        headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36'}
        req=requests.get(self.url, headers=headers)
        return req.text
    
    def AddExtractorList(self,extractor):
        self.extractorlist.append(extractor)
    
    def Extract(self):
        self.ExtractStrategy=self.Mapping[self.Source]
        text=self.DownloadContent()
        return self.ExtractStrategy.ExtractionAlgo(text)
    
    def Beautify(self):
        title,output=self.Extract()            
        print "=========================================================================="
        print "\t"+title
        print "=========================================================================="
        print (self.textWrap.fill(output)) #wrap of the line
        print "*********************************************************************************"
        print "\n\n"
        if len(output) == 0:
            print "There isn't much on the site. It is media(video/image) post. To further view the media post, go to the below link"
            print self.url
            print "*********************************************************************************"
            print "\n\n"
    def PrintToFile(self, currArticle):
        reload(sys)
        sys.setdefaultencoding('utf8')
        
        date = dateutil.parser.parse(currArticle[3]).strftime("%y-%m-%d")
        title = currArticle[1]

        filepath = ConfigurationReader().GetDefaultPath() + self.Source + '/' +  date + '/' + title + ".txt"
        
        if not os.path.exists(os.path.dirname(filepath)):
            os.makedirs(os.path.dirname(filepath))

        orig_stdout = sys.stdout
        f = open(filepath, "w")
        sys.stdout = f

        self.Beautify()

        sys.stdout = orig_stdout
        f.close()

        print("SAVED to corresponding source folder in saved_news folder")
            
        
    
    
    
