Skip to content

Commit 2a419b1

Browse files
authored
Update ChessBoard.py (DhanushNehru#401)
* Update ChessBoard.py Coordinate Labels: Displays row numbers (1-8) and column letters (A-H) around the board. Dynamic Piece Placement: Pieces (e.g., rooks and pawns) are placed using Unicode chess symbols. Square Highlighting: Allows highlighting specific squares (e.g., valid moves or selected positions). * Fixes Implemented Properly handled form POST requests Created a /cloned_sites/ directory for saved pages Fixed JavaScript to properly submit the form Handled errors gracefully
1 parent f7b5300 commit 2a419b1

File tree

3 files changed

+124
-68
lines changed

3 files changed

+124
-68
lines changed

Chess Board/ChessBoard.py

+55-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,63 @@
11
import matplotlib.pyplot as plt
22
import numpy as np
3-
from matplotlib.colors import LogNorm
3+
from matplotlib.patches import Rectangle
44

5-
dx, dy = 0.015, 0.05
6-
# numpy.arange returns evenly spaced values within a given interval
7-
x = np.arange(-4.0, 4.0, dx) # Corrected -04.0 to -4.0
8-
y = np.arange(-4.0, 4.0, dy) # Corrected -04.0 to -4.0
9-
# returns coordinate matrices from coordinate vectors
10-
X, Y = np.meshgrid(x, y)
5+
# Chess symbols for pieces (Unicode)
6+
chess_pieces = {
7+
"rook_white": "\u2656",
8+
"rook_black": "\u265C",
9+
"pawn_white": "\u2659",
10+
"pawn_black": "\u265F",
11+
}
1112

12-
extent = np.min(x), np.max(x), np.min(y), np.max(y)
13+
# Initial piece positions for demonstration
14+
initial_pieces = [
15+
(0, 0, chess_pieces["rook_black"]),
16+
(0, 7, chess_pieces["rook_black"]),
17+
(7, 0, chess_pieces["rook_white"]),
18+
(7, 7, chess_pieces["rook_white"]),
19+
(1, 0, chess_pieces["pawn_black"]),
20+
(1, 7, chess_pieces["pawn_black"]),
21+
(6, 0, chess_pieces["pawn_white"]),
22+
(6, 7, chess_pieces["pawn_white"]),
23+
]
1324

14-
Z1 = np.add.outer(range(8), range(8)) % 2
25+
# Function to draw the chessboard
26+
def draw_chessboard(highlight_squares=None):
27+
board_size = 8
28+
chessboard = np.add.outer(range(board_size), range(board_size)) % 2
1529

16-
plt.imshow(Z1, cmap="binary_r", interpolation="nearest", extent=extent, alpha=1)
30+
fig, ax = plt.subplots(figsize=(8, 8))
31+
ax.imshow(chessboard, cmap="cividis", interpolation="nearest")
1732

18-
plt.title("Chess Board", fontweight="bold")
33+
# Remove axes for a cleaner look
34+
ax.set_xticks([])
35+
ax.set_yticks([])
1936

20-
plt.show()
37+
# Add grid lines to separate the squares
38+
for i in range(board_size + 1):
39+
ax.axhline(i - 0.5, color="black", linewidth=1, alpha=0.7)
40+
ax.axvline(i - 0.5, color="black", linewidth=1, alpha=0.7)
41+
42+
# Highlight specific squares
43+
if highlight_squares:
44+
for (row, col) in highlight_squares:
45+
ax.add_patch(Rectangle((col - 0.5, row - 0.5), 1, 1, color="yellow", alpha=0.4))
46+
47+
# Add chess pieces
48+
for row, col, piece in initial_pieces:
49+
ax.text(col, row, piece, fontsize=28, ha="center", va="center", color="black" if (row + col) % 2 == 0 else "white")
50+
51+
# Add coordinate labels
52+
for i in range(board_size):
53+
ax.text(-0.8, i, str(8 - i), fontsize=12, va="center") # Row labels
54+
ax.text(i, 8 - 0.3, chr(65 + i), fontsize=12, ha="center") # Column labels
55+
56+
# Title
57+
ax.set_title("Chess Board", fontweight="bold", fontsize=16)
58+
59+
plt.show()
60+
61+
# Highlight squares (optional demo)
62+
highlight = [(0, 0), (7, 7), (3, 3)] # Example of squares to highlight
63+
draw_chessboard(highlight_squares=highlight)

Website Cloner/index.html

+25-33
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
11
<!DOCTYPE html>
22
<html lang="en">
33
<head>
4-
<title>PROXY SERVER</title>
5-
<meta charset="UTF-8">
6-
<meta name="viewport" content="width=device-width, initial-scale=1">
7-
<link rel="stylesheet" type="text/css" href="./util.css">
8-
<link rel="stylesheet" type="text/css" href="./main.css">
4+
<title>PROXY SERVER</title>
5+
<meta charset="UTF-8">
6+
<meta name="viewport" content="width=device-width, initial-scale=1">
7+
<link rel="stylesheet" type="text/css" href="./util.css">
8+
<link rel="stylesheet" type="text/css" href="./main.css">
99
</head>
1010
<body>
11-
<div class="limiter">
12-
<div class="container-login0">
13-
<div class="wrap-login0" style="width: 500px">
14-
<form class="login0-form validate-form" method="post" style="width: 100%">
15-
<div class="wrap-input0">
16-
<input class="input0" type="text" id="link" placeholder="ENTER LINK" required>
17-
<span class="focus-input0"></span>
18-
<span class="symbol-input0">
19-
<i class="fa fa-pencil" aria-hidden="true"></i>
20-
</span>
21-
</div>
22-
23-
<div class="container-login0-form-btn">
24-
<button class="login0-form-btn" id="proxyButton" name="submitButton" type="submit" value="" onclick="proxy()">
25-
CLONE NOW
26-
</button>
27-
</div>
28-
</form>
29-
</div>
30-
</div>
31-
</div>
32-
33-
<script>
34-
function proxy() {
35-
let link = document.getElementById("link").value;
36-
document.getElementById("proxyButton").value = link;
37-
}
38-
</script>
11+
<div class="limiter">
12+
<div class="container-login0">
13+
<div class="wrap-login0" style="width: 500px">
14+
<form class="login0-form validate-form" method="post" action="/" style="width: 100%">
15+
<div class="wrap-input0">
16+
<input class="input0" type="text" id="link" name="submitButton" placeholder="ENTER LINK" required>
17+
<span class="focus-input0"></span>
18+
<span class="symbol-input0">
19+
<i class="fa fa-pencil" aria-hidden="true"></i>
20+
</span>
21+
</div>
22+
<div class="container-login0-form-btn">
23+
<button class="login0-form-btn" type="submit">
24+
CLONE NOW
25+
</button>
26+
</div>
27+
</form>
28+
</div>
29+
</div>
30+
</div>
3931
</body>
4032
</html>

Website Cloner/server.py

+44-23
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,52 @@ class RequestHandler(http.server.SimpleHTTPRequestHandler):
88
def do_GET(self):
99
if self.path == "/":
1010
self.path = "index.html"
11-
return http.server.SimpleHTTPRequestHandler.do_GET(self)
11+
try:
12+
return http.server.SimpleHTTPRequestHandler.do_GET(self)
13+
except FileNotFoundError:
14+
self.send_error(404, "File not found")
15+
1216
def do_POST(self):
13-
form = cgi.FieldStorage(
14-
fp=self.rfile,
15-
headers=self.headers,
16-
environ={
17-
'REQUEST_METHOD': 'POST',
18-
'CONTENT_TYPE': self.headers['Content-Type'],
19-
})
20-
url = form.getvalue('submitButton')
21-
urlN = 'https://' + str(url) +'/'
22-
isdir = os.path.isdir(url)
23-
if not isdir:
24-
kwargs = {'project_name': url}
25-
save_webpage(
26-
url=urlN,
27-
project_folder='./',
28-
**kwargs
29-
)
30-
path = url + "/" + url + "/index.html"
17+
content_length = int(self.headers.get('Content-Length', 0))
18+
post_data = self.rfile.read(content_length).decode("utf-8")
19+
20+
# Parse the URL from the form submission
21+
form = cgi.parse_qs(post_data)
22+
url = form.get("submitButton", [""])[0]
23+
24+
if not url:
25+
self.send_error(400, "Bad Request: URL is missing")
26+
return
27+
28+
urlN = "https://" + url.strip("/")
29+
project_folder = os.path.join('./cloned_sites', url.replace("https://", "").replace("http://", ""))
30+
31+
if not os.path.isdir(project_folder):
32+
os.makedirs(project_folder, exist_ok=True)
33+
try:
34+
save_webpage(
35+
url=urlN,
36+
project_folder=project_folder,
37+
project_name=url.replace("https://", "").replace("http://", "")
38+
)
39+
except Exception as e:
40+
self.send_error(500, f"Error cloning website: {e}")
41+
return
42+
43+
path = os.path.join(project_folder, "index.html")
44+
if not os.path.isfile(path):
45+
self.send_error(404, "Cloned page not found")
46+
return
47+
3148
self.send_response(301)
32-
self.send_header('Location', path)
49+
self.send_header("Location", "/" + path)
3350
self.end_headers()
34-
return http.server.SimpleHTTPRequestHandler.do_GET(self)
51+
3552

3653
PORT = 7000
37-
s = socketserver.TCPServer(("127.0.0.1", PORT), RequestHandler)
38-
s.serve_forever()
54+
os.chdir(os.path.dirname(os.path.abspath(__file__)))
55+
56+
with socketserver.TCPServer(("127.0.0.1", PORT), RequestHandler) as s:
57+
s.allow_reuse_address = True
58+
print(f"Server running on http://127.0.0.1:{PORT}")
59+
s.serve_forever()

0 commit comments

Comments
 (0)