Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from bs4 import BeautifulSoup as bs
- import datetime
- import requests
- from multiprocessing import Pool
- user_list = ['bigMooney06', 'h3h3Productions', 'UCLtREJY21xRfCuEKvdki1Kw', 'BrothersGreenEats', 'JimSterling',
- 'penguinz0', 'LastWeekTonight', 'LinusTechTips', 'foodwishes', 'hickok45', 'bigMooney06',
- 'sentdex', 'UCWN3xxRkmTPmbKwht9FuE5A', 'TheReportOfTheWeek', 'HardwareCanucks',
- 'UCSdma21fnJzgmPodhC9SJ3g', 'iDubbbzTV', 'h2h2productions', 'WeirdistBuilds','videogamedunkey',
- ]
- master_dict = {}
- def get_youtube(user):
- """Grabs link to latest youtube video from user"""
- li_list = []
- info_dict = {}
- url = 'https://www.youtube.com'
- v = '/videos'
- def string_to_delta(string_delta):
- value, unit, _ = string_delta.split()
- value = int(value)
- if not unit.endswith('s'):
- unit += 's'
- if unit == 'months':
- value *= 30
- unit = 'days'
- elif unit == 'years':
- value *= 365
- unit = 'days'
- return datetime.timedelta(**{unit: float(value)})
- try:
- raw_html = requests.get(f'{url}/user/{user}{v}').text
- soup = bs(raw_html, 'html.parser')
- first_vid_html = soup.find('div', {'class': 'yt-lockup-content'})
- first_link = url + str(first_vid_html.find(href=True)['href'])
- except AttributeError:
- raw_html = requests.get(f'{url}/channel/{user}{v}').text
- soup = bs(raw_html, 'html.parser')
- first_vid_html = soup.find('div', {'class': 'yt-lockup-content'})
- first_link = url + str(first_vid_html.find(href=True)['href'])
- user = soup.title.string.replace('- YouTube', '').strip()
- title = str(first_vid_html.find(class_='yt-lockup-title').text)
- for ultag in first_vid_html.find_all('ul', {'class': 'yt-lockup-meta-info'}):
- for litag in ultag.find_all('li'):
- li_list.append(litag.text)
- post_date = (li_list[1])
- info_dict.update({f"""{user}
- {first_link}
- {title}""":
- post_date})
- for k, x in info_dict.items():
- info_dict[k] = string_to_delta(x)
- return info_dict
- def dict_parse(inp_dict):
- for k, v in sorted(inp_dict.items(), key=lambda y: y[1], reverse=True):
- print(k)
- print('Posted ' + str(v).split(',')[0] + ' ago')
- print()
- if __name__ == '__main__':
- p = Pool(processes=len(user_list))
- [master_dict.update(x) for x in p.map(get_youtube, user_list)]
- dict_parse(master_dict)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement