import csv
import pandas as pd
from webcrawl import WebCrawl

"""
Explanation: <h1><center>Extract Box Office [0, 1000)</center></h1>
End of explanation
"""


movie = pd.read_csv('datasetWithoutBoxOffice.csv') 

movie.head(3)

"""
Explanation: Reading filtered dataset
End of explanation
"""


listWithBoxOffice = [[],[]]

"""
Explanation: List with 
*  First Index - IMDB ID
*  Second Index - Box Office
End of explanation
"""


def extract_boxOffice(tmdbid,imdbid):
    
    # Extract Box office by crawling IMDB page using IMDB ID
    boxOffice = WebCrawl().extractBoxOfficeByIMDB(imdbid)
    
    if boxOffice == 'N/A':
        # If 'N/A' in box office, crawl TMDB web page by using TMDB ID
        boxOffice = WebCrawl().extractBoxOfficeByTMDB(str(tmdbid))
    
    if boxOffice != 'N/A':    
        listWithBoxOffice[0].append(imdbid)
        listWithBoxOffice[1].append(boxOffice)
    else:
        listWithBoxOffice[0].append(imdbid)
        # Appending NaN to Box office so the column will have uniform data type (float) 
        listWithBoxOffice[1].append(float('nan'))
        

"""
Explanation: Function to extract Box Office by Web Crawling
End of explanation
"""


for movieID in movie.values[0:1000]:
    # parameters TMDB ID, IMDB ID
    extract_boxOffice(movieID[0], movieID[1])


"""
Explanation: Optional(but suggested): <div style='color:red'>Duplicate this file and then crawl in each file 1000 entries and then merge the values
 </div>
End of explanation
"""


with open('movie_with_boxoffice_1000.csv', 'w') as csvfile:
    fieldnames = ['IMDB ID','BOX OFFICE']
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    
    writer.writeheader()
    for i in range(len(listWithBoxOffice[0])):
        writer.writerow({'IMDB ID': listWithBoxOffice[0][i], 'BOX OFFICE': listWithBoxOffice[1][i]})

"""
Explanation: Creating a csv file with 1000 entries
End of explanation
"""


