libokj commited on
Commit
f06e185
·
verified ·
1 Parent(s): accd44d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -533,7 +533,7 @@ def send_email(job_info):
533
 
534
  for key, value in job_info.items():
535
  if key.endswith("time") and value:
536
- job_info[key] = datetime.fromtimestamp(value).strftime("%Y-%m-%d %H:%M:%S")
537
 
538
  server = smtplib.SMTP(email_serv, int(email_port))
539
  # server.starttls()
@@ -603,19 +603,21 @@ def check_user_running_job(email, request):
603
 
604
  def get_timezone_by_ip(ip):
605
  try:
606
- data = session.get(f'http://ip-api.com/json/{ip}').json()
607
  return data['timezone']
608
  except Exception:
609
  return 'UTC'
610
 
611
 
612
- def ts_to_str(ts, timezone_str):
613
- if isinstance(ts, str):
614
- return ts
615
- local_tz = pytz.timezone(timezone_str)
616
- dt = datetime.fromtimestamp(ts)
617
- dt = dt.replace(tzinfo=pytz.utc) # Set the datetime object to UTC
618
- localized_dt = dt.astimezone(local_tz) # Convert the datetime object to the desired timezone
 
 
619
  return localized_dt.strftime('%Y-%m-%d %H:%M:%S (%Z)')
620
 
621
 
@@ -2000,7 +2002,7 @@ QALAHAYFAQYHDPDDEPVADPYDQSFESRDLLIDEWKSLTYDEVISFVPPPLDQEEMES
2000
  'task': task,
2001
  'status': 'RUNNING',
2002
  'email': email,
2003
- 'ip': str(request.client.host),
2004
  'cookies': dict(request.cookies),
2005
  'start_time': time(),
2006
  'end_time': None,
 
533
 
534
  for key, value in job_info.items():
535
  if key.endswith("time") and value:
536
+ job_info[key] = ts_to_str(value, get_timezone_by_ip(job_info['ip']))
537
 
538
  server = smtplib.SMTP(email_serv, int(email_port))
539
  # server.starttls()
 
603
 
604
  def get_timezone_by_ip(ip):
605
  try:
606
+ data = session.get(f'https://worldtimeapi.org/api/ip/{ip}').json()
607
  return data['timezone']
608
  except Exception:
609
  return 'UTC'
610
 
611
 
612
+ def ts_to_str(timestamp, timezone):
613
+ # Create a timezone-aware datetime object from the UNIX timestamp
614
+ dt = datetime.fromtimestamp(timestamp, pytz.utc)
615
+
616
+ # Convert the timezone-aware datetime object to the target timezone
617
+ target_timezone = pytz.timezone(timezone)
618
+ localized_dt = dt.astimezone(target_timezone)
619
+
620
+ # Format the datetime object to the specified string format
621
  return localized_dt.strftime('%Y-%m-%d %H:%M:%S (%Z)')
622
 
623
 
 
2002
  'task': task,
2003
  'status': 'RUNNING',
2004
  'email': email,
2005
+ 'ip': request.headers.get('x-forwarded-for', request.client.host),
2006
  'cookies': dict(request.cookies),
2007
  'start_time': time(),
2008
  'end_time': None,