import asyncio
from pyodide.http import pyfetch
from js import document, FormData, console, fetch, window
import json
ip = 'https://vps2441966.servdiscount-customer.com'
ipPort = 'https://vps2441966.servdiscount-customer.com:5000'
async def post_tweet():
file_input = document.querySelector("#fileInput")
password = Element('password').element.value
if file_input.files.length == 0:
console.log("No file selected.")
return
form_data = FormData.new()
form_data.append("file", file_input.files.item(0)) # Revised line
headers = {'Authorization': password}
response = await pyfetch(f"{ipPort}/post_ai",
method="POST",
headers=headers,
body=form_data
)
if response.ok:
console.log("uploaded")
else:
console.error("Failed to upload file.")
print("Wrong Password LOL")
asyncio.create_task(fetch_tweets())
async def post_review(title: str):
comment_input = Element(f'comment-input-{title}')
comment = comment_input.element.value
star_input = Element(f'sterne-input-{title}')
stars = star_input.element.value
if comment:
comments_div = Element(f'comments-{title}')
tweet = comment
password = Element('password').element.value
tweetTitel = title
console.log(tweetTitel)
console.log(password)
console.log(tweet)
headers = {'Content-Type': 'application/json'}
body = json.dumps({'tweetTitle': tweetTitel,'tweet': tweet, 'password': password, 'stars': stars})
if tweet and password and tweetTitel:
response = await pyfetch(f'{ipPort}/post_aicomment', method='POST', headers=headers, body=body)
if response.ok:
asyncio.create_task(fetch_tweets())
comment_input.element.value = ''
else:
print("Wrong Password LOL")
async def fetch_tweets():
response = await pyfetch(f'{ipPort}/get_ai', method='GET')
data = await response.json()
Element('post-buttonPW').element.innerHTML = 'Refresh Posts'
Element('posts').element.innerHTML = ''
#print(data)
for d in data:
d = dict(d)
title = d['filename'].replace('.png', '')
title = title.replace('.', '_')
url = d['filename']
url = f"{ip}/{url}"
commentButtonString = ""
password = Element('password').element.value
if password:
commentButtonString = f"""
"""
SimuWarning = ""
#print(d)
if "isSimu" in d:
#print("Simu")
if d["isSimu"] == True:
SimuWarning = "Achtung, Propaganda der Obrigkeit
"
new_post = f"""
{SimuWarning}
{commentButtonString}
"""
Element('posts').element.innerHTML = new_post + Element('posts').element.innerHTML
for c in d["reviews"]:
if c:
starString = ""
#print(c[1])
for i in range(5):
if int(c[1])-1 >= i:
starString = starString + """"""
else:
starString = starString + """"""
comments_div = Element(f'comments-{title}')
comments_div.element.innerHTML += f""""""
#comment_input.element.value = ''
async def PWlock():
Element('PW').element.style.display = 'none'
Element('send').element.style.display = 'block'
asyncio.create_task(fetch_tweets())
# Bind the post_tweet function to the button
def on_post_click(event):
asyncio.create_task(post_tweet())
def on_post_clickPW(event):
asyncio.create_task(fetch_tweets())
def on_PWlock(event):
asyncio.create_task(PWlock())
Element('post-button').element.onclick = on_post_click
Element('post-buttonPW').element.onclick = on_post_clickPW
Element('PWlock').element.onclick = on_PWlock
window.post_review = post_review
# Schedule fetch_tweets to run asynchronously
asyncio.create_task(fetch_tweets())
{c[0]}