YouTube_captions_search_engine/website/search.py

38 lines
834 B
Python
Executable File

#!/usr/bin/python3
import sys, time, fcntl, os
clientId = sys.argv[1]
message = sys.argv[2]
clientFilePath = f'users/{clientId}.txt'
def write(s):
f = open(clientFilePath, 'w+')
try:
fcntl.flock(f, fcntl.LOCK_EX)
# If the output file is empty, then it means that `websocket.php` read it. Anyway we don't wait it and we append what we want to output.
read = f.read()
f.write(f"{read}\n{s}")
except Exception as e:
sys.exit(e)
f.close()
for i in range(10):
write(f'{i}: {message}')
time.sleep(2)
f = open(clientFilePath, 'r')
while True:
try:
fcntl.flock(f, fcntl.LOCK_EX)
if f.read() == '':
os.remove(clientFilePath)
break
else:
time.sleep(1)
except Exception as e:
sys.exit(e)
f.close()