-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
176 lines (149 loc) · 6.03 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
from replit import db
import time
import keep_alive
import random
import requests
apikey = os.environ["APIKEY"]
city = "new york city"
city = city.replace(" ", "+")
url = "http://api.openweathermap.org/data/2.5/weather?appid=baede7da17f762fbffef4981ca45dad1&q="+city
data = requests.get(url).json()
print(round(1.8*(data["main"]["temp"]-273)+32))
os.system(
'pip install scratch2py & pip install --force-reinstall websocket-client')
from scratch2py import Scratch2Py
username = os.environ['username']
password = os.environ['password']
while True:
try:
s2py = Scratch2Py(username, password)
except:
print("Login failed, trying again.")
continue
break
print("Connected to user")
cloudproject = s2py.scratchConnect('658854833')
print("Connected to project and cloud variables")
def readVar(name):
return cloudproject.readCloudVar(name)
def setVar(name, val):
return cloudproject.setCloudVar(name, val)
def resetDatabase():
for i in db.keys():
try:
print(f"Deleting {i}")
del db[i]
except:
print(f"Couldn't delete {i}")
print("Server reset complete")
db["Ads"] = []
print("Ads are reset")
project = s2py.project('658854833')
loop = 0
keep_alive.keep_alive()
while True:
try:
print(project.getComments()[0]["author"]["username"])
loop += 1
print(f"Server has been up for {loop} loops")
#time.sleep(0)
if str(readVar("weather_get")) != "0":
location = readVar("weather_get")
city = str(s2py.decode(location))
city = city.replace(" ", "+")
url = "http://api.openweathermap.org/data/2.5/weather?appid=baede7da17f762fbffef4981ca45dad1&q="+city
try:
data = requests.get(url).json()
setVar("weather_recieve",round(1.8*(data["main"]["temp"]-273)+32))
except:
setVar("weather_recieve", "123456789")
setVar("weather_get", 0)
# WEBSITE SEARCHING
if str(readVar("search")) != "0":
print("Search found")
print(readVar("search"))
search = s2py.decode(str(readVar("search")))
print(f"Decoded search {search}")
if search.lower() in db.keys():
print("Website found")
search = search.lower()
print(db[search])
setVar("result", s2py.encode(str(db[search][1])))
try:
ad = db["Ads"][random.randint(0, len(list(db["Ads"])) - 1)]
except:
ad = "No ads are created yet."
print(f"Sending ad {ad}")
setVar("ad", s2py.encode(ad))
setVar("search", 0)
else:
print("Website not found")
setVar("result", 1)
setVar("search", 0)
else:
# WEBSITE CREATION
latestComment = project.getComments()[0]["content"]
if latestComment.startswith(
"createWebsite(") and latestComment.endswith(
")") and "," in latestComment:
latestComment = latestComment.split("(")
latestComment = latestComment[1]
latestComment = latestComment.split(")")
latestComment = latestComment[0]
latestComment = latestComment.split(",")
print("Split latest comment")
if latestComment[0] in db.keys() or len(
latestComment[1]) > 128 or len(latestComment[0]) > 128:
print(
f"Attempted to create website {latestComment[0]} with data {latestComment[1]}"
)
continue
else:
db[latestComment[0].lower()] = [
project.getComments()[0]["author"]["username"],
latestComment[1]
]
print(
f'Created website {latestComment[0]} with data {latestComment[1]} made by {project.getComments()[0]["author"]["username"]}'
)
# WEBSITE EDITING
latestComment = project.getComments()[0]["content"]
author = project.getComments()[0]["author"]["username"]
if latestComment.startswith(
"editWebsite("
) and "," in latestComment and latestComment.endswith(")"):
latestComment = latestComment.split("(")
latestComment = latestComment[1]
latestComment = latestComment.split(")")
latestComment = latestComment[0]
latestComment = latestComment.split(",")
if author == db[latestComment[0]][0]:
db[latestComment[0]][1] = latestComment[1]
# ADVERTISEMENT CREATION
latestComment = project.getComments()[0]["content"]
if latestComment.startswith(
"createAd(") and latestComment.endswith(")"):
print("Got advertisement")
latestComment = latestComment.split("(")
latestComment = latestComment[1]
latestComment = latestComment.split(")")
latestComment = latestComment[0]
print(latestComment)
if latestComment in list(db["Ads"]):
print("Ad already exists")
else:
db["Ads"] = list(db["Ads"]) + [latestComment]
print("Ad created")
except:
print("Loop failed. Trying startup sequence.")
while True:
try:
s2py = Scratch2Py(username, password)
cloudproject = s2py.scratchConnect('658854833')
project = s2py.project('658854833')
print("Startup sequence complete.")
break
except:
print("Login failed, trying again.") #add multi acc switching from HAL
continue