File size: 1,948 Bytes
f2fb12f
 
 
 
369e64d
f2fb12f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0dbcacb
f2fb12f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c562ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f2fb12f
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
"""Data loading utilities for phishing detection."""

import pandas as pd
from pathlib import Path
from typing import Union


def load_phishing_urls(
    file_path: Union[str, Path] = None,
    is_phishing: bool = True,
) -> pd.DataFrame:
    """
    Load phishing URLs from a text file into a DataFrame.

    Args:
        file_path: Path to the file containing phishing URLs (one per line).
                   If None, uses the default phishing-links-ACTIVE.txt in the data directory.
        is_phishing: Whether to add a label column indicating phishing (default True)

    Returns:
        DataFrame with columns: url_id, url, and optionally is_phishing
    """
    if file_path is None:
        # Default to the file in the same directory as this module
        file_path = Path(__file__).parent / "data_files" /"phishing-links-ACTIVE.txt"
    else:
        file_path = Path(file_path)

    if not file_path.exists():
        raise FileNotFoundError(f"File not found: {file_path}")

    # Read URLs from file
    with open(file_path, 'r', encoding='utf-8') as f:
        urls = [line.strip() for line in f if line.strip()]

    # Create DataFrame
    df = pd.DataFrame({
        'url_id': range(len(urls)),
        'url': urls,
        'is_phishing': int(is_phishing)
    })

    return df

def convert_csv_to_urls(csv_path: Union[str, Path], output_path: Union[str, Path]) -> None:
    """
    Convert a CSV file containing URLs to a text file with one URL per line.

    Args:
        csv_path: Path to the input CSV file.
        output_path: Path to the output text file.
    """
    csv_path = Path(csv_path)
    output_path = Path(output_path)

    if not csv_path.exists():
        raise FileNotFoundError(f"CSV file not found: {csv_path}")

    # Read CSV and extract domains
    df = pd.read_csv(csv_path, header=None, names=['rank', 'domain'])

    # Write domains to text file
    output_path.write_text('\n'.join(df['domain']))