#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 10 23:28:58 2017

@author: dhingratul
"""
import urllib.request
import os
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from bs4 import BeautifulSoup
import ssl
import requests
import wget
from PyPDF2 import PdfFileReader


def download_file(pdf_url, mdir, filename, flag=False):
    if flag is True:
        context = ssl._create_unverified_context()
        response = urllib.request.urlopen(pdf_url, context=context)
    else:
        response = urllib.request.urlopen(pdf_url)

    filename = mdir + filename
    file = open(filename, 'wb')
    file.write(response.read())
    if os.stat(filename).st_size == 0:
        flag = 0
    else:
        flag = 1
    file.close()
    return flag


def download_file_R(pdf_url, mdir, filename, file_out):
    requests.packages.urllib3.disable_warnings()
    while True:  # Keep trying until the webpage successfully downloads
        try:
            r = requests.get(pdf_url, verify=False, timeout=10)
            break  # If it downloads, get out and get on with life
        # If it doesn't download after the timeout period, an exceptions is thrown, and we try again
        except requests.exceptions.RequestException as e:
            with open(file_out, "a") as myfile:
                myfile.write(pdf_url + '\n')

    filename = mdir + filename
    with open(filename,  'wb') as f:
        f.write(r.content)
    if os.stat(filename).st_size == 0:
        flag = 0
    else:
        flag = 1

    return flag


def download_file_W(pdf_url, mdir, filename, flag=False):
    filename = mdir + filename
    ssl._create_default_https_context = ssl._create_unverified_context
    wget.download(pdf_url, filename)
    if os.stat(filename).st_size == 0:
        flag = 0
    else:
        flag = 1
    return flag


def getDriver(url):
    driver = webdriver.Chrome()
    driver.get(url)
    return driver


def is_valid_pdf(fn):
    """Check is the PDF valid """
    try:
        with open(fn, 'rb') as f:
            pdf = PdfFileReader(f)
            numpages = pdf.numPages
        return (numpages > 0)
    except Exception as e:
        return False

[((38, 4, 38, 48), 'requests.packages.urllib3.disable_warnings', 'requests.packages.urllib3.disable_warnings', ({}, {}), '()', False, 'import requests\n'), ((62, 4, 62, 36), 'wget.download', 'wget.download', ({(62, 18, 62, 25): 'pdf_url', (62, 27, 62, 35): 'filename'}, {}), '(pdf_url, filename)', False, 'import wget\n'), ((71, 13, 71, 31), 'selenium.webdriver.Chrome', 'webdriver.Chrome', ({}, {}), '()', False, 'from selenium import webdriver\n'), ((21, 18, 21, 50), 'ssl._create_unverified_context', 'ssl._create_unverified_context', ({}, {}), '()', False, 'import ssl\n'), ((29, 7, 29, 24), 'os.stat', 'os.stat', ({(29, 15, 29, 23): 'filename'}, {}), '(filename)', False, 'import os\n'), ((41, 16, 41, 63), 'requests.get', 'requests.get', (), '', False, 'import requests\n'), ((51, 7, 51, 24), 'os.stat', 'os.stat', ({(51, 15, 51, 23): 'filename'}, {}), '(filename)', False, 'import os\n'), ((63, 7, 63, 24), 'os.stat', 'os.stat', ({(63, 15, 63, 23): 'filename'}, {}), '(filename)', False, 'import os\n'), ((80, 18, 80, 34), 'PyPDF2.PdfFileReader', 'PdfFileReader', ({(80, 32, 80, 33): 'f'}, {}), '(f)', False, 'from PyPDF2 import PdfFileReader\n')]