File size: 14,835 Bytes
60a25ab
1b5b9ce
200e562
 
def429d
200e562
c1f083f
200e562
4d3af6e
c1f083f
200e562
c1f083f
200e562
 
 
29808fd
200e562
4d3af6e
200e562
29808fd
200e562
4d3af6e
c1f083f
1160608
c1f083f
 
 
4d3af6e
c1f083f
 
4d3af6e
def429d
4d3af6e
3707133
 
 
 
 
 
 
 
29808fd
 
 
 
3707133
 
c1f083f
 
3707133
200e562
 
 
c1f083f
200e562
 
 
4d3af6e
200e562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1f083f
4d3af6e
200e562
 
 
c1f083f
200e562
 
 
 
 
 
 
 
 
 
 
 
 
 
4d3af6e
29808fd
eed2b1e
 
 
 
 
 
 
 
 
 
29808fd
 
 
 
 
 
eed2b1e
 
 
 
 
 
 
 
 
 
 
29808fd
 
 
 
 
 
 
 
 
 
 
 
 
 
4d3af6e
29808fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d3af6e
 
29808fd
 
 
 
 
 
 
 
 
 
 
 
 
c1f083f
4d3af6e
 
200e562
4d3af6e
 
 
 
 
 
 
 
 
 
 
 
29808fd
 
4d3af6e
 
 
 
 
 
c1f083f
4d3af6e
 
 
 
 
 
 
 
c1f083f
4d3af6e
 
c1f083f
4d3af6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1f083f
4d3af6e
c1f083f
29808fd
4d3af6e
29808fd
 
 
 
 
 
 
 
 
 
 
c1f083f
29808fd
 
287afed
c1f083f
4d3af6e
c1f083f
4d3af6e
 
 
 
 
 
 
 
ecc3973
4d3af6e
 
 
 
 
 
200e562
4d3af6e
 
 
 
 
 
 
 
 
 
 
200e562
c1f083f
4d3af6e
 
 
 
c1f083f
4d3af6e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352fbac
4d3af6e
 
 
 
 
 
 
 
 
 
 
352fbac
 
 
 
4d3af6e
 
 
 
 
 
 
 
 
 
 
c1f083f
200e562
4d3af6e
 
 
200e562
c1f083f
4d3af6e
 
 
 
 
 
 
200e562
a514380
60a25ab
4d3af6e
 
c1f083f
 
200e562
c1f083f
 
4d3af6e
c1f083f
 
 
4d3af6e
 
 
 
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import json
import os
import re
import time
import logging
import mimetypes
import concurrent.futures
import string
from typing import List, Dict, Optional, Union
from pathlib import Path
from urllib.parse import urlparse

import requests
import validators
import gradio as gr
from diskcache import Cache
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from ratelimit import limits, sleep_and_retry
from cleantext import clean

# 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__)

class URLProcessor:
    """Class to handle URL processing with advanced features"""
    
    def __init__(self, timeout=15, max_retries=3, cache_dir='cache'):
        self.ua = UserAgent()  # Initialize UserAgent first
        self.timeout = timeout
        self.max_retries = max_retries
        
        # Persistent caching setup
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(exist_ok=True)
        self.url_cache = Cache(str(self.cache_dir / 'url_cache'))
        self.content_cache = Cache(str(self.cache_dir / 'content_cache'), size_limit=2**30)

        # Session configuration
        self.session = requests.Session()
        self.session.headers.update({
            'User-Agent': self.ua.random,  # Correct header key
            'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
            'Accept-Language': 'en-US,en;q=0.5',
            'Connection': 'keep-alive'
        })
    @sleep_and_retry
    @limits(calls=10, period=60)  # Rate limiting: 10 calls per minute
    def validate_url(self, url: str) -> Dict[str, Union[bool, str]]:
        """Validate URL format and accessibility"""
        try:
            # Check cache first
            if url in self.url_cache:
                return self.url_cache[url]
            
            result = urlparse(url)
            validation_result = {
                'is_valid': False,
                'message': 'Invalid URL',
                'scheme': result.scheme,
                'netloc': result.netloc
            }
            
            if not all([result.scheme, result.netloc]):
                validation_result['message'] = 'Missing scheme or network location'
                return validation_result
            
            if not validators.url(url):
                validation_result['message'] = 'URL format validation failed'
                return validation_result
            
            # Perform HEAD request for accessibility
            try:
                response = self.session.head(url, timeout=self.timeout, allow_redirects=True)
                validation_result['is_valid'] = response.status_code in [200, 301, 302]
                validation_result['status_code'] = response.status_code
                validation_result['message'] = f"URL is {'valid' if validation_result['is_valid'] else 'invalid'}"
            except requests.RequestException as e:
                validation_result['message'] = f"Connection error: {str(e)}"
            
            # Cache the result
            self.url_cache[url] = validation_result
            return validation_result
        
        except Exception as e:
            logger.error(f"Unexpected error validating URL {url}: {e}")
            return {
                'is_valid': False, 
                'message': f"Unexpected validation error: {str(e)}"
            }

    @sleep_and_retry
    @limits(calls=20, period=60)  # Refined rate limiting
    def fetch_content(self, url: str) -> Optional[Dict]:
        """Fetch and structure content from URL, handling Google Drive and Calendar links."""
        if 'drive.google.com' in url:
            # Convert Google Drive URL to direct download link
            file_id = re.search(r'/file/d/([a-zA-Z0-9_-]+)', url)
            if file_id:
                url = f"https://drive.google.com/uc?export=download&id={file_id.group(1)}"
            else:
                logger.error(f"Invalid Google Drive URL: {url}")
                return None
    
        cached = self.content_cache.get(url)
        if cached:
            return cached
    
        try:
            response = self.session.get(url, timeout=self.timeout)
            response.raise_for_status()
    
            # Handle ICS files (Google Calendar)
            if 'text/calendar' in response.headers.get('Content-Type', ''):
                return {
                    'content': response.text,
                    'content_type': 'text/calendar',
                    'timestamp': datetime.now().isoformat()
                }
    
            # Handle HTML content
            soup = BeautifulSoup(response.text, 'html.parser')
            title = soup.title.text.strip() if soup.title else ''
            meta_desc = soup.find('meta', {'name': 'description'})
            description = meta_desc['content'].strip() if meta_desc else ''
            
            headings = [{'level': h.name, 'text': h.text.strip()} 
                       for h in soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])]
            
            links = [a['href'] for a in soup.find_all('a', href=True) 
                    if validators.url(a['href'])]
            
            # Main content extraction
            for element in soup(['script', 'style', 'nav', 'footer']):
                element.decompose()
                
            main_content = soup.find('main') or soup.find('article') or soup.body
            text = main_content.get_text(separator=' ') if main_content else ''
            
            structured_data = {
                'title': title,
                'description': description,
                'headings': headings,
                'links': links,
                'content': self.advanced_text_cleaning(text),
                'status_code': response.status_code,
                'content_type': response.headers.get('Content-Type', ''),
                'timestamp': datetime.now().isoformat()
            }
            
            self.content_cache.set(url, structured_data, expire=3600)
            return structured_data
        except Exception as e:
            logger.error(f"Error fetching {url}: {e}")
            return None

    def advanced_text_cleaning(self, text: str) -> str:
        return clean(text,
            fix_unicode=True,
            to_ascii=True,
            lower=True,
            no_line_breaks=True,
            no_urls=True,
            no_emails=True,
            no_phone_numbers=True,
            no_numbers=False,
            no_digits=False,
            no_currency_symbols=True,
            no_punct=False
        ).strip()

class FileProcessor:
    """Class to handle file processing"""
    
    def __init__(self, max_file_size: int = 10 * 1024 * 1024):  # 10MB default
        self.max_file_size = max_file_size
        self.supported_text_extensions = {'.txt', '.md', '.csv', '.json', '.xml'}

    def is_text_file(self, filepath: str) -> bool:
        """Check if file is a text file"""
        try:
            mime_type, _ = mimetypes.guess_type(filepath)
            return mime_type and mime_type.startswith('text/')
        except Exception:
            return False


    
    def process_file(self, file) -> List[Dict]:
        """Process uploaded file with enhanced error handling"""
        if not file:
            return []

        dataset = []
        try:
            file_size = os.path.getsize(file.name)
            if file_size > self.max_file_size:
                logger.warning(f"File size ({file_size} bytes) exceeds maximum allowed size")
                return []

            with tempfile.TemporaryDirectory() as temp_dir:
                if zipfile.is_zipfile(file.name):
                    dataset.extend(self._process_zip_file(file.name, temp_dir))
                else:
                    dataset.extend(self._process_single_file(file))

        except Exception as e:
            logger.error(f"Error processing file: {str(e)}")
            return []

        return dataset

    def _process_zip_file(self, zip_path: str, temp_dir: str) -> List[Dict]:
        """Process ZIP file contents"""
        results = []
        with zipfile.ZipFile(zip_path, 'r') as zip_ref:
            zip_ref.extractall(temp_dir)
            for root, _, files in os.walk(temp_dir):
                for filename in files:
                    filepath = os.path.join(root, filename)
                    if self.is_text_file(filepath):
                        try:
                            with open(filepath, 'r', errors='ignore') as f:
                                content = f.read()
                            if content.strip():
                                results.append({
                                    "source": "file",
                                    "filename": filename,
                                    "content": content,
                                    "timestamp": time.strftime("%Y-%m-%d %H:%M:%S")
                                })
                        except Exception as e:
                            logger.error(f"Error reading file {filename}: {str(e)}")
        return results

    def _process_single_file(self, file) -> List[Dict]:
        try:
            file_stat = os.stat(file.name)
            content = file.read().decode('utf-8', errors='ignore')
            
            return [{
                'source': 'file',
                'filename': os.path.basename(file.name),
                'file_size': file_stat.st_size,
                'mime_type': mimetypes.guess_type(file.name)[0],
                'created': datetime.fromtimestamp(file_stat.st_ctime).isoformat(),
                'modified': datetime.fromtimestamp(file_stat.st_mtime).isoformat(),
                'content': content,
                'timestamp': datetime.now().isoformat()
            }]
        except Exception as e:
            logger.error(f"File processing error: {e}")
            return []

def create_interface():
    """Create a comprehensive Gradio interface with advanced features"""
    
    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:
        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..."
            )
        
        process_btn = gr.Button("Process Input", variant="primary")
        
        output_text = gr.Textbox(label="Processing Results", interactive=False)
        output_file = gr.File(label="Processed Output")
        
        def process_all_inputs(urls, file, text):
            """Process all input types with progress tracking"""
            try:
                processor = URLProcessor()
                file_processor = FileProcessor()
                results = []
                
                # Process URLs
                if urls:
                    url_list = re.split(r'[,\n]', urls)
                    url_list = [url.strip() for url in url_list if url.strip()]
                    
                    for url in url_list:
                        validation = processor.validate_url(url)
                        if validation.get('is_valid'):
                            content = processor.fetch_content(url)
                            if content:
                                results.append({
                                    'source': 'url',
                                    'url': url,
                                    'content': content,
                                    'timestamp': time.strftime("%Y-%m-%d %H:%M:%S")
                                })
                
                # Process files
                if file:
                    results.extend(file_processor.process_file(file))
        
                # Process text input
                if text:
                    cleaned_text = processor.advanced_text_cleaning(text)
                    results.append({
                        'source': 'direct_input',
                        'content': cleaned_text,
                        'timestamp': time.strftime("%Y-%m-%d %H:%M:%S")
                    })
                
                # Generate output
                if results:
                    output_dir = Path('output') / datetime.now().strftime('%Y-%m-%d')
                    output_dir.mkdir(parents=True, exist_ok=True)
                    output_path = output_dir / f'processed_{int(time.time())}.json'
                    
                    with open(output_path, 'w', encoding='utf-8') as f:
                        json.dump(results, f, ensure_ascii=False, indent=2)
                    
                    summary = f"Processed {len(results)} items successfully!"
                    return output_path, summary
                else:
                    return None, "No valid content to process."
            
            except Exception as e:
                logger.error(f"Processing error: {e}")
                return None, f"Error: {str(e)}"
        
        process_btn.click(
            process_all_inputs, 
            inputs=[url_input, file_input, text_input], 
            outputs=[output_file, output_text]
        )
        
        gr.Markdown("""
        ### Usage Guidelines
        - **URL Processing**: Enter valid HTTP/HTTPS URLs
        - **File Input**: Upload text files or ZIP archives
        - **Text Input**: Direct text processing
        - Advanced cleaning and validation included
        """)
    
    return interface

def main():
    # Configure system settings
    mimetypes.init()
    
    # Create and launch interface
    interface = create_interface()
    interface.launch(
        share=True,
        server_name="0.0.0.0",
        server_port=7860,
        debug=True
    )

if __name__ == "__main__":
    main()