tecuts commited on
Commit
f7540d5
·
verified ·
1 Parent(s): 440b041

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -9,7 +9,8 @@ import asyncio
9
  import cloudscraper
10
  from pydantic import BaseModel
11
  from urllib.parse import urlparse, parse_qs
12
- import redis
 
13
 
14
  app = Flask(__name__)
15
  ytmusic = YTMusic()
@@ -208,23 +209,41 @@ async def get_track_download_url(track_id: str, quality: str) -> str:
208
 
209
 
210
 
211
- # Initialize Redis client
212
- redis_client = redis.Redis(host='localhost', port=6379, db=0)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
  def get_daily_requests(ip):
215
  """Get the number of requests made today by this IP"""
216
- today = datetime.now().strftime('%Y-%m-%d')
217
- key = f'daily_requests:{ip}:{today}'
218
- return int(redis_client.get(key) or 0)
 
 
 
 
219
 
220
  def increment_daily_requests(ip):
221
  """Increment the request counter for this IP"""
222
- today = datetime.now().strftime('%Y-%m-%d')
223
- key = f'daily_requests:{ip}:{today}'
224
- # Set expiration to end of day (seconds until midnight)
225
- seconds_until_midnight = 86400 - (datetime.now().hour * 3600 + datetime.now().minute * 60 + datetime.now().second)
226
- redis_client.incr(key)
227
- redis_client.expire(key, seconds_until_midnight)
228
 
229
  @app.route('/track_dl', methods=['POST'])
230
  async def track_dl():
@@ -266,7 +285,6 @@ async def track_dl():
266
  "error": "Invalid quality value provided. It should be a valid integer or FLAC."
267
  }), 400
268
 
269
-
270
 
271
 
272
 
 
9
  import cloudscraper
10
  from pydantic import BaseModel
11
  from urllib.parse import urlparse, parse_qs
12
+ from collections import defaultdict
13
+ import threading
14
 
15
  app = Flask(__name__)
16
  ytmusic = YTMusic()
 
209
 
210
 
211
 
212
+
213
+
214
+ # In-memory storage for request counting
215
+ # Structure: {ip: [(timestamp, count), ...]}
216
+ request_counts = defaultdict(list)
217
+ request_lock = threading.Lock()
218
+
219
+ def cleanup_old_records():
220
+ """Remove records older than 24 hours"""
221
+ today = datetime.now()
222
+ with request_lock:
223
+ for ip in list(request_counts.keys()):
224
+ request_counts[ip] = [
225
+ (timestamp, count)
226
+ for timestamp, count in request_counts[ip]
227
+ if (today - timestamp).days < 1
228
+ ]
229
+ if not request_counts[ip]:
230
+ del request_counts[ip]
231
 
232
  def get_daily_requests(ip):
233
  """Get the number of requests made today by this IP"""
234
+ cleanup_old_records()
235
+ today = datetime.now()
236
+ with request_lock:
237
+ return sum(
238
+ count for timestamp, count in request_counts[ip]
239
+ if (today - timestamp).days < 1
240
+ )
241
 
242
  def increment_daily_requests(ip):
243
  """Increment the request counter for this IP"""
244
+ now = datetime.now()
245
+ with request_lock:
246
+ request_counts[ip].append((now, 1))
 
 
 
247
 
248
  @app.route('/track_dl', methods=['POST'])
249
  async def track_dl():
 
285
  "error": "Invalid quality value provided. It should be a valid integer or FLAC."
286
  }), 400
287
 
 
288
 
289
 
290