Skip to content

Commit

Permalink
- Allow/Disallow streams
Browse files Browse the repository at this point in the history
- Small code improvements
  • Loading branch information
Martmists-GH committed Nov 18, 2016
1 parent 7642db1 commit 9aa8db0
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions asynctwitch/bots.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Bot:

def __init__(self, *, oauth=None, user=None, channel="twitch",
prefix="!", admins=None, config=None, cache=100,
client_id=None):
client_id=None, allow_streams=False):

if config:
self.load(config)
Expand Down Expand Up @@ -122,6 +122,7 @@ def __init__(self, *, oauth=None, user=None, channel="twitch",
self.song = Song()
self.is_mod = False
self.is_playing = False
self.allow_streams = allow_streams

self.message_count = 1 # Just in case some get sent almost simultaneously even though they shouldn't

Expand Down Expand Up @@ -160,7 +161,6 @@ def load(self, path):
self.prefix = config.get("Settings", "prefix", fallback="!")
self.client_id = config.get("Settings", "client_id", fallback=None)


def override(self, coro):
""" Allows for overriding certain functions """
if not 'event' in coro.__name__:
Expand Down Expand Up @@ -715,13 +715,22 @@ def play_file(self, file):
])

j = json.loads(j.decode().strip())
t = math.ceil( float( j["format"]["duration"] ) ) + 2
if self.song == Song():
self.song.setattrs({
'title': ' '.join(file.split('.')[:-1]),
'duration': t
})
asyncio.ensure_future(self.song._play(file))
try:
t = math.ceil( float( j["format"]["duration"] ) ) + 2
except:
# Song is a stream
if not self.allow_streams:
return
else:
# TODO: Find a way to play streams, pass for now
pass
else:
if self.song == Song():
self.song.setattrs({
'title': ' '.join(file.split('.')[:-1]),
'duration': t
})
asyncio.ensure_future(self.song._play(file))

@asyncio.coroutine
def play_ytdl(self, query, *, filename="song.flac", options={}, play=True):
Expand All @@ -742,10 +751,7 @@ def play_ytdl(self, query, *, filename="song.flac", options={}, play=True):
}
args.update(options)
ytdl = youtube_dl.YoutubeDL(args)
if play:
func = functools.partial(ytdl.extract_info, query)
else:
func = functools.partial(ytdl.extract_info, query, download=False)
func = functools.partial(ytdl.extract_info, query, download=play)
info = yield from self.loop.run_in_executor(None, func)
try:
info = info['entries'][0]
Expand Down

0 comments on commit 9aa8db0

Please sign in to comment.