Pedro Bento commited on
Commit
40dc7ac
·
1 Parent(s): e5bb0b1

fixed Virus total dependencies

Browse files
Files changed (1) hide show
  1. tdagent/tools/virus_total.py +38 -8
tdagent/tools/virus_total.py CHANGED
@@ -2,16 +2,24 @@ import gradio as gr
2
  import vt
3
  import os
4
  from datetime import datetime
 
 
 
5
 
6
  # Get API key from environment variable
7
  API_KEY = os.getenv('VT_API_KEY')
8
 
 
 
9
 
10
- def get_url_info(url):
11
- try:
12
- # Initialize the client
13
- client = vt.Client(API_KEY)
14
 
 
 
 
 
 
 
 
15
  # URL ID is created by computing the base64-encoded SHA-256 of the URL
16
  url_id = vt.url_id(url)
17
 
@@ -20,12 +28,20 @@ def get_url_info(url):
20
 
21
  # Format the results
22
  last_analysis_stats = url_analysis.last_analysis_stats
23
- last_analysis_date = datetime.utcfromtimestamp(url_analysis.last_analysis_date).strftime(
24
- '%Y-%m-%d %H:%M:%S UTC')
 
 
 
 
 
 
 
 
25
 
26
  result = f"""
27
  URL: {url}
28
- Last Analysis Date: {last_analysis_date}
29
 
30
  Analysis Statistics:
31
  - Harmless: {last_analysis_stats['harmless']}
@@ -36,15 +52,29 @@ Analysis Statistics:
36
 
37
  Reputation Score: {url_analysis.reputation}
38
  Times Submitted: {url_analysis.times_submitted}
 
 
39
  """
40
 
41
- client.close()
42
  return result
43
 
44
  except Exception as e:
45
  return f"Error: {str(e)}"
46
 
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  gr_virus_total = gr.Interface(
49
  fn=get_url_info,
50
  inputs=["text"],
 
2
  import vt
3
  import os
4
  from datetime import datetime
5
+ from functools import lru_cache
6
+ from typing import Optional
7
+ import time
8
 
9
  # Get API key from environment variable
10
  API_KEY = os.getenv('VT_API_KEY')
11
 
12
+ # Initialize the VT client globally
13
+ client = vt.Client(API_KEY)
14
 
 
 
 
 
15
 
16
+ @lru_cache(maxsize=100)
17
+ def get_url_info_cached(url: str, timestamp: Optional[int] = None) -> str:
18
+ """
19
+ Cached version of URL info retrieval.
20
+ timestamp parameter is used to force cache invalidation when needed
21
+ """
22
+ try:
23
  # URL ID is created by computing the base64-encoded SHA-256 of the URL
24
  url_id = vt.url_id(url)
25
 
 
28
 
29
  # Format the results
30
  last_analysis_stats = url_analysis.last_analysis_stats
31
+
32
+ # Fix: Convert the datetime attribute properly
33
+ last_analysis_date = url_analysis.last_analysis_date
34
+ if last_analysis_date:
35
+ # Convert to datetime if it's a timestamp
36
+ if isinstance(last_analysis_date, (int, float)):
37
+ last_analysis_date = datetime.utcfromtimestamp(last_analysis_date)
38
+ date_str = last_analysis_date.strftime('%Y-%m-%d %H:%M:%S UTC')
39
+ else:
40
+ date_str = "Not available"
41
 
42
  result = f"""
43
  URL: {url}
44
+ Last Analysis Date: {date_str}
45
 
46
  Analysis Statistics:
47
  - Harmless: {last_analysis_stats['harmless']}
 
52
 
53
  Reputation Score: {url_analysis.reputation}
54
  Times Submitted: {url_analysis.times_submitted}
55
+
56
+ Cache Status: Hit
57
  """
58
 
 
59
  return result
60
 
61
  except Exception as e:
62
  return f"Error: {str(e)}"
63
 
64
 
65
+ def get_url_info(url: str) -> str:
66
+ """
67
+ Wrapper function to handle the cached URL info retrieval
68
+ """
69
+ # Clean the URL to ensure consistent caching
70
+ url = url.strip().lower()
71
+
72
+ # Use current timestamp rounded to nearest hour to maintain cache for an hour
73
+ timestamp = int(time.time()) // 3600
74
+
75
+ return get_url_info_cached(url, timestamp)
76
+
77
+
78
  gr_virus_total = gr.Interface(
79
  fn=get_url_info,
80
  inputs=["text"],