aboutsummaryrefslogtreecommitdiffstats
path: root/livie.py
blob: dadee46b6395bad41509396e047b1435837d6b34 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""This module does render video"""

import sys
import requests
from bs4 import BeautifulSoup

BASE_URL = 'https://www.youtube.com'

SEARCH_FILTER = '&sp=EgIQAQ%253D%253D'

URL = BASE_URL + '/results?search_query=' + sys.argv[1] + SEARCH_FILTER

HTML = requests.get(URL).text

SOUP = BeautifulSoup(HTML, 'lxml')

FIRST = True

for vid in SOUP.find_all(class_='yt-lockup-content'):
    try:
        link = BASE_URL + vid.h3.a['href']
        title = vid.h3.a.text
        description = vid.h3.span.text
        author = vid.find(class_='yt-lockup-byline').a.text
        meta = vid.find(class_='yt-lockup-meta').ul.contents
        time = meta[0].text
        views_str = meta[-1].text[:-6]
        views = int(views_str.replace(',', ''))

    except TypeError:
        continue

    if FIRST:
        FIRST = False
    else:
        print()

    print(f'     title: {title}')
    print(f'       url: {link}')
    print(f'   channel: {author}')
    print(f'  uploaded: {time}')
    print(f'     views: {views_str}')

# test
# f = open('output.xml','w')
# f.write(str(SOUP))
# f.write(soup.encode('utf-8'))