Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,163 +1,146 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from internetarchive import search_items, get_item
|
| 3 |
-
import requests
|
| 4 |
-
import
|
| 5 |
-
import subprocess
|
| 6 |
-
import json
|
| 7 |
-
import re
|
| 8 |
import networkx as nx
|
| 9 |
from pyvis.network import Network
|
| 10 |
-
from bs4 import BeautifulSoup
|
| 11 |
|
| 12 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
def scan_url_vt(url, api_key):
|
| 14 |
headers = {"x-apikey": api_key}
|
| 15 |
-
resp = requests.post(
|
| 16 |
-
"https://www.virustotal.com/api/v3/urls", headers=headers, data={"url": url}
|
| 17 |
-
)
|
| 18 |
resp.raise_for_status()
|
| 19 |
analysis_id = resp.json()["data"]["id"]
|
| 20 |
while True:
|
| 21 |
time.sleep(5)
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
attr = status_resp.json()["data"]["attributes"]
|
| 27 |
-
if attr.get("status") == "completed":
|
| 28 |
-
return attr.get("stats", {}).get("malicious", 0) == 0
|
| 29 |
|
| 30 |
-
# --- FFprobe ---
|
| 31 |
-
def extract_ffprobe_metadata(
|
| 32 |
-
|
| 33 |
-
"ffprobe",
|
| 34 |
-
|
| 35 |
-
url_or_path
|
| 36 |
-
]
|
| 37 |
-
out = subprocess.check_output(cmd)
|
| 38 |
return json.loads(out)
|
| 39 |
|
| 40 |
-
# --- Fetch page metadata + favicon ---
|
| 41 |
def fetch_page_metadata(url):
|
| 42 |
try:
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
for
|
| 48 |
-
|
| 49 |
-
if
|
| 50 |
-
meta[
|
| 51 |
-
# favicon
|
| 52 |
-
icon =
|
| 53 |
-
meta["favicon"] =
|
| 54 |
return meta
|
| 55 |
except Exception as e:
|
| 56 |
-
return {"url":
|
| 57 |
|
| 58 |
-
# --- IA search
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
if any(ns in title for ns in NEWS_STATIONS):
|
| 69 |
continue
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
if scan_enabled and api_key:
|
| 75 |
try:
|
| 76 |
-
|
| 77 |
-
continue
|
| 78 |
except:
|
| 79 |
continue
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
# ---
|
| 84 |
-
def
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
links.extend(sub_links)
|
| 97 |
-
nodes.extend(sub_nodes)
|
| 98 |
-
# link from origin to IA later
|
| 99 |
-
links.append((url, 'internet_archive'))
|
| 100 |
-
return nodes, links
|
| 101 |
|
| 102 |
-
# ---
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
for url, meta in nodes + [('internet_archive', {'title':'Internet Archive'})]:
|
| 106 |
-
label = meta.get('title') or url
|
| 107 |
-
favicon = meta.get('favicon')
|
| 108 |
-
net.add_node(url, label=label, title=json.dumps(meta), shape='image' if favicon else 'dot',
|
| 109 |
-
image=favicon if favicon else None)
|
| 110 |
-
for src, dst in links:
|
| 111 |
-
net.add_edge(src, dst)
|
| 112 |
-
net.force_atlas_2based()
|
| 113 |
-
return net.generate_html()
|
| 114 |
-
|
| 115 |
-
# --- Gradio UI ---
|
| 116 |
-
with gr.Blocks() as demo:
|
| 117 |
-
gr.Markdown("# IA Drone‑Strike Chain Explorer")
|
| 118 |
with gr.Row():
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
scan_toggle
|
| 122 |
-
|
| 123 |
-
run_btn
|
| 124 |
|
| 125 |
-
url_dd
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
graph_html
|
| 130 |
-
origin_meta
|
| 131 |
|
| 132 |
-
def
|
| 133 |
-
urls =
|
| 134 |
return gr.update(choices=urls, value=urls[0] if urls else None)
|
| 135 |
|
| 136 |
-
def
|
| 137 |
-
if not
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
ident = parts[4]
|
| 141 |
item = get_item(ident)
|
| 142 |
-
raw = {
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
|
| 158 |
-
run_btn.click(
|
| 159 |
-
url_dd.change(
|
| 160 |
-
|
| 161 |
|
| 162 |
-
|
| 163 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from internetarchive import search_items, get_item
|
| 3 |
+
import requests, time, subprocess, json, re, tempfile, os
|
| 4 |
+
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
| 5 |
import networkx as nx
|
| 6 |
from pyvis.network import Network
|
|
|
|
| 7 |
|
| 8 |
+
# --- SETTINGS ---
|
| 9 |
+
NEWS_FILTER = [r"\bcnn\b", r"\bfox\b", r"\bbbc\b", r"\bmsnbc\b", r"\breuters\b"]
|
| 10 |
+
THEME = "gradio/soft" # bring back the default soft theme
|
| 11 |
+
|
| 12 |
+
# --- VirusTotal scan (unchanged) ---
|
| 13 |
def scan_url_vt(url, api_key):
|
| 14 |
headers = {"x-apikey": api_key}
|
| 15 |
+
resp = requests.post("https://www.virustotal.com/api/v3/urls", headers=headers, data={"url": url})
|
|
|
|
|
|
|
| 16 |
resp.raise_for_status()
|
| 17 |
analysis_id = resp.json()["data"]["id"]
|
| 18 |
while True:
|
| 19 |
time.sleep(5)
|
| 20 |
+
st = requests.get(f"https://www.virustotal.com/api/v3/analyses/{analysis_id}", headers=headers)
|
| 21 |
+
attr = st.json()["data"]["attributes"]
|
| 22 |
+
if attr.get("status")=="completed":
|
| 23 |
+
return attr["stats"].get("malicious",0)==0
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# --- FFprobe metadata (unchanged) ---
|
| 26 |
+
def extract_ffprobe_metadata(path):
|
| 27 |
+
out = subprocess.check_output([
|
| 28 |
+
"ffprobe","-v","error","-print_format","json","-show_format","-show_streams", path
|
| 29 |
+
])
|
|
|
|
|
|
|
|
|
|
| 30 |
return json.loads(out)
|
| 31 |
|
| 32 |
+
# --- Fetch page metadata + favicon URL ---
|
| 33 |
def fetch_page_metadata(url):
|
| 34 |
try:
|
| 35 |
+
r = requests.get(url, timeout=5); r.raise_for_status()
|
| 36 |
+
bs = BeautifulSoup(r.text,"html.parser")
|
| 37 |
+
meta = {"url":url, "title": bs.title.string if bs.title else ""}
|
| 38 |
+
# og: and twitter:
|
| 39 |
+
for m in bs.find_all("meta"):
|
| 40 |
+
p = m.get("property") or m.get("name")
|
| 41 |
+
if p and p.startswith(("og:","twitter:")):
|
| 42 |
+
meta[p] = m.get("content")
|
| 43 |
+
# find favicon
|
| 44 |
+
icon = bs.find("link", rel=lambda v:v and "icon" in v.lower())
|
| 45 |
+
meta["favicon"] = icon["href"] if icon else ""
|
| 46 |
return meta
|
| 47 |
except Exception as e:
|
| 48 |
+
return {"url":url, "error":str(e), "favicon":""}
|
| 49 |
|
| 50 |
+
# --- Core IA search + filter ---
|
| 51 |
+
def fetch_clean_videos(keywords, api_key, scan_enabled):
|
| 52 |
+
# build query
|
| 53 |
+
q = " OR ".join(kw.strip().replace(" ","+") for kw in keywords.split(","))
|
| 54 |
+
items = list(search_items(f"mediatype:(movies) AND ({q})"))[:50]
|
| 55 |
+
clean = []
|
| 56 |
+
for it in items:
|
| 57 |
+
title = it.get("title","").lower()
|
| 58 |
+
# filter out news
|
| 59 |
+
if any(re.search(p, title) for p in NEWS_FILTER):
|
|
|
|
| 60 |
continue
|
| 61 |
+
# find video files
|
| 62 |
+
for f in get_item(it["identifier"]).files:
|
| 63 |
+
fmt = f.get("format","").lower()
|
| 64 |
+
if fmt.startswith(("mpeg","mp4","avi","mov","webm","m4v")):
|
| 65 |
+
url = f"https://archive.org/download/{it['identifier']}/{f['name']}"
|
| 66 |
if scan_enabled and api_key:
|
| 67 |
try:
|
| 68 |
+
ok = scan_url_vt(url, api_key)
|
|
|
|
| 69 |
except:
|
| 70 |
continue
|
| 71 |
+
else:
|
| 72 |
+
ok = True
|
| 73 |
+
if ok:
|
| 74 |
+
clean.append(url)
|
| 75 |
+
return clean
|
| 76 |
|
| 77 |
+
# --- Build a PyVis graph and return its HTML path ---
|
| 78 |
+
def build_graph(chain):
|
| 79 |
+
G = Network(height="300px", width="100%", directed=True)
|
| 80 |
+
for node in chain:
|
| 81 |
+
label = node.get("metadata",{}).get("title","origin")
|
| 82 |
+
icon = node.get("metadata",{}).get("favicon","")
|
| 83 |
+
G.add_node(node["url"], label="", shape="image", image=icon or None, title=label)
|
| 84 |
+
# link them in order
|
| 85 |
+
for i in range(len(chain)-1):
|
| 86 |
+
G.add_edge(chain[i]["url"], chain[i+1]["url"])
|
| 87 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 88 |
+
G.show(tmp.name)
|
| 89 |
+
return tmp.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
+
# --- UI ---
|
| 92 |
+
with gr.Blocks(theme=THEME) as demo:
|
| 93 |
+
gr.Markdown("## 📼 Raw-Footage Chain Explorer")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
with gr.Row():
|
| 95 |
+
kw = gr.Textbox("Keywords (comma-sep)", value="drone strike, military uav")
|
| 96 |
+
vt = gr.Textbox("VT API Key", type="password")
|
| 97 |
+
scan_toggle = gr.Checkbox("Enable VT scan", True)
|
| 98 |
+
ff_toggle = gr.Checkbox("Enable FFprobe", False)
|
| 99 |
+
run_btn = gr.Button("Search & Scan")
|
| 100 |
|
| 101 |
+
url_dd = gr.Dropdown("Clean Video URLs", choices=[])
|
| 102 |
+
vid_player = gr.Video()
|
| 103 |
+
ia_json = gr.JSON()
|
| 104 |
+
ff_json = gr.JSON()
|
| 105 |
+
graph_html = gr.HTML()
|
| 106 |
+
origin_meta = gr.JSON()
|
| 107 |
|
| 108 |
+
def search_and_populate(k, api, s):
|
| 109 |
+
urls = fetch_clean_videos(k, api, s)
|
| 110 |
return gr.update(choices=urls, value=urls[0] if urls else None)
|
| 111 |
|
| 112 |
+
def update_all(sel, ff_on, api_key):
|
| 113 |
+
if not sel:
|
| 114 |
+
return None, {}, {}, "", {}
|
| 115 |
+
# 1) IA metadata + files
|
| 116 |
+
parts = sel.split("/"); ident = parts[4]
|
| 117 |
item = get_item(ident)
|
| 118 |
+
raw = {"metadata":item.metadata, "files": [
|
| 119 |
+
{"name":f["name"], "format":f["format"], "size":f.get("size")}
|
| 120 |
+
for f in item.files
|
| 121 |
+
]}
|
| 122 |
+
# 2) FFprobe
|
| 123 |
+
ffm = extract_ffprobe_metadata(sel) if ff_on else {}
|
| 124 |
+
# 3) trace origins
|
| 125 |
+
desc = raw["metadata"].get("description","")
|
| 126 |
+
urls = re.findall(r"https?://[^\s\"']+", desc)
|
| 127 |
+
chain = []
|
| 128 |
+
for u in urls:
|
| 129 |
+
m = fetch_page_metadata(u)
|
| 130 |
+
chain.append({"url":u, "metadata":m})
|
| 131 |
+
# finally add IA itself as last hop
|
| 132 |
+
chain.append({"url":sel, "metadata": {"title": raw["metadata"].get("title"), "favicon": ""}})
|
| 133 |
+
# 4) graph
|
| 134 |
+
gfile = build_graph(chain)
|
| 135 |
+
# 5) default show first origin metadata
|
| 136 |
+
om = chain[0]["metadata"] if chain else {}
|
| 137 |
+
# embed graph HTML
|
| 138 |
+
graph_data = open(gfile,"r",encoding="utf8").read()
|
| 139 |
+
os.unlink(gfile)
|
| 140 |
+
return sel, raw, ffm, graph_data, om
|
| 141 |
|
| 142 |
+
run_btn.click(search_and_populate, [kw, vt, scan_toggle], [url_dd])
|
| 143 |
+
url_dd.change(update_all, [url_dd, ff_toggle, vt],
|
| 144 |
+
[vid_player, ia_json, ff_json, graph_html, origin_meta])
|
| 145 |
|
| 146 |
+
demo.launch()
|
|
|