-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
131 lines (117 loc) · 3.36 KB
/
search.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
import os
from flask import Flask, request, jsonify
from googlesearch import search
# Define the Flask app
app = Flask(__name__)
# Function to search Google and return results
def check_google_search(query):
try:
result = list(search(query, num_results=1))
return result
except Exception as e:
print(f"An error occurred during the search: {e}")
return []
# Function to run Ruby script
def run_ruby_script():
ruby_code = '''
require 'curses'
class SnakeGame
def initialize
Curses.init_screen
Curses.curs_set(0) # Hide the cursor
Curses.timeout = 100 # Set delay for getch to 100ms
@window = Curses.stdscr
@snake = [[4, 4], [4, 3], [4, 2]]
@direction = 'right'
@new_direction = 'right'
place_food
play_game
end
def place_food
begin
@food = [rand(Curses.lines), rand(Curses.cols)]
end until @snake.include?(@food)
end
def play_game
loop do
handle_input
move_snake
handle_collisions
render
sleep 0.1
end
end
def handle_input
case @window.getch
when 'w'
@new_direction = 'up' unless @direction == 'down'
when 's'
@new_direction = 'down' unless @direction == 'up'
when 'a'
@new_direction = 'left' unless @direction == 'right'
when 'd'
@new_direction = 'right' unless @direction == 'left'
end
@direction = @new_direction
end
def move_snake
head = @snake.first.dup
case @direction
when 'up' then head[0] -= 1
when 'down' then head[0] += 1
when 'left' then head[1] -= 1
when 'right' then head[1] += 1
end
@snake.unshift(head)
@snake.pop unless head == @food
end
def handle_collisions
if @snake.first[0] < 0 || @snake.first[0] >= Curses.lines || @snake.first[1] < 0 || @snake.first[1] >= Curses.cols
end_game
end
if @snake[1..].include?(@snake.first)
end_game
end
if @snake.first == @food
place_food
end
end
def render
@window.clear
@window.setpos(@food[0], @food[1])
@window.addch('F')
@snake.each do |segment|
@window.setpos(segment[0], segment[1])
@window.addch('O')
end
@window.refresh
end
def end_game
@window.setpos(Curses.lines / 2.0, Curses.cols / 2.0 - 5)
@window.addstr("Game Over!")
@window.refresh
sleep 2
Curses.close_screen
exit
end
end
SnakeGame.new
'''
with open("snake_game.rb", "w") as file:
file.write(ruby_code)
os.system("ruby snake_game.rb")
@app.route('/search', methods=['POST'])
def search_query():
data = request.get_json()
query = data.get('query', '')
if query:
results = check_google_search(query)
if not results:
run_ruby_script()
return jsonify({'message': 'No results found. Ruby script executed.'})
else:
return jsonify({'message': f'Results found: {results}'})
else:
return jsonify({'message': 'No query provided.'}), 400
if __name__ == '__main__':
app.run(debug=True)