GamerC0der commited on
Commit
bdd511a
·
verified ·
1 Parent(s): de59979

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -1
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import requests, time, json
2
  from bs4 import BeautifulSoup
3
  from flask import Flask, request, jsonify
4
  from flask_cors import CORS
@@ -25,6 +25,60 @@ def scrape_startpage(query, n=10):
25
  print(f"Error: {e}")
26
  return []
27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28
  @app.route('/search', methods=['GET'])
29
  def search():
30
  query = request.args.get('q')
@@ -35,6 +89,18 @@ def search():
35
  results = scrape_startpage(query, n)
36
  return jsonify({'query': query, 'results': results})
37
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  @app.route('/', methods=['GET'])
39
  def health():
40
  return jsonify({'status': 'running', 'message': 'GridLock search API'})
 
1
+ import requests, time, json, os
2
  from bs4 import BeautifulSoup
3
  from flask import Flask, request, jsonify
4
  from flask_cors import CORS
 
25
  print(f"Error: {e}")
26
  return []
27
 
28
+ def get_weather_from_ip(ip_address):
29
+ try:
30
+ ip_response = requests.get(f'https://ipinfo.io/{ip_address}/json')
31
+ ip_response.raise_for_status()
32
+ ip_data = ip_response.json()
33
+
34
+ if ip_data.get('error'):
35
+ return {'error': 'Invalid IP address or unable to locate'}
36
+
37
+ loc = ip_data.get('loc', '')
38
+ if not loc:
39
+ return {'error': 'Location data not available'}
40
+
41
+ lat, lon = loc.split(',')
42
+ city = ip_data.get('city', 'Unknown')
43
+ country = ip_data.get('country', '')
44
+
45
+ if country != 'US':
46
+ return {'error': 'Weather service only available for US locations'}
47
+
48
+ points_response = requests.get(f'https://api.weather.gov/points/{lat},{lon}')
49
+ points_response.raise_for_status()
50
+ points_data = points_response.json()
51
+
52
+ forecast_url = points_data['properties']['forecast']
53
+ forecast_response = requests.get(forecast_url)
54
+ forecast_response.raise_for_status()
55
+ forecast_data = forecast_response.json()
56
+
57
+ current_period = forecast_data['properties']['periods'][0]
58
+
59
+ return {
60
+ 'ip': ip_address,
61
+ 'location': {
62
+ 'city': city,
63
+ 'country': ip_data.get('country'),
64
+ 'latitude': lat,
65
+ 'longitude': lon
66
+ },
67
+ 'weather': {
68
+ 'temperature': current_period['temperature'],
69
+ 'feels_like': None,
70
+ 'humidity': None,
71
+ 'description': current_period['detailedForecast'],
72
+ 'icon': None
73
+ }
74
+ }
75
+ except requests.exceptions.RequestException as e:
76
+ if '429' in str(e):
77
+ return {'error': 'Rate limit exceeded. Please try again later.'}
78
+ return {'error': f'API request failed: {str(e)}'}
79
+ except Exception as e:
80
+ return {'error': f'Unexpected error: {str(e)}'}
81
+
82
  @app.route('/search', methods=['GET'])
83
  def search():
84
  query = request.args.get('q')
 
89
  results = scrape_startpage(query, n)
90
  return jsonify({'query': query, 'results': results})
91
 
92
+ @app.route('/weather', methods=['GET'])
93
+ def weather():
94
+ ip = request.args.get('ip')
95
+ if not ip:
96
+ return jsonify({'error': 'Missing IP parameter "ip"'}), 400
97
+
98
+ weather_data = get_weather_from_ip(ip)
99
+ if 'error' in weather_data:
100
+ return jsonify(weather_data), 400
101
+
102
+ return jsonify(weather_data)
103
+
104
  @app.route('/', methods=['GET'])
105
  def health():
106
  return jsonify({'status': 'running', 'message': 'GridLock search API'})