Advertisement
Masoko

Hass MPC Media Player Posters

Jan 17th, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.56 KB | None | 0 0
  1. # Download the first image from Google Images for a given filename/keyword
  2. # the keyword is taken from the home assistant media player component mentioned on line 41
  3. # this script is used for displaying media posters in home assistant using a local_file camera
  4.  
  5. import os
  6. import argparse
  7. import json
  8. import re
  9. import urllib.request
  10.  
  11. from urllib.request import Request, urlopen
  12.  
  13. # Downloading entire Web Document (Raw Page Content)
  14. def download_page(url):
  15.     headers = {}
  16.     headers[
  17.         'User-Agent'] = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36"
  18.     req = urllib.request.Request(url, headers=headers)
  19.     resp = urllib.request.urlopen(req)
  20.     respData = str(resp.read())
  21.     return respData
  22.  
  23. # Finding 'Next Image' from the given raw page
  24. def get_image(s):
  25.     start_line = s.find('rg_di')
  26.     if start_line == -1:  # If no links are found then give an error!
  27.         end_quote = 0
  28.         link = "no_links"
  29.         return link, end_quote
  30.     else:
  31.         start_line = s.find('"class="rg_meta"')
  32.         start_content = s.find('"ou"', start_line + 1)
  33.         end_content = s.find(',"ow"', start_content + 1)
  34.         content_raw = str(s[start_content + 6:end_content - 1])
  35.         return content_raw
  36.  
  37. if __name__ == '__main__':
  38.     save_path = '/ram'
  39.     file_name = 'movie_data'
  40.  
  41.     # Get file name from home assistant api
  42.     url = 'http://localhost:8123/api/states/media_player.mpclivingroom?api_password=your-hass-password'
  43.     with urllib.request.urlopen(url) as wurl:
  44.         data = json.loads(wurl.read().decode())
  45.     search_keyword = data['attributes']['media_title']
  46.    
  47.     # Check if there is year in the filename and crop it from there
  48.     match = re.match(r'.*([1-3][0-9]{3})', search_keyword)
  49.     if match is not None:
  50.         search_keyword = match.group(0)
  51.  
  52.     # Download Image Links
  53.     print ("Evaluating...")
  54.     search_term = search_keyword
  55.     search = search_term.replace(' ', '%20')
  56.  
  57.     url = 'https://www.google.com/search?q=' + search + '&espv=2&biw=1366&bih=667&site=webhp&source=lnms&tbm=isch&sa=X&ei=XosDVaCXD8TasATItgE&ved=0CAcQ_AUoAg'
  58.     raw_html = (download_page(url))
  59.  
  60.     items = get_image(raw_html)
  61.  
  62.     req = Request(items, headers={
  63.         "User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.27 Safari/537.17"})
  64.     response = urlopen(req, None, 15)
  65.     image_name = file_name
  66.    
  67.     output_file = open(save_path + "/" + image_name + ".jpg", 'wb')
  68.     image_name = image_name + ".jpg"
  69.  
  70.     data = response.read()
  71.     output_file.write(data)
  72.     response.close()
  73.     print("completed ====> " + image_name)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement