forked from makefu/mediengewitter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mediengewitter.py
executable file
·109 lines (87 loc) · 3.21 KB
/
mediengewitter.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
#!/usr/bin/env python3
#requires additionally installed
# websockets-10.4
# requests
import websockets
import asyncio
import json
from random import randrange
import requests #to check for valid images
import os
PORT = 8080
imgcache= ["/image/default.jpg","/image/default.jpg","/image/default.jpg","/image/default.jpg","/image/default.jpg"]
# A set of connected ws clients
connected = set()
async def handler(websocket):
connected.add(websocket) #add client to list
print(f"{websocket.request_headers.get('X-Forwarded-For')} just connected")
try:
#send init sequence/imagecache to new client
initdata = {
"type":"cache",
"payload":{
"action":"init",
"data":imgcache
}
}
await websocket.send(json.dumps(initdata))
async for message in websocket:
try:
indata = json.loads(message)
if indata["type"] == "chat":
#print("Received message from client: " + message)
print( f"msg:{websocket.request_headers.get('X-Forwarded-For')}: {indata['payload']['data']} ")
chatmsg = {
"type":"chat",
"payload":{
"action":"msg",
"data": "anonym:" + indata['payload']['data']
}
}
websockets.broadcast(connected, json.dumps(chatmsg))
except:
pass
except websockets.exceptions.ConnectionClosed as e:
print(f"{websocket.request_headers.get('X-Forwarded-For')} disconnected")
finally:
connected.remove(websocket)
def checkURL(url):
try:
r = requests.head(url, data ={'key':'value'})
return r.status_code == 200
except:
return False
async def broadcast_messages():
imagelist = []
with open("./list.txt") as file:
imagelist = file.read().splitlines()
urls= len(imagelist)
while True:
#check for new image list file
try:
if os.path.isfile("./update.txt"):
with open("./update.txt") as file:
imagelist.extend(file.read().splitlines())
urls= len(imagelist)
print("added new images")
os.rename("./update.txt", "./update_done.txt")
except:
pass
# get next (random) image from list
url = imagelist[ randrange(urls) ]
# check if image available
while(checkURL(url)==False):
#print("404 image skipped")
url = imagelist[randrange(urls) ]
msg = '{"type":"cache","payload":{"action":"nextImage","data":"' + url + '"}}' # your application logic goes here
# wait till it's time to update
await asyncio.sleep(7)
websockets.broadcast(connected, msg)
#update imgcache with current picture
imgcache.pop(0)
imgcache.append(url)
async def main():
async with websockets.serve(handler, "", PORT):
await broadcast_messages() # runs forever
if __name__ == "__main__":
asyncio.run(main())