-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow.py
executable file
·84 lines (67 loc) · 2.59 KB
/
flow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""Get in the flow.
Starts Youtube video on all chromecast connected devices
and sends a wake on lan packet to a computer that is turned off.
"""
import pychromecast
import webbrowser
import requests
import os
import time
from wakeonlan import wol
__author__ = 'Mikael Kall'
__email__ = 'kall.micke@gmail.com'
"""Youtube link to play"""
YOUTUBE_URL = 'https://www.youtube.com/watch?v=dQw4w9WgXcQ'
"""Mac address for wake on lan"""
MACADDR = 'f4:b7:e2:0d:d4:c0'
class Flow:
"""All flow definitions."""
global YOUTUBE_URL
global MACADDR
def __init__(self):
"""Set instance variables from global variables."""
self.youtube_url = YOUTUBE_URL
self.youtube_id = YOUTUBE_URL.split('?')[1]
self.macaddr = MACADDR
def stream_youtube(self):
"""Stream youtube to every chromecast connected to the network."""
for chromecast_info in pychromecast.discover_chromecasts():
(address, port, uuid, tp, desc) = chromecast_info
self.__puts('info', "Chromecast [%s] detected" % (address))
url = "http://%s:8008/apps/YouTube" % (address)
self.__puts('success', "Streams %s" % (url))
head = {'Content-type': 'application/json', 'Accept': 'text/plain'}
requests.post(url, self.youtube_id, headers=head)
def browse_youtube(self):
"""Open youtube in webbrowser."""
self.__puts('success', "Browsing youtube: %s" % (self.youtube_url))
controller = webbrowser.get()
controller.open(self.youtube_url)
def stream_terminal(self):
"""Stream on terminal."""
command = 'curl -s -L https://raw.githubusercontent.com/keroserene/rickrollrc/master/roll.sh | bash'
self.__puts('success', 'Stream to terminal')
time.sleep(3)
os.system(command)
def start_computer(self):
"""Start computer that it's turned off."""
self.__puts('success', "Sends wakeon lan packet [%s]" % (self.macaddr))
wol.send_magic_packet(self.macaddr)
def __puts(self, tp, msg):
"""Output messages in fancy colors."""
if tp == "info":
print("%s%s%s" % ('\033[93m', "➜ ", msg))
elif tp == "warning":
print("%s%s%s" % ('\033[93m', "➜ ", msg))
elif tp == "error":
print("%s%s%s" % ('\033[91m', "✖ ", msg))
elif tp == "success":
print("%s%s%s" % ('\033[92m', "✔ ", msg))
if __name__ == '__main__':
flow = Flow()
flow.stream_youtube()
flow.browse_youtube()
flow.start_computer()
flow.stream_terminal()