-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminer2graph.py
More file actions
68 lines (50 loc) · 2.1 KB
/
Copy pathminer2graph.py
File metadata and controls
68 lines (50 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from flask import Flask, jsonify, request
from services.video_fetcher import *
from services.graph_builder import *
app = Flask(__name__)
@app.route('/api/videos', methods=['GET'])
def get_videos():
video_data = fetch_videos()
if video_data is not None:
return jsonify(video_data)
else:
return jsonify({"error": "Failed to fetch video data"}), 500
@app.route('/api/analyze', methods=['GET'])
def analyze_videos():
video_data = fetch_videos()
G = build_graph(video_data)
if G.number_of_nodes() == 0:
return jsonify({"error": "No video data available or graph construction failed."}), 400
degrees, community_data = analyze_and_visualize_graph(G)
communities_json = []
for name, community in community_data.items():
communities_json.append({
"name": name,
"members": list(community)
})
return jsonify({
"degrees": {node: round(degree, 4) for node, degree in degrees.items()},
"communities": communities_json
})
@app.route('/api/analyze_kmeans', methods=['GET'])
def analyze_videos_kmeans():
video_data = fetch_videos() # Assuming there's a function to fetch video data
G = build_graph_2(video_data)
if G.number_of_nodes() == 0:
return jsonify({"error": "No video data available or graph construction failed."}), 400
degrees, clusters_colors = analyze_and_visualize_graph_2(G)
clusters_json = []
# Generate JSON data for each cluster by extracting nodes belonging to each cluster and including the assigned color
for cluster_label, color in clusters_colors.items():
cluster_members = [node for node in G.nodes if G.nodes[node]['cluster'] == cluster_label]
clusters_json.append({
"name": f"Cluster {cluster_label}",
"color": color, # Include the color in the JSON response
"members": cluster_members
})
return jsonify({
"degrees": {node: round(degree, 4) for node, degree in degrees.items()},
"clusters": clusters_json
})
if __name__ == '__main__':
app.run(debug=True)