Mohzen321 commited on
Commit
1311fed
·
verified ·
1 Parent(s): 1bed17c

Create Script.py

Browse files
Files changed (1) hide show
  1. Script.py +245 -0
Script.py ADDED
@@ -0,0 +1,245 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ import re
4
+ import time
5
+
6
+ # تحميل النموذج
7
+ classifier = pipeline("zero-shot-classification", model="cross-encoder/nli-distilroberta-base")
8
+
9
+ # عنوان التطبيق
10
+ st.title("Keyword & URL Analysis App")
11
+
12
+ # اختيار العملية
13
+ operation = st.radio("Choose an operation:", ["Filter Keywords", "Analyze URLs"])
14
+
15
+ # إدخال الملف النصي
16
+ uploaded_file = st.file_uploader("Upload a text file", type=["txt"])
17
+
18
+ if uploaded_file is not None:
19
+ # قراءة الملف النصي
20
+ content = uploaded_file.read().decode("utf-8")
21
+ items = [line.strip() for line in content.splitlines() if line.strip()]
22
+
23
+ # تحديد الفئات
24
+ categories = ["shop", "game", "stream"]
25
+
26
+ # قوائم لتخزين النتائج (Keywords)
27
+ shopping_items = []
28
+ gaming_items = []
29
+ streaming_items = []
30
+ unknown_items = []
31
+
32
+ # قوائم لتحليل الروابط
33
+ parameters = []
34
+ domains = []
35
+ full_page_types = []
36
+ file_extensions = []
37
+
38
+ # متغيرات للتحكم في العملية
39
+ progress_bar = st.progress(0)
40
+ pause_button = st.button("Pause")
41
+ stop_button = st.button("Stop")
42
+ continue_button = st.button("Continue")
43
+ paused = False
44
+ stopped = False
45
+ current_index = 0 # مؤشر للعنصر الحالي
46
+ batch_size = 10 # عدد العناصر التي يتم معالجتها في الدفعة الواحدة
47
+
48
+ # دالة تصنيف الكلمات المفتاحية باستخدام الدفعات
49
+ def classify_keywords_batch(items, categories, start_index=0):
50
+ global paused, stopped, current_index
51
+ total_items = len(items)
52
+ for i in range(start_index, total_items, batch_size):
53
+ if stopped:
54
+ break
55
+ if paused:
56
+ time.sleep(0.5)
57
+ continue
58
+
59
+ # معالجة دفعة من العناصر
60
+ batch = items[i:i + batch_size]
61
+ results = classifier(batch, categories)
62
+
63
+ for j, result in enumerate(results):
64
+ best_category = result['labels'][0]
65
+ score = result['scores'][0]
66
+
67
+ if best_category == "shop" and score > 0.5:
68
+ shopping_items.append(batch[j])
69
+ elif best_category == "game" and score > 0.5:
70
+ gaming_items.append(batch[j])
71
+ elif best_category == "stream" and score > 0.5:
72
+ streaming_items.append(batch[j])
73
+ else:
74
+ unknown_items.append(batch[j])
75
+
76
+ # تحديث المؤشر الحالي
77
+ current_index = min(i + batch_size, total_items)
78
+
79
+ # تحديث شريط التقدم
80
+ progress = min((current_index) / total_items, 1.0)
81
+ progress_bar.progress(progress)
82
+
83
+ # تحديث النتائج في الوقت الحقيقي
84
+ update_results()
85
+
86
+ # إبطاء العملية قليلاً للسماح بتحديث الواجهة
87
+ time.sleep(0.1)
88
+
89
+ # دالة تحليل الروابط
90
+ def analyze_urls(urls, start_index=0):
91
+ global paused, stopped, current_index
92
+ total_items = len(urls)
93
+ for i in range(start_index, total_items, batch_size):
94
+ if stopped:
95
+ break
96
+ if paused:
97
+ time.sleep(0.5)
98
+ continue
99
+
100
+ # معالجة دفعة من الروابط
101
+ batch = urls[i:i + batch_size]
102
+ for url in batch:
103
+ # استخراج الباراميترات بدون '='
104
+ params = re.findall(r'(\w+)=', url)
105
+ parameters.extend(params)
106
+
107
+ # استخراج الدومينات فقط (مثل .com, .org)
108
+ domain_match = re.search(r'\.([a-zA-Z]{2,})$', url)
109
+ if domain_match:
110
+ domain = domain_match.group(1)
111
+ if domain not in domains:
112
+ domains.append(domain)
113
+
114
+ # استخراج أنماط الصفحات الكاملة (مثل product_detail.php?, index.php?)
115
+ page_type_match = re.search(r'(\w+\.[a-z]+)\?', url)
116
+ if page_type_match:
117
+ page_type = page_type_match.group(1)
118
+ if page_type not in full_page_types:
119
+ full_page_types.append(page_type)
120
+
121
+ # استخراج الصيغ (مثل php, phtml, asp) بدون باقي الرابط
122
+ extension_match = re.search(r'\.([a-z]+)(\?|$)', url)
123
+ if extension_match:
124
+ extension = extension_match.group(1)
125
+ if extension not in file_extensions:
126
+ file_extensions.append(extension)
127
+
128
+ # تحديث المؤشر الحالي
129
+ current_index = min(i + batch_size, total_items)
130
+
131
+ # تحديث شريط التقدم
132
+ progress = min((current_index) / total_items, 1.0)
133
+ progress_bar.progress(progress)
134
+
135
+ # تحديث النتائج في الوقت الحقيقي
136
+ update_results()
137
+
138
+ # إبطاء العملية قليلاً للسماح بتحديث الواجهة
139
+ time.sleep(0.1)
140
+
141
+ # دالة تحديث النتائج
142
+ def update_results():
143
+ # تحديث محتوى المربعات النصية للكلمات المفتاحية
144
+ st.session_state.shopping_text = "\n".join(shopping_items)
145
+ st.session_state.gaming_text = "\n".join(gaming_items)
146
+ st.session_state.streaming_text = "\n".join(streaming_items)
147
+ st.session_state.unknown_text = "\n".join(unknown_items)
148
+
149
+ # تحديث محتوى المربعات الخاصة بالروابط
150
+ st.session_state.parameters = "\n".join(set(parameters))
151
+ st.session_state.domains = "\n".join(domains)
152
+ st.session_state.full_page_types = "\n".join(full_page_types)
153
+ st.session_state.file_extensions = "\n".join(file_extensions)
154
+
155
+ # دالة تصدير النتائج
156
+ def export_results(key, filename):
157
+ with open(filename, "w") as f:
158
+ f.write(st.session_state[key])
159
+ st.success(f"Results exported to {filename}")
160
+
161
+ # زر البدء
162
+ if st.button("Start"):
163
+ stopped = False
164
+ paused = False
165
+ current_index = 0
166
+
167
+ if operation == "Filter Keywords":
168
+ classify_keywords_batch(items, categories, start_index=current_index)
169
+ elif operation == "Analyze URLs":
170
+ analyze_urls(items, start_index=current_index)
171
+
172
+ # زر الإيقاف المؤقت
173
+ if pause_button:
174
+ paused = True
175
+ st.write("Process paused.")
176
+
177
+ # زر الاستمرار
178
+ if continue_button and paused:
179
+ paused = False
180
+ st.write("Process resumed.")
181
+ if operation == "Filter Keywords":
182
+ classify_keywords_batch(items, categories, start_index=current_index)
183
+ elif operation == "Analyze URLs":
184
+ analyze_urls(items, start_index=current_index)
185
+
186
+ # زر التوقف الكامل
187
+ if stop_button:
188
+ stopped = True
189
+ st.write("Process stopped.")
190
+
191
+ # عرض النتائج بناءً على الخيار المختار
192
+ if operation == "Filter Keywords":
193
+ # عرض النتائج للكلمات المفتاحية
194
+ st.header("Shopping Keywords")
195
+ if 'shopping_text' not in st.session_state:
196
+ st.session_state.shopping_text = ""
197
+ st.text_area("Copy the shopping keywords here:", value=st.session_state.shopping_text, height=200, key="shopping")
198
+ st.button("Export Shopping Keywords", on_click=export_results, args=("shopping_text", "shopping_keywords.txt"))
199
+
200
+ st.header("Gaming Keywords")
201
+ if 'gaming_text' not in st.session_state:
202
+ st.session_state.gaming_text = ""
203
+ st.text_area("Copy the gaming keywords here:", value=st.session_state.gaming_text, height=200, key="gaming")
204
+ st.button("Export Gaming Keywords", on_click=export_results, args=("gaming_text", "gaming_keywords.txt"))
205
+
206
+ st.header("Streaming Keywords")
207
+ if 'streaming_text' not in st.session_state:
208
+ st.session_state.streaming_text = ""
209
+ st.text_area("Copy the streaming keywords here:", value=st.session_state.streaming_text, height=200, key="streaming")
210
+ st.button("Export Streaming Keywords", on_click=export_results, args=("streaming_text", "streaming_keywords.txt"))
211
+
212
+ st.header("Unknown Keywords")
213
+ if 'unknown_text' not in st.session_state:
214
+ st.session_state.unknown_text = ""
215
+ st.text_area("Copy the unknown keywords here:", value=st.session_state.unknown_text, height=200, key="unknown")
216
+ st.button("Export Unknown Keywords", on_click=export_results, args=("unknown_text", "unknown_keywords.txt"))
217
+
218
+ elif operation == "Analyze URLs":
219
+ # عرض النتائج للروابط
220
+ st.header("Parameters")
221
+ if 'parameters' not in st.session_state:
222
+ st.session_state.parameters = ""
223
+ st.text_area("Copy the parameters here:", value=st.session_state.parameters, height=200, key="parameters")
224
+ st.button("Export Parameters", on_click=export_results, args=("parameters", "parameters.txt"))
225
+
226
+ st.header("Domains")
227
+ if 'domains' not in st.session_state:
228
+ st.session_state.domains = ""
229
+ st.text_area("Copy the domains here:", value=st.session_state.domains, height=200, key="domains")
230
+ st.button("Export Domains", on_click=export_results, args=("domains", "domains.txt"))
231
+
232
+ st.header("Full PageType")
233
+ if 'full_page_types' not in st.session_state:
234
+ st.session_state.full_page_types = ""
235
+ st.text_area("Copy the full page types here:", value=st.session_state.full_page_types, height=200, key="full_page_types")
236
+ st.button("Export Full PageTypes", on_click=export_results, args=("full_page_types", "full_page_types.txt"))
237
+
238
+ st.header("File Extensions")
239
+ if 'file_extensions' not in st.session_state:
240
+ st.session_state.file_extensions = ""
241
+ st.text_area("Copy the file extensions here:", value=st.session_state.file_extensions, height=200, key="file_extensions")
242
+ st.button("Export File Extensions", on_click=export_results, args=("file_extensions", "file_extensions.txt"))
243
+
244
+ else:
245
+ st.warning("Please upload a text file to start analysis.")