# python script to parte articles from Detroit, Chicago and Dallas news feeds
# must first install newspaper
# pip install newspaper#
# pip3 install newspaper3k                                                

# uset wget get to download the image associated with the story
#wget -O image-file.jpg "https://images.foxtv.com/static.fox2detroit.com/www.fox2detroit.com/content/uploads/2020/11/1280/720/wjbk-vehicles-stolen-112320.jpg?ve=1\u0026tl=1"

#pass in the url link to the story
# e.g. "https://www.fox2detroit.com/news/detroit-police-investigating-multi-car-theft-at-east-side-dealership"

# start exection of script
#parse-detroit-articles.py ""https://www.fox2detroit.com/news/detroit-police-investigating-multi-car-theft-at-east-side-dealership"" "output-file.caption"

import sys
import os
from newspaper import Article

#get output filename
fname="article-summary.txt"

myfile=open(fname,"r")
article=myfile.read()
myfile.close()
#print("\nPRINTING ARTICLE ------")
#print(article)
#print("\nDONE PRINTING ARTICLE ------")


with open(fname, "w") as tfile:
 
  #download and parse article
               

 #print text of article to a file
 #limit article to 2200 characters
 #tfile.write(article.text[0:2125])
 modifiedarticle=article[0:2125].replace('—','-')
 modifiedarticle=modifiedarticle.replace('’','\'')
 modifiedarticle=modifiedarticle.replace('′','\'')
 modifiedarticle=modifiedarticle.replace('“','"')
 modifiedarticle=modifiedarticle.replace('”','"')
 modifiedarticle=modifiedarticle.replace('©',' ')
 modifiedarticle=modifiedarticle.replace('�',' ')
 modifiedarticle=modifiedarticle.replace('▼',' ')
 modifiedarticle=modifiedarticle.replace('ù',' ')
 tfile.write(modifiedarticle[0:2125])

 #add hashtags to end of file
 newssrc="fox5atlanta"
 tfile.write("")
 tfile.write("\r\n"+"#"+newssrc+"  #CrimeNewsInTheATL")
 tfile.write("")
 tfile.close()
 
print("\nMODIFIED")
print(modifiedarticle)
print("\n")
 
with open(fname, "r+") as tfile:
 finalsummary=''
 modifiedarticle=''
 #remove unwanted tags from article summary file
 for line in tfile:
  if "article" in line:
     print("\r\n-SKIPPED-"+line)
  elif "Next up in 5" in line:
     print("\r\n-SKIPPED-"+line)
  elif "Example video title" in line:
     print("\r\n-SKIPPED-"+line)   
  elif "[DOWNLOAD:" in line:
     print("\r\n-SKIPPED-"+line)
  elif "TRENDING STORIES:" in line:
     print("\r\n-SKIPPED-"+line)
  elif "[SIGN UP:" in line:
        print("\r\n-SKIPPED-"+line)   
  elif "Advertisement" in line:
        print("\r\n-SKIPPED-"+line)
  else:
   finalsummary=finalsummary+line    
 tfile.write(finalsummary)
 tfile.close()
#the end

print("\nFINAL SUMMARY")
print(finalsummary)
print("\n")

# write final summary without tags to file 
with open(fname, "w") as summary: 
 summary.write(finalsummary)
 summary.close()