Advertisement
Guest User

latest_youtube_vid

a guest
May 16th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.52 KB | None | 0 0
  1. from bs4 import BeautifulSoup as bs
  2. import datetime
  3. import requests
  4. from multiprocessing import Pool
  5.  
  6. user_list = ['bigMooney06', 'h3h3Productions', 'UCLtREJY21xRfCuEKvdki1Kw', 'BrothersGreenEats', 'JimSterling',
  7.              'penguinz0', 'LastWeekTonight', 'LinusTechTips', 'foodwishes', 'hickok45', 'bigMooney06',
  8.             'sentdex', 'UCWN3xxRkmTPmbKwht9FuE5A', 'TheReportOfTheWeek', 'HardwareCanucks',
  9.              'UCSdma21fnJzgmPodhC9SJ3g', 'iDubbbzTV', 'h2h2productions', 'WeirdistBuilds','videogamedunkey',
  10.  
  11.                 ]
  12.  
  13. master_dict = {}
  14.  
  15.  
  16. def get_youtube(user):
  17.     """Grabs link to latest youtube video from user"""
  18.     li_list = []
  19.     info_dict = {}
  20.     url = 'https://www.youtube.com'
  21.     v = '/videos'
  22.  
  23.     def string_to_delta(string_delta):
  24.         value, unit, _ = string_delta.split()
  25.         value = int(value)
  26.  
  27.         if not unit.endswith('s'):
  28.             unit += 's'
  29.         if unit == 'months':
  30.             value *= 30
  31.             unit = 'days'
  32.         elif unit == 'years':
  33.             value *= 365
  34.             unit = 'days'
  35.         return datetime.timedelta(**{unit: float(value)})
  36.  
  37.     try:
  38.         raw_html = requests.get(f'{url}/user/{user}{v}').text
  39.         soup = bs(raw_html, 'html.parser')
  40.         first_vid_html = soup.find('div', {'class': 'yt-lockup-content'})
  41.         first_link = url + str(first_vid_html.find(href=True)['href'])
  42.     except AttributeError:
  43.         raw_html = requests.get(f'{url}/channel/{user}{v}').text
  44.         soup = bs(raw_html, 'html.parser')
  45.         first_vid_html = soup.find('div', {'class': 'yt-lockup-content'})
  46.         first_link = url + str(first_vid_html.find(href=True)['href'])
  47.         user = soup.title.string.replace('- YouTube', '').strip()
  48.  
  49.     title = str(first_vid_html.find(class_='yt-lockup-title').text)
  50.     for ultag in first_vid_html.find_all('ul', {'class': 'yt-lockup-meta-info'}):
  51.         for litag in ultag.find_all('li'):
  52.             li_list.append(litag.text)
  53.     post_date = (li_list[1])
  54.     info_dict.update({f"""{user}
  55. {first_link}
  56. {title}""":
  57. post_date})
  58.  
  59.     for k, x in info_dict.items():
  60.         info_dict[k] = string_to_delta(x)
  61.     return info_dict
  62.  
  63.  
  64. def dict_parse(inp_dict):
  65.     for k, v in sorted(inp_dict.items(), key=lambda y: y[1], reverse=True):
  66.         print(k)
  67.         print('Posted ' + str(v).split(',')[0] + ' ago')
  68.         print()
  69.  
  70.  
  71. if __name__ == '__main__':
  72.     p = Pool(processes=len(user_list))
  73.     [master_dict.update(x) for x in p.map(get_youtube, user_list)]
  74.     dict_parse(master_dict)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement