ozzmotik

dm

Jun 6th, 2016
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2.  
  3. from pytube import YouTube
  4. import dailymotion
  5. import os
  6. import sys
  7. from urlparse import urlparse
  8. from pprint import pprint
  9. import subprocess
  10. from apiclient.discovery import build
  11. from apiclient.errors import HttpError
  12. from oauth2client.tools import argparser
  13. import random
  14.  
  15. API_KEY    = "" #dailymotion api key
  16. API_SECRET = "" #dailymotion api secret
  17. USERNAME   = "" #dailymotion username
  18. PASSWORD   = "" #dailymotion password
  19.  
  20. DEVELOPER_KEY = "" #youtube api key
  21. YOUTUBE_API_SERVICE_NAME = "youtube"
  22. YOUTUBE_API_VERSION = "v3"
  23.  
  24. def youtube_search(query,options={}):
  25.   youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
  26.     developerKey=DEVELOPER_KEY)
  27.  
  28.   # Call the search.list method to retrieve results matching the specified
  29.   # query term.
  30.   search_response = youtube.search().list(
  31.     q=query,
  32.     type='video',
  33.     part="id,snippet",
  34.     videoDuration='short',
  35.     maxResults=25
  36.   ).execute()
  37.  
  38.   videos = []
  39.   channels = []
  40.   playlists = []
  41.   for search_result in search_response.get("items", []):
  42.     if search_result["id"]["kind"] == "youtube#video":
  43.       videos.append(search_result["id"]["videoId"])
  44.  
  45.   return videos
  46.  
  47. vds = youtube_search(sys.argv.pop(1)) #use first word on command line as search
  48. #i just pick a random word from 'usr/share/dict/words
  49.  
  50. d = dailymotion.Dailymotion()
  51.  
  52. d.set_grant_type('password', api_key=API_KEY, api_secret=API_SECRET,
  53.     scope=['manage_videos'], info={'username': USERNAME, 'password': PASSWORD})
  54.  
  55. result = random.choice(vds)
  56.  
  57. url = "http://youtube.com/watch?v=%s" % result 
  58. print url
  59. yt = YouTube(url)
  60. fname = yt.filename
  61. if os.path.isfile("/tmp/"+fname+".mp4"):
  62.     print "supa udpa"
  63.     os.remove("/tmp/"+fname+".mp4")
  64. video = yt.get('mp4','360p')
  65. print "downloading " + fname
  66. video.download("/tmp")
  67. print "done downloading " + fname
  68. carg = ["ffmpeg", "-y", "-i", "/tmp/"+fname+".mp4", "-vcodec", "h264", "-acodec", "mp2", "-preset", "ultrafast", "tmp/"+fname+".mp4"]
  69. print " ".join(carg)
  70. FNULL = open(os.devnull, 'w')
  71. subprocess.call(carg,stdout=FNULL, stderr=subprocess.STDOUT)
  72. url = d.upload('tmp/'+fname+'.mp4')
  73. opt = d.post('/me/videos',
  74.     {'url': url, 'title': fname, 'published': 'true', 'channel': 'news'}
  75. )
  76. print("url: http://dailymotion.com/video/"+opt['id'])
  77. os.remove("/tmp/"+fname+".mp4")
  78. os.remove("tmp/"+fname+".mp4")
Advertisement
Add Comment
Please, Sign In to add comment