1+ import json
2+ import matplotlib .pyplot as plt
3+ from wordcloud import WordCloud
4+ #import os
5+
6+ # Color Codes for Printing
7+ GREEN = "\033 [32m"
8+ YELLOW = "\033 [33m"
9+ BLUE = "\033 [34m"
10+ RED = "\033 [31m"
11+ RESET = "\033 [0m"
12+
13+ # Load user agents from the JSON file
14+ json_file_path = "user_agents.json" # Update the file path to the correct one
15+ try :
16+ with open (json_file_path , "r" ) as json_file :
17+ user_agents = json .load (json_file )
18+ except FileNotFoundError :
19+ print (f"{ RED } [ERROR]{ RESET } JSON file '{ json_file_path } ' not found." )
20+ exit ()
21+
22+ # Count the number of different groups in the JSON file for mobile and general user agents
23+ mobile_group_counts = {}
24+ general_group_counts = {}
25+ for ua in user_agents :
26+ if ua ["Host" ] == "Mobile" :
27+ if ua ["group" ]:
28+ mobile_group_counts [ua ["group" ]] = mobile_group_counts .get (ua ["group" ], 0 ) + 1
29+ else :
30+ if ua ["group" ]:
31+ general_group_counts [ua ["group" ]] = general_group_counts .get (ua ["group" ], 0 ) + 1
32+
33+ def create_labeled_pie_chart (groups , title ):
34+ plt .figure ()
35+ labels = [f"{ group } ({ count } )" for group , count in groups .items ()]
36+ sizes = groups .values ()
37+ plt .pie (sizes , labels = labels , autopct = "%1.1f%%" , startangle = 140 )
38+ plt .title (title )
39+
40+ # Create the legend in the upper left corner with a defined box position
41+ plt .legend (labels , loc = "upper left" , bbox_to_anchor = (1 , 1 ))
42+
43+ plt .tight_layout ()
44+
45+ # User input for pie charts
46+ while True :
47+ print ("Select an option:" )
48+ print ("1. Pie chart for Mobile User Agents (Count < 10)" )
49+ print ("2. Pie chart for Mobile User Agents (10 <= Count < 500)" )
50+ print ("3. Pie chart for General User Agents (10 <= Count < 50)" )
51+ print ("4. Pie chart for General User Agents (50 <= Count < 500)" )
52+ print ("5. Pie chart for General User Agents (Count >= 500)" )
53+ print ("6. Word Cloud for Mobile User Agent Group Names" )
54+ print ("7. Word Cloud for General User Agent Group Names" )
55+ print ("8. Exit" )
56+ choice = input ("Enter your choice (1/2/3/4/5/6/7/8): " )
57+
58+ if choice == "1" :
59+ create_labeled_pie_chart ({group : count for group , count in mobile_group_counts .items () if count < 10 }, "Mobile User Agents (Count < 10)" )
60+ plt .subplots_adjust (bottom = 0.1 , top = 1.0 ) # Adjust the layout
61+ #plt.savefig(os.path.join(charts_dir, "mobile_pie_chart.png"))
62+ elif choice == "2" :
63+ create_labeled_pie_chart ({group : count for group , count in mobile_group_counts .items () if 10 <= count < 500 }, "Mobile User Agents (10 <= Count < 500)" )
64+ #plt.savefig(os.path.join(charts_dir, "mobile_pie_chart_10_to_500.png"))
65+ elif choice == "3" :
66+ create_labeled_pie_chart ({group : count for group , count in general_group_counts .items () if 10 <= count < 50 }, "General User Agents (10 <= Count < 50)" )
67+ plt .subplots_adjust (bottom = 0.1 , top = 1.0 ) # Adjust the layout
68+ #plt.savefig(os.path.join(charts_dir, "general_pie_chart_10_to_50.png"))
69+ elif choice == "4" :
70+ create_labeled_pie_chart ({group : count for group , count in general_group_counts .items () if 50 <= count < 500 }, "General User Agents (50 <= Count < 500)" )
71+ #plt.savefig(os.path.join(charts_dir, "general_pie_chart_50_to_500.png"))
72+ elif choice == "5" :
73+ create_labeled_pie_chart ({group : count for group , count in general_group_counts .items () if count >= 500 }, "General User Agents (Count >= 500)" )
74+ #plt.savefig(os.path.join(charts_dir, "general_pie_chart_500_and_above.png"))
75+ elif choice == "6" :
76+ # Create a word cloud for mobile user agent group names
77+ wordcloud = WordCloud (width = 800 , height = 400 , background_color = "white" ).generate_from_frequencies (mobile_group_counts )
78+ plt .figure ()
79+ plt .imshow (wordcloud , interpolation = "bilinear" )
80+ plt .axis ("off" )
81+ plt .title ("Highest Mobile User Agents" )
82+ #plt.savefig(os.path.join(charts_dir, "mobile_wordcloud.png"))
83+ elif choice == "7" :
84+ # Create a word cloud for general user agent group names
85+ wordcloud = WordCloud (width = 800 , height = 400 , background_color = "white" ).generate_from_frequencies (general_group_counts )
86+ plt .figure ()
87+ plt .imshow (wordcloud , interpolation = "bilinear" )
88+ plt .axis ("off" )
89+ plt .title ("Highest General User Agents" )
90+ #plt.savefig(os.path.join(charts_dir, "general_wordcloud.png"))
91+ elif choice == "8" :
92+ print ("Exiting the program." )
93+ break
94+ else :
95+ print ("Invalid choice. Please enter a valid choice." )
96+ # Display the selected pie chart
97+ plt .show ()
0 commit comments