- 
      
- 
        Save otienog1/d9ab7f794207616d30cf0ce17c66ce86 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | # coding=utf-8 | |
| import base64 | |
| from urllib2 import urlopen | |
| import tempfile | |
| import os | |
| import tk | |
| import json | |
| class App(tk.Tk): | |
| def __init__(self, title): | |
| tk.Tk.__init__(self) | |
| self.title(title) | |
| install_pytube(reinstall=False) | |
| if tk.py2: | |
| mainframe = tk.Frame(self) | |
| else: | |
| mainframe = tk.Frame(self, padding='3 3 12 12') | |
| mainframe.grid(column=0, row=0, sticky=(tk.N, tk.W, tk.E, tk.S)) | |
| mainframe.columnconfigure(0, weight=1) | |
| mainframe.rowconfigure(0, weight=1) | |
| var_video_id = tk.StringVar(value='Enter YouTube video id or url here') | |
| var_video_title = tk.StringVar() | |
| w_video_id = tk.Entry(mainframe, width=40, textvariable=var_video_id) | |
| w_video_id.grid(column=1, row=1, sticky=(tk.W, tk.E)) | |
| w_video_id.focus() | |
| def select_all(*args): | |
| w_video_id.selection_range(0, tk.END) | |
| select_all() | |
| self.bind('<Command-a>', select_all) | |
| self.bind('<Command-A>', select_all) # in case caps lock | |
| var_videos = tk.StringVar(mainframe) | |
| self.w_videos = None | |
| self.persisted_image = None | |
| self.w_canvas = None | |
| self.w_save_btn = None | |
| def download(*args): | |
| video = self.video_map[var_videos.get()] | |
| path = tk.filedialog.asksaveasfilename( | |
| initialdir=os.environ['HOME'] + '/Downloads', | |
| initialfile='%s.%s' % (video.filename, video.extension), | |
| ) | |
| video.download(path=path) | |
| def make_option_widget(options): | |
| if self.w_videos: | |
| self.w_videos.destroy() | |
| self.w_save_btn.destroy() | |
| var_videos.set(options[0]) # default value | |
| self.w_videos = w = tk.OptionMenu(mainframe, var_videos, *options) | |
| w.grid(column=1, row=4, sticky=tk.W) | |
| self.w_save_btn = w = tk.Button(mainframe, text="Save", | |
| command=download) | |
| w.grid(column=2, row=4, sticky=tk.W) | |
| def make_image_widget(url): | |
| if self.w_canvas: | |
| self.w_canvas.destroy() | |
| self.persisted_image = i = make_image(url) | |
| self.w_canvas = c = tk.Canvas(bg='white', width=i.width(), | |
| height=i.height()) | |
| self.w_canvas.create_image(i.width()/2, i.height()/2, image=self.persisted_image) | |
| c.grid(column=0, row=3, sticky=tk.W) | |
| def fetch(*args): | |
| import pytube | |
| y = pytube.YouTube() | |
| vid = var_video_id.get().rsplit('=', 1)[-1].rsplit('/', 1)[-1] | |
| y.from_url('http://www.youtube.com/watch?v=' + vid) | |
| var_video_title.set(y.title) | |
| self.video_map = {str(v): v for v in y.videos} | |
| make_option_widget(options=y.videos) | |
| make_image_widget(url=y.data['args']['iurlsd']) | |
| self.bind('<Return>', fetch) | |
| tk.Button(mainframe, text="Fetch", command=fetch)\ | |
| .grid(column=2, row=1, sticky=tk.W) | |
| l = tk.Label(mainframe, textvariable=var_video_title, | |
| font=('Arial', 18), fg='red') | |
| l.grid(column=1, row=2, sticky=(tk.W, tk.E)) | |
| def refresh(): | |
| for child in mainframe.winfo_children(): | |
| child.grid_configure(padx=5, pady=5) | |
| refresh() | |
| def install_pytube(path=None, reinstall=False): | |
| if path: | |
| path = path.rstrip('/').rstrip('/pytube') | |
| else: | |
| path = os.path.dirname(os.path.abspath(__file__)) | |
| if not reinstall and os.path.exists(path + '/pytube/__init__.py'): | |
| return | |
| u = 'https://api.github.com/repos/nficano/pytube/releases/latest' | |
| r = urlopen(u) | |
| j = r.read() | |
| d = json.loads(j) | |
| u = d['tarball_url'] | |
| od = os.getcwd() | |
| os.chdir(path) | |
| os.system('rm -r nficano-pytube-* pytube*') | |
| os.system('curl -L "%s" | tar zx' % u) | |
| os.system('cd nficano-pytube-* && mv pytube ../') | |
| os.system('rm -r nficano-pytube-*') | |
| os.system("sed -i '' 's/data = json.loads/data = self.data = json.loads/'" | |
| " pytube/api.py") | |
| os.chdir(od) | |
| def make_image(url): | |
| b = urlopen(url).read() | |
| tf = tempfile.NamedTemporaryFile(delete=False) | |
| tf.write(b) | |
| tf.close() | |
| os.system('sips -s format gif ' + tf.name) | |
| with open(tf.name) as fp: | |
| gif = fp.read() | |
| os.unlink(tf.name) | |
| assert not os.path.exists(tf.name) | |
| return tk.PhotoImage(data=base64.encodestring(gif)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment