from django.shortcuts import render
from bs4 import BeautifulSoup
import requests
import pandas
from requests.compat import quote_plus

image_url = 'https://www.google.com/search?q={}&tbm=isch&hl=en&nfpr=1&sa=X&ved=2ahUKEwjvyci8mZntAhWYOewKHXTkCGgQBXoECAEQIg&biw=1519&bih=722'



# Create your views here.

def top10_movies(request):


    data = requests.get('https://www.imdb.com/chart/top/').text
    soup = BeautifulSoup(data, features='html.parser')
    data=soup.body.find('table').tbody.find_all('tr')
    output=[]
    for i in data[0:10]:
        output+=[[i.td.find('a').img['alt'],i.find('strong').text,i.td.find('a').img['src']]]
    
    return render(request,'top/movies.html',{'data':output})

def top10_rich(request):


    data = requests.get('https://en.wikipedia.org/wiki/The_World%27s_Billionaires#2020').text
    soup = BeautifulSoup(data,features='html.parser')
    data = soup.find('table',{'class':"wikitable sortable"})
    data = pandas.read_html(str(data),header=0)[0]
    final_list= data.values.tolist()
    data2=[]
    for i in final_list[0:10]:
        image = requests.get(image_url.format(quote_plus(i[1]))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        data2.append(i+[image_src])

    return render(request,'top/rich.html',{'data':data2})

def top10_instgram(request):

    
    data = requests.get('https://en.wikipedia.org/wiki/List_of_most-followed_Instagram_accounts').text
    soup = BeautifulSoup(data, features='html.parser')
    data = soup.find('table')
    data = pandas.read_html(str(data), header=0)[0]
    final_list = data.values.tolist()
    data2 = []
    for i in final_list[0:10]:
        image = requests.get(image_url.format(quote_plus(i[2]))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        data2.append(i+[image_src])
    return render(request,'top/insta.html',{'data':data2})

def top10_youtube_channels(request):

    
    data = requests.get('https://en.wikipedia.org/wiki/List_of_most-subscribed_YouTube_channels').text
    soup = BeautifulSoup(data,features='html.parser')
    data = soup.find('table',{'class':"wikitable sortable"})
    data = pandas.read_html(str(data),header=0)[0]
    final_list= data.values.tolist()
    data2=[]
    for i in final_list[0:10]:
        i[1]=i[1][:len(i[1])-4]
        image = requests.get(image_url.format(quote_plus(f'youtube channel logo {i[1]}'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        data2.append(i+[image_src])
    print(data2[1])

    return render(request,'top/youtube_channels.html',{'data':data2})


def top10_Influential_Persons(request):
    data = requests.get('https://en.wikipedia.org/wiki/The_100:_A_Ranking_of_the_Most_Influential_Persons_in_History').text
    soup = BeautifulSoup(data, features='html.parser')
    data_soup = soup.find('table', {'class': "wikitable"})
    data = pandas.read_html(str(data_soup), header=0)[0]
    final_list = data.values.tolist()
    s=0
    for i in data_soup.find_all('a',{'class':'image'}):
        final_list[s].append(i.img['src'])
        s+=1

    return render(request,'top/influential_persons.html',{'data':final_list})


def top10_internet_companies(request):

    data = requests.get('https://en.wikipedia.org/wiki/List_of_largest_Internet_companies').text
    soup = BeautifulSoup(data, features='html.parser')
    data = soup.find('table')
    data = pandas.read_html(str(data), header=0)[0]
    final_list = data.values.tolist()
    data2 = []
    for i in final_list[0:10]:
        image = requests.get(image_url.format(quote_plus(f'{i[1]} logo'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        data2.append(i+[image_src])
    return render(request,'top/internet_companies.html',{'data':data2})



def top10_videos(request):
    data = requests.get('https://en.wikipedia.org/wiki/List_of_most-viewed_YouTube_videos').text
    soup = BeautifulSoup(data, features='html.parser')
    data = soup.find('table')
    data = pandas.read_html(str(data), header=0)[0]
    final_list = data.values.tolist()
    data2 = []
    for i in final_list[0:10]:
        i[1]=i[1][:len(i[1])-4]
        image = requests.get(image_url.format(quote_plus(f'{i[1]} youtube video image'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        data2.append(i+[image_src])
    data2[-1][-1]='data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAk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IUXS599hSs617jvt5qkPO/sUy87QChpIRyb7dO1Tbpf7KoSahmBq/VlPjhvPiiaTdpblIX2jwVXGg7fFAkI39hRdLmvI1+SQqTqFu5VuqOntChx+496aO3r9ItrAVzKsZZeK8BnU43DXl4qcYs2yLq06tB5HNY/mrGVII5LhbUC36FYwS2/DY22r0R1GVtW6zgpcYbyZFk45+5XsqW6rHuWNZUHVc9p+it44315cyxcY1usiJQd4714cSI0hr5I8yk2QbXXv4Lx4hJ6ws5vJ385Vxx7S26dWpuQzqM8gm0+t2FNC9V9PHE+1Wtmda2mbbtI27kIXNt5qzUF42O9YdFkIW56Yy9vLj872QTvje+Nwhls6Nxa4HRNrEZhcVq66WaRsk8skzhoDSmkc8gXGV3EmyaF2x+Ncb8o9UZJ25dKtZ0lCF5K+lDv+iEdg7EIUaBdzFRMnShCsSmHfrJTaf1ZJCAJO8IDzv8EIQoc49PQVEv6UIViUB/Yi5TQqiTc87qWiTt8kIUWQx2eIU2v3jxQhQ0hYb7KLmH9ZoQmzQbuv5qWl29BQhVBdu4oI6fFCEqrmi41jtCbOgX3i6EKC8Db5f7Jk5fVCFlS40b2/EV5ayxIz2bxvKSF0xnbNr/2Q=='
    return render(request,'top/videos.html',{'data':data2})

def top10_series(request):


    data = requests.get('https://www.imdb.com/chart/toptv/').text
    soup = BeautifulSoup(data, features='html.parser')
    data=soup.body.find('table').tbody.find_all('tr')
    output=[]
    for i in data[0:10]:
        output+=[[i.td.find('a').img['alt'],i.find('strong').text,i.td.find('a').img['src']]]
    
    return render(request,'top/series.html',{'data':output})


def top10_popluations(request):

    data = requests.get('https://en.wikipedia.org/wiki/List_of_countries_and_dependencies_by_population').text
    soup = BeautifulSoup(data, features='html.parser')
    data = soup.find('table')
    data = pandas.read_html(str(data), header=0)[0]
    final_list = data.values.tolist()
    data2 = []
    for i in final_list[0:10]:
        if '[' in i[1]:
            i[1]=i[1][:len(i[1])-3]
        image = requests.get(image_url.format(quote_plus(f'{i[1]} flag'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        data2.append(i+[image_src])
    return render(request,'top/popluations.html',{'data':data2})

def top10_largest_contries(request):

    data = requests.get('https://the-top-10.fandom.com/wiki/Top_10_Biggest_Countries').text
    soup = BeautifulSoup(data, features='html.parser')
    data = soup.find('table')
    data = pandas.read_html(str(data), header=0)[0]
    final_list = data.values.tolist()
    data2 = []
    for i in final_list[0:10]:
        image = requests.get(image_url.format(quote_plus(f'{i[1]} flag'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        data2.append(i+[image_src])
    return render(request,'top/largest_contries.html',{'data':data2})


def top10_cheap_laptops(request):
    data = requests.get('https://wiki.ezvid.com/best-budget-laptops')
    data_soup = BeautifulSoup(data.text, features='html.parser')
    data = data_soup.find_all('div', {'class': 'item-container'})
    data2 = []
    for i in data:
        image = i.find('div', {'class': 'image-box'}).find('amp-img')['src']
        name = i.find('h3', {'class': 'item-title'}).text
        link = i.find('div',{'class':'item-btn amzn'}).a['href']
        discrption = i.find('div', {'class': "item-review"}).text.replace('\n    \t  \t ','').replace(' \n    \t  \t','')
        data2 += [[ name, discrption,link, image]]

    return render(request,'top/cheap_laptops.html',{'data':data2[::-1]})


def top10_phones(request):
    data = requests.get('https://www.tomsguide.com/best-picks/best-phones')
    data_soup = BeautifulSoup(data.text, features='html.parser')
    data = data_soup.find_all('div', {'class': 'buying-guide-block'})
    final_list= []
    data2=[]
    for i in data:
        name = i.h3.span.text
        link = i.find('div', {'class': 'hawk-affiliate-link-button'}).a['href']
        discrption = i.p.text
        data2 += [[name, discrption, link,]]
    for i in data2:
        image = requests.get(image_url.format(quote_plus(f'{i[0]}'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        final_list.append(i+[image_src])
    return render(request,'top/best_phones.html',{'data':final_list})


def top10_gaming_laptops(request):
    data = requests.get('https://www.laptopmag.com/articles/best-gaming-laptops')
    data_soup = BeautifulSoup(data.text, features='html.parser')
    data = data_soup.find_all('div', {'class': 'buying-guide-block'})
    data2 = []
    final_list=[]
    for i in data:
        name = i.h3.span.text
        link = i.find('div', {'class': 'hawk-affiliate-link-button'}).a['href']
        discrption = i.find('p',{'class':'specs__container'}).text
        data2 += [[name, discrption, link,]]
    for i in data2[0:10]:
        image = requests.get(image_url.format(quote_plus(f'{i[0]} laptop'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        final_list.append(i+[image_src])
    return render(request,'top/gaming_laptops.html',{'data':final_list})


def top10_useful_apps(request):
    data = requests.get(
        'https://greatperformersacademy.com/interesting/top-30-most-useful-apps-you-need-on-your-phone-right-now').text
    data_soup = BeautifulSoup(data, features='html.parser')
    data = data_soup.find_all('h3')
    name = []
    link = []
    for i in data[1:11]:
        name += [i.a.text]
        link += [i.find('a')['href']]
    data = data_soup.find_all('p')
    discrption = []
    for i in data[3:]:
        if len(i.text) > 100:
            discrption += [i.text]
    final_list = []
    for i in range(0, 10):
        final_list += [[name[i], link[i], discrption[i]]]
    final_list2=[]
    for i in final_list:
        image = requests.get(image_url.format(quote_plus(f'{i[0]} app logo'))).text
        image_soup = BeautifulSoup(image, 'html.parser')
        image_src=image_soup.find('img',{'class':'t0fcAb'})['src']
        final_list2.append(i+[image_src])
    return render(request,'top/useful_apps.html',{'data':final_list2})


def home(request):
    return render(request,'base.html')