-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathv3.py
116 lines (80 loc) · 2.6 KB
/
v3.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import shout, io, configparser, os, random, praw, metovlogs
from PIL import Image
from pysstv import color
from pydub import AudioSegment
from itertools import chain
from array import array
from urllib.request import urlopen
config = configparser.ConfigParser()
log = metovlogs.get_log("sstv and music icecaster")
config.read('config.ini')
icecast = shout.Shout()
musicCount = 0
files = os.listdir("./music/")
reddit = praw.Reddit(
client_id = config["praw"]["client_id"],
client_secret = config["praw"]["client_secret"],
user_agent = "sstv and music icecaster/3.0.0"
)
posts = []
log.debug("Started fetching subreddit posts..")
posts += list(reddit.subreddit("schizopostingmemes").hot(limit=500))
posts += list(reddit.subreddit("offensivejokes").hot(limit=500))
log.debug("Finished fetching subreddit!!")
def randomPost():
post = random.choice(posts).url
while "i.redd.it" not in post and ".gif" not in post:
post = random.choice(posts).url
return post
def sendToIcecast(song):
f = io.BytesIO()
song.export(f, format="ogg")
nbuf = f.read(4096)
log.debug("Started sending bytes to Icecast.")
while(True):
buf = nbuf
nbuf = f.read(4096)
icecast.send(buf)
icecast.sync()
if len(buf) == 0:
log.debug("Finished sending bytes to Icecast.")
break
f.close()
def sendSSTV():
image = Image.open(urlopen(randomPost()))
image = image.resize((320, 240), Image.ANTIALIAS)
sstv = color.Robot36(image, 44100, 16)
sstv.nchannels = 2
# generate wav samples
data = array("h", sstv.gen_samples())
if sstv.nchannels != 1:
data = array("h", chain.from_iterable(
zip(*([data] * sstv.nchannels))))
# load wav samples into pydub
song = AudioSegment(
data=data,
sample_width=sstv.bits // 8,
frame_rate=sstv.samples_per_sec,
channels=sstv.nchannels,
)
sendToIcecast(song)
def sendMusic(path):
seg = AudioSegment.from_file(path)
sendToIcecast(seg)
icecast.host = config["icecast"]["url"].split(":")[0]
icecast.port = int(config["icecast"]["url"].split(":")[1])
icecast.password = config["icecast"]["password"]
icecast.mount = "/yf-sstv-and-music"
icecast.name = "Yourfriend's SSTV and Music"
icecast.description = "Music, mixed with random SSTV's from time to time. They are .. somewhat rare. Images range from memes to interdimensional connections."
icecast.open()
while(True):
if musicCount >= 4:
sendSSTV()
log.info("Started playing SSTV!")
musicCount = 0
else:
musicCount+=1
file = random.choice(files)
log.info("Started playing audio " + file + ". It is count " + str(musicCount) + "!")
sendMusic("music/"+random.choice(files))