File size: 13,852 Bytes
c47ffce
6e67e3a
f8d8935
 
6e67e3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8d8935
6e67e3a
 
 
 
 
 
 
 
 
 
 
 
76eedaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e67e3a
 
76eedaa
 
 
 
 
 
 
 
 
6e67e3a
76eedaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e67e3a
76eedaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e67e3a
76eedaa
 
6e67e3a
76eedaa
 
 
 
 
 
 
 
 
 
 
14baf76
 
 
 
 
 
 
76eedaa
 
 
 
 
 
 
 
 
14baf76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76eedaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14baf76
76eedaa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14baf76
 
 
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
import json
import sys
sys.path.append(./config')
import config
import os
import re
import time
import logging
import mimetypes
import tempfile
from datetime import datetime
from pathlib import Path
from urllib.parse import urlparse
from typing import List, Dict, Tuple, Union, Optional
import requests
import validators
import gradio as gr
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from cleantext import clean
import qrcode
import zipfile


# Setup logging with detailed configuration
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(levelname)s - [%(filename)s:%(lineno)d] - %(message)s',
    handlers=[
        logging.StreamHandler(),
        logging.FileHandler('app.log', encoding='utf-8')
    ])
logger = logging.getLogger(__name__)


# Add these imports at the top
from config import Config
from proxy_handler import ProxyHandler
from robots_handler import RobotsHandler
import asyncio
import aiohttp
from tqdm import tqdm

# Add new imports for rate limiting and testing
from ratelimit import limits, sleep_and_retry
from typing import Dict, Any, Optional, List
import pytest
from urllib.robotparser import RobotFileParser
import concurrent.futures

class URLProcessor:
    def __init__(self):
        self.config = Config()
        self.proxy_handler = ProxyHandler(self.config.get('PROXY_URL'))
        self.robots_handler = RobotsHandler()
        self.session = self._create_session()
        self.rate_limit = self.config.get('RATE_LIMIT', 60)  # requests per minute
        self.timeout = self.config.get('TIMEOUT', 10)
        
    @sleep_and_retry
    @limits(calls=60, period=60)  # Rate limiting decorator
    def fetch_content(self, url: str) -> Optional[Dict]:
        """Fetch content with rate limiting"""
        if self.config.get('RESPECT_ROBOTS', True):
            if not self.robots_handler.can_fetch(url):
                logger.warning(f"Skipping {url} - robots.txt disallowed")
                return None
        
        def _create_session(self):
            session = requests.Session()
            if self.config.get('USE_PROXY'):
                session.proxies = self.proxy_handler.get_proxy_config()
            session.headers.update({
                'User-Agent': UserAgent().random,
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                'Accept-Language': 'en-US,en;q=0.5',
                'Accept-Encoding': 'gzip, deflate, br',
                'Connection': 'keep-alive',
                'Upgrade-Insecure-Requests': '1'
            })
            return session
        
        def _fetch_with_selenium(self, url: str) -> Optional[str]:
            try:
                chrome_options = Options()
                from selenium import webdriver
                from selenium.webdriver.chrome.options import Options
                from selenium.webdriver.common.by import By
                from selenium.webdriver.support.ui import WebDriverWait
                from selenium.webdriver.support import expected_conditions as EC
                from selenium.common.exceptions import TimeoutException
                import time
    
                logger.info(f"Attempting to fetch {url} with Selenium")
    
                # Set up Chrome options
                chrome_options = Options()
                chrome_options.add_argument("--headless")
                chrome_options.add_argument("--no-sandbox")
                chrome_options.add_argument("--disable-dev-shm-usage")
                chrome_options.add_argument("--disable-gpu")
                chrome_options.add_argument("--window-size=1920,1080")
                chrome_options.add_argument(
                    "user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
    
                # Initialize the driver
                driver = webdriver.Chrome(options=chrome_options)
    
                try:
                    # Navigate to the URL
                    driver.get(url)
    
                    # Wait for the page to load
                    WebDriverWait(driver, 10).until(
                        EC.presence_of_element_located((By.TAG_NAME, "body"))
                    )
    
                    # Simulate pressing ESC key to dismiss overlays
                    from selenium.webdriver.common.keys import Keys
                    action_chains = webdriver.ActionChains(driver)
                    action_chains.send_keys(Keys.ESCAPE).perform()
                    time.sleep(1)  # give it a moment to take effect
                    action_chains.reset_actions() # Clear actions
    
                    # try again
                    action_chains.send_keys(Keys.ESCAPE).perform()
                    time.sleep(1)  # give it a moment to take effect
                    action_chains.reset_actions()
    
                    # Get the page source
                    page_source = driver.page_source
    
                    # Save the Selenium HTML for debugging
                    debug_path = f"/Users/a2014/urld/debug_selenium_{int(time.time())}.html"
                    with open(debug_path, "w", encoding="utf-8") as f:
                        f.write(page_source)
                    logger.info(f"Saved Selenium HTML to {debug_path}")
    
                    return page_source
                finally:
                    driver.quit()
    
            except ImportError:
                logger.error("Selenium is not installed. Cannot use browser automation.")
                return None
            except Exception as e:
                logger.error(f"Selenium processing failed for {url}: {e}")
                return None
    
        async def fetch_urls_async(self, urls: List[str]) -> List[Dict]:
            """Asynchronous URL fetching with rate limiting"""
            async with aiohttp.ClientSession() as session:
                tasks = []
                for url in urls:
                    if len(tasks) >= self.rate_limit:
                        await asyncio.sleep(60)  # Rate limiting
                        tasks = []
                    tasks.append(self.fetch_content_async(session, url))
                return await asyncio.gather(*tasks)

def create_interface():
    css = """
    .container { max-width: 1200px; margin: auto; }
    .warning { background-color: #fff3cd; color: #856404; }
    .error { background-color: #f8d7da; color: #721c24; }
    """

    with gr.Blocks(css=css, title="Advanced Text & URL Processor") as interface:
        with gr.Tab("Settings"):
            respect_robots = gr.Checkbox(label="Respect robots.txt", value=True)
            use_proxy = gr.Checkbox(label="Use Proxy", value=False)
            proxy_url = gr.Textbox(label="Proxy URL", placeholder="http://proxy:port")
            request_delay = gr.Slider(minimum=0, maximum=10, value=1, label="Request Delay (seconds)")
            output_format = gr.Dropdown(choices=["json", "csv", "txt"], value="json", label="Output Format")

        gr.Markdown("# 🌐 Advanced URL & Text Processing Toolkit")

        with gr.Tab("URL Processing"):
            url_input = gr.Textbox(
                label="Enter URLs (comma or newline separated)",
                lines=5,
                placeholder="https://example1.com\nhttps://example2.com"
            )

        with gr.Tab("File Input"):
            file_input = gr.File(
                label="Upload text file or ZIP archive",
                file_types=[".txt", ".zip", ".md", ".csv", ".json", ".xml"]
            )

        with gr.Tab("Text Input"):
            text_input = gr.Textbox(
                label="Raw Text Input",
                lines=5,
                placeholder="Paste your text here..."
            )

        with gr.Tab("JSON Editor"):
            json_editor = gr.Textbox(
                label="JSON Editor",
                lines=20,
                placeholder="View and edit your JSON data here...",
                interactive=True,
                elem_id="json-editor"  # Optional: for custom styling
            )

        with gr.Tab("Scratchpad"):
            scratchpad = gr.Textbox(
                label="Scratchpad",
                lines=10,
                placeholder="Quick notes or text collections...",
                interactive=True
            )

        process_btn = gr.Button("Process Input", variant="primary")
        qr_btn = gr.Button("Generate QR Code", variant="secondary")

        output_text = gr.Textbox(label="Processing Results", interactive=False)
        output_file = gr.File(label="Processed Output")
        qr_output = gr.Image(label="QR Code", type="filepath")  # To display the generated QR code

        process_btn.click(
            process_all_inputs,
            inputs=[url_input, file_input, text_input, scratchpad],
            outputs=[output_file, output_text, json_editor]  # Update outputs to include JSON editor
        )
        qr_btn.click(
            generate_qr_code,
            inputs=json_editor,
            outputs=qr_output
        )
        gr.Markdown("""
    ### Usage Guidelines
    - **URL Processing**: Enter valid HTTP/HTTPS URLs
    - **File Input**: Upload text files or ZIP archives
    - ** Text Input**: Direct text processing
    - **JSON Editor**: View and edit your JSON data
    - **Scratchpad**: Quick notes or text collections
    - Advanced cleaning and validation included
    """)
    return interface


def check_network_connectivity():
    """Check if the network is working properly by testing connection to common sites"""
    test_sites = ["https://www.google.com", "https://www.cloudflare.com", "https://www.amazon.com"]
    results = []

    for site in test_sites:
        try:
            response = requests.get(site, timeout=5)
            results.append({
                "site": site,
                "status": "OK" if response.status_code == 200 else f"Error: {response.status_code}",
                "response_time": response.elapsed.total_seconds()
            })
        except Exception as e:
            results.append({
                "site": site,
                "status": f"Error: {str(e)}",
                "response_time": None
            })
    # If all sites failed, there might be a network issue
    if all(result["status"].startswith("Error") for result in results):
        logger.error("Network connectivity issue detected. All test sites failed.")
        return False, results

    return True, results


def validate_config(config: Dict[str, Any]) -> Dict[str, str]:
    """Validate configuration settings"""
    errors = {}
    if config.get('RATE_LIMIT', 0) < 1:
        errors['rate_limit'] = "Rate limit must be positive"
    if config.get('TIMEOUT', 0) < 1:
        errors['timeout'] = "Timeout must be positive"
    if config.get('USE_PROXY') and not config.get('PROXY_URL'):
        errors['proxy'] = "Proxy URL required when proxy is enabled"
    return errors

def update_settings(respect_robots: bool, use_proxy: bool, proxy_url: str, 
                   request_delay: float, output_format: str) -> str:
    """Update application settings"""
    config = Config()
    new_settings = {
        'RESPECT_ROBOTS': respect_robots,
        'USE_PROXY': use_proxy,
        'PROXY_URL': proxy_url,
        'REQUEST_DELAY': request_delay,
        'OUTPUT_FORMAT': output_format
    }
    
    # Validate settings before updating
    errors = validate_config(new_settings)
    if errors:
        return f"Configuration error: {', '.join(errors.values())}"
        
    config.update(new_settings)
    return "Configuration updated successfully"

def create_settings_tab() -> gr.Tab:
    """Create settings tab with configuration controls"""
    with gr.Tab("Settings") as settings_tab:
        respect_robots = gr.Checkbox(label="Respect robots.txt", value=True)
        use_proxy = gr.Checkbox(label="Use Proxy", value=False)
        proxy_url = gr.Textbox(label="Proxy URL", placeholder="http://proxy:port")
        request_delay = gr.Slider(minimum=0, maximum=10, value=1, label="Request Delay (seconds)")
        output_format = gr.Dropdown(choices=["json", "csv", "txt"], value="json", label="Output Format")
        
        settings_btn = gr.Button("Update Settings")
        settings_output = gr.Textbox(label="Settings Status")
        
        settings_btn.click(
            update_settings,
            inputs=[respect_robots, use_proxy, proxy_url, request_delay, output_format],
            outputs=settings_output
        )
        
    return settings_tab

def main():
    """Main application entry point"""
    try:
        # Initialize system settings
        mimetypes.init()
        
        # Validate initial configuration
        config = Config()
        errors = validate_config(config.get_all())
        if errors:
            logger.error(f"Configuration errors found: {errors}")
            sys.exit(1)

        # Check network connectivity
        network_ok, network_results = check_network_connectivity()
        if not network_ok:
            logger.warning("Network connectivity issues detected. Some features may not work properly.")
            for result in network_results:
                logger.warning(f"Test site {result['site']}: {result['status']}")

        # Create and launch interface
        interface = create_interface()
        
        # Launch with proper configuration
        interface.launch(
            server_name="0.0.0.0",
            server_port=7860,
            show_error=True,
            share=False,
            inbrowser=True,
            debug=True
        )
        
    except Exception as e:
        logger.error(f"Application startup failed: {str(e)}")
        sys.exit(1)

if __name__ == "__main__":
    main()