# python script to parte articles from Detroit, Chicago and Dallas news feeds
# must first install newspaper
# pip install newspaper#

# 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

igpath=sys.argv[5]
print("\n")
print("py-ig-path=:",igpath)

url=sys.argv[1]
print("\n")
print("py-parse-news-article:",url)
print("\n")

#get output filename
fname=sys.argv[2]
fnpath=igpath+"\\"+fname+".txt"
print("py-output-filename:",fnpath)
print("\n")

# create file with title-in filename use first 30 characters of the string and remove special characters

igtitle=sys.argv[4]
fntitle=igtitle[1:50]
fntitle=fntitle.replace(' ','-')
fntitle=fntitle.replace(':','-')
fntitle=fntitle.replace('/','-')
fntitle=fntitle.replace('?','-')
fntitle=fntitle.replace(',','-')
fntitle=fntitle.replace('Γ','-')
fntitle=fntitle.replace('Ç','-')
fntitle=fntitle.replace('ÿ','-')
fntitle=fntitle.replace('Ö','-')
fntitle=fntitle.replace('"','-')
tfpath=igpath+"\\"+fname+"-"+fntitle+".txt"
print("py-new-output-filename:",tfpath)
print("\n")

with open(tfpath, "w") as tfile:
 
 #add caption title to first line of file
 # DO NOT ADD TITLE TO BEGINNING OF FILE ... comment out lines below
 #caption=sys.argv[4]
 #caption=caption.replace(' ','-')
 #caption=caption.replace(':','-')
 #caption=caption.replace('/','-')
 #caption=caption.replace('?','-')
 #caption=caption.replace(',','-')
 #caption=caption.replace('Γ','-')
 #caption=caption.replace('Ç','-')
 #caption=caption.replace('ÿ','-')
 #caption=caption.replace('Ö','-')
 #caption=caption.replace('"','-')
 #tfile.write("")
 #tfile.write("\r"+caption+" ")
 #tfile.write("\r\n")
 #tfile.write("")
 
 #download and parse article
 article=Article(url)
 article.download()
 article.parse()

 #print text of article to a file
 #limit article to 2200 characters
 tfile.write(article.text[0:2125])


 #add hashtags to end of file
 newssrc=sys.argv[3]
 tfile.write("")
 tfile.write("\r\n"+"#"+newssrc+"  #CrimeInTheA")
 tfile.write("")
 
 tfile.close()
#the end