1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-12-25 06:37:29 +00:00

WIP YouTube upload command

This commit is contained in:
Myned 2017-10-11 03:01:13 -04:00
parent 89329f52c3
commit 1415c92fcc

View file

@ -1,4 +1,5 @@
import asyncio import asyncio
import datetime as dt
import discord import discord
import httplib2 import httplib2
import mimetypes import mimetypes
@ -8,22 +9,18 @@ import tempfile
import traceback import traceback
import webbrowser import webbrowser
from discord.ext import commands from discord.ext import commands
#from run import config
from cogs import booru from cogs import booru
from misc import checks from misc import checks
from misc import exceptions as exc from misc import exceptions as exc
from utils import formatter from utils import formatter
from apiclient.discovery import build from apiclient.discovery import build
from apiclient.http import MediaFileUpload from apiclient import http
from oauth2client.client import flow_from_clientsecrets from oauth2client.client import flow_from_clientsecrets
youtube = None
# flow = flow_from_clientsecrets('../client_secrets.json', scope='https://www.googleapis.com/auth/youtube.upload', login_hint='botmyned@gmail.com', redirect_uri='urn:ietf:wg:oauth:2.0:oob') tempfile.tempdir = os.getcwd()
# flow.params['access_type'] = 'offline'
# webbrowser.open_new(flow.step1_get_authorize_url())
# credentials = flow.step2_exchange(input('Authorization code: '))
# youtube = build('youtube', 'v3', http=credentials.authorize(httplib2.Http()))
tempfile.tempdir = '../temp'
command_dict = {} command_dict = {}
@ -41,7 +38,7 @@ class Utils:
print(command_dict) print(command_dict)
await ctx.invoke(command_dict.get(str(ctx.message.author.id), {}).get('command', None), args) await ctx.invoke(command_dict.get(str(ctx.message.author.id), {}).get('command', None), args)
except Exception: except Exception:
await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```')
traceback.print_exc(limit=1) traceback.print_exc(limit=1)
# [prefix]ping -> Pong! # [prefix]ping -> Pong!
@ -52,7 +49,7 @@ class Utils:
try: try:
await ctx.send(ctx.message.author.mention + ' 🏓 `' + str(int(self.bot.latency * 1000)) + 'ms`', delete_after=5) await ctx.send(ctx.message.author.mention + ' 🏓 `' + str(int(self.bot.latency * 1000)) + 'ms`', delete_after=5)
except Exception: except Exception:
await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```')
traceback.print_exc(limit=1) traceback.print_exc(limit=1)
command_dict.setdefault(str(ctx.message.author.id), {}).update({'command': ctx.command}) command_dict.setdefault(str(ctx.message.author.id), {}).update({'command': ctx.command})
@ -60,9 +57,9 @@ class Utils:
@checks.del_ctx() @checks.del_ctx()
async def prefix(self, ctx): async def prefix(self, ctx):
try: try:
await ctx.send('**Prefix:** `,` or ' + ctx.me.mention) await ctx.send('**Prefix:** `,`')
except Exception: except Exception:
await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```')
traceback.print_exc(limit=1) traceback.print_exc(limit=1)
@commands.group(name=',send', aliases=[',s'], hidden=True) @commands.group(name=',send', aliases=[',s'], hidden=True)
@ -78,17 +75,42 @@ class Utils:
async def send_user(self, ctx, user, *message): async def send_user(self, ctx, user, *message):
await discord.utils.get(self.bot.get_all_members(), id=int(user)).send(formatter.tostring(message)) await discord.utils.get(self.bot.get_all_members(), id=int(user)).send(formatter.tostring(message))
@commands.command(aliases=['authenticateupload', 'authupload', 'authup', 'auth'])
async def authenticate_upload(self, ctx):
global youtube
flow = flow_from_clientsecrets('client_secrets.json', scope='https://www.googleapis.com/auth/youtube.upload', login_hint='botmyned@gmail.com', redirect_uri='urn:ietf:wg:oauth:2.0:oob')
flow.params['access_type'] = 'offline'
webbrowser.open_new_tab(flow.step1_get_authorize_url())
credentials = flow.step2_exchange(input('Authorization code: '))
youtube = build('youtube', 'v3', http=credentials.authorize(http.build_http()))
print('Service built.')
@commands.command(aliases=['up', 'u', 'vid', 'v']) @commands.command(aliases=['up', 'u', 'vid', 'v'])
@checks.is_listed() @checks.is_listed()
async def upload(self, ctx): async def upload(self, ctx):
global youtube global youtube
attachments = ctx.message.attachments
try: try:
print(mimetypes.guess_type(ctx.message.attachments[0].filename)) if not attachments:
with tempfile.TemporaryFile() as temp: raise exc.MissingAttachment
await ctx.message.attachments[0].save(temp) if len(attachments) > 1:
print(os.path.basename('../temp/*')) raise exc.TooManyAttachments(len(attachments))
print(mimetypes.guess_type(os.path.basename('../temp/*'))) mime = mimetypes.guess_type(attachments[0].filename)[0]
# print('https://www.youtube.com/watch?v=' + youtube.videos().insert(part='snippet', body={'categoryId': '24', 'title': 'Test'}, media_body=MediaFileUpload('../temp/*', chunksize=-1)) if 'video/' in mime:
with tempfile.NamedTemporaryFile() as temp:
await attachments[0].save(temp)
else:
raise exc.InvalidVideoFile(mime)
print('https://www.youtube.com/watch?v=' + youtube.videos().insert(part='snippet', body={'categoryId': '24', 'title': 'Test'}, media_body=http.MediaFileUpload(temp.name, chunksize=-1)))
except exc.InvalidVideoFile as e:
await ctx.send('❌ `' + str(e) + '` **not valid video type.**', delete_after=10)
except exc.TooManyAttachments as e:
await ctx.send('❌ `' + str(e) + '` **too many attachments.** Only one attachment is permitted to upload.', delete_after=10)
except exc.MissingAttachment:
await ctx.send('❌ **Missing attachment.**', delete_after=10)
except Exception: except Exception:
await ctx.send(exc.base + '\n```python' + traceback.format_exc(limit=1) + '```') await ctx.send(exc.base + '\n```' + traceback.format_exc(limit=1) + '```')
traceback.print_exc(limit=1) traceback.print_exc(limit=1)
@upload.error
async def upload_error(self, ctx, error):
pass
# http.