blob: 5b5028a926184d9b52d8d3f2f405dddf2f2e87b5 (
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
48
49
50
|
"""This module does render video"""
import sys
import requests
from bs4 import BeautifulSoup
URL = 'https://www.youtube.com'
FILTER = '&sp=EgIQAQ%253D%253D'
INPUT = sys.argv[1]
SEARCH = '%s/results?search_query=%s%s' % (URL, INPUT, FILTER)
REQUEST = requests.get(SEARCH)
SOUP = BeautifulSoup(REQUEST.content, 'lxml', from_encoding=REQUEST.encoding)
FIRST = True
def replace(string):
"""Remove unnecessary characters"""
string = string.replace(' - Duration: ', '')
string = string.replace('.', '')
string = string.replace(' views', '')
return string
for vid in SOUP.find_all(class_='yt-lockup-content'):
try:
link = 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_srt = vid.find(class_='yt-lockup-title').span.text
time = replace(time_srt)
uploaded = meta[0].text
views_str = vid.find(class_='yt-lockup-meta').ul.li.find_next()
views = replace(views_str.text)
except TypeError:
continue
if FIRST:
FIRST = False
else:
print() # print skip line
# prints
print(' title: %s' % title)
print(' url: %s' % link)
print(' channel: %s' % author)
print(' uploaded: %s' % uploaded)
print(' time: %s' % time)
print(' views: %s' % views)
|