Adjusted the project structure and added things.

This commit is contained in:
Lord_Devi 2024-07-22 01:20:59 -04:00
parent 39c49a0713
commit 1e71b43377
7 changed files with 346 additions and 48 deletions

2
.gitignore vendored
View file

@ -1 +1 @@
*.csv
data/*

View file

@ -1,17 +1,21 @@
#!/usr/bin/env python
import csv
import sys
import os
from datetime import datetime
# Get the project root directory
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(script_dir)
data_dir = os.path.join(project_root, 'data')
def get_output_filename(input_filename, chunk_number):
base, ext = os.path.splitext(input_filename)
return f"{base}-chunk-{chunk_number}{ext}"
base, ext = os.path.splitext(os.path.basename(input_filename))
return os.path.join(data_dir, f"{base}-chunk-{chunk_number}{ext}")
def get_history_filename(input_filename):
base, ext = os.path.splitext(input_filename)
return f"{base}-history{ext}"
base, ext = os.path.splitext(os.path.basename(input_filename))
return os.path.join(data_dir, f"{base}-history{ext}")
def read_csv(filename):
with open(filename, 'r', newline='') as csvfile:
@ -51,10 +55,11 @@ def get_extracted_rows(history_filename):
return extracted_rows
def extract_targets(input_filename, num_rows):
full_input_path = os.path.join(data_dir, input_filename)
history_filename = get_history_filename(input_filename)
extracted_rows = get_extracted_rows(history_filename)
input_data = read_csv(input_filename)
input_data = read_csv(full_input_path)
headers = input_data[0]
new_chunk = [headers]
@ -88,8 +93,9 @@ if __name__ == "__main__":
input_filename = sys.argv[1]
num_rows = int(sys.argv[2])
if not os.path.exists(input_filename):
print(f"Error: Input file '{input_filename}' not found.")
full_input_path = os.path.join(data_dir, input_filename)
if not os.path.exists(full_input_path):
print(f"Error: Input file '{full_input_path}' not found.")
sys.exit(1)
extract_targets(input_filename, num_rows)

View file

@ -1,22 +0,0 @@
#!/usr/bin/env bash
# Check if the correct number of arguments is provided
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <input_file> <output_file>"
exit 1
fi
input_file="$1"
output_file="$2"
# Use awk to filter the lines and save to the output file
awk '
BEGIN { FS = OFS = "\"" }
{
url = $2
if (url !~ /^https?:\/\/[^:\/]*\.(gov|uk)([\/:]|$)/ && url !~ /\.govt\./) {
print $0
}
}' "$input_file" > "$output_file"
echo "Filtered lines have been saved to $output_file"

View file

@ -4,14 +4,15 @@ import sys
import re
import os
import csv
from urllib.parse import urlparse
def load_tld_list(project_root):
tld_file = os.path.join(project_root, 'tld-list.csv')
def load_tld_list(project_root, filename):
tld_file = os.path.join(project_root, 'conf.d', filename)
tlds = set()
with open(tld_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
tlds.add(row['tld'].lower())
tlds.add(row['tld'].lower().strip('.'))
return tlds
def filter_domains(input_file):
@ -19,20 +20,37 @@ def filter_domains(input_file):
script_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(script_dir)
# Load the TLD list
tld_list = load_tld_list(project_root)
# Load the positive and negative TLD lists
positive_tlds = load_tld_list(project_root, 'positive-tlds.csv')
negative_tlds = load_tld_list(project_root, 'negative-tlds.csv')
# Generate output filenames in the project root
# Generate output filenames in the data directory
data_dir = os.path.join(project_root, 'data')
base_name = os.path.splitext(os.path.basename(input_file))[0]
output_file = os.path.join(project_root, f"{base_name}-no-bad-doms.csv")
removed_file = os.path.join(project_root, f"{base_name}-removed-urls.csv")
output_file = os.path.join(data_dir, f"{base_name}-no-bad-doms.csv")
removed_file = os.path.join(data_dir, f"{base_name}-removed-urls.csv")
# Compile the regex patterns for efficiency
domain_pattern = re.compile(r'^https?://[^:/]*\.(gov|uk)([/:]|$)')
edu_govt_pattern = re.compile(r'\.(edu|govt)\.')
tld_pattern = re.compile(r'\.([a-z]{2,})([/:]|$)')
with open(input_file, 'r') as infile, \
def should_keep(url):
parsed_url = urlparse(url)
domain_parts = parsed_url.netloc.lower().split('.')
return any(part in positive_tlds for part in domain_parts)
def should_remove(url):
if domain_pattern.search(url) or edu_govt_pattern.search(url):
return True
parsed_url = urlparse(url)
domain_parts = parsed_url.netloc.lower().split('.')
return any(part in negative_tlds for part in domain_parts)
# Ensure the full path to the input file is in the data directory
input_file_full_path = os.path.join(data_dir, os.path.basename(input_file))
with open(input_file_full_path, 'r') as infile, \
open(output_file, 'w', newline='') as outfile, \
open(removed_file, 'w', newline='') as removedfile:
@ -48,15 +66,13 @@ def filter_domains(input_file):
# Extract URL without quotes for pattern matching
url_no_quotes = url.strip('"')
# Check if the URL matches any of the patterns to be filtered
if (not domain_pattern.search(url_no_quotes) and
not edu_govt_pattern.search(url_no_quotes) and
not any(url_no_quotes.endswith(tld) for tld in tld_list)):
# If it doesn't match, write to the main output file
# Check if the URL should be kept or removed
if should_keep(url_no_quotes):
outfile.write(line)
else:
# If it matches, write to the removed URLs file
elif should_remove(url_no_quotes):
removedfile.write(line)
else:
outfile.write(line) # If it's neither in positive nor negative list, we keep it
print(f"Filtered lines have been saved to {output_file}")
print(f"Removed URLs have been saved to {removed_file}")

36
bin/toclip.sh Executable file
View file

@ -0,0 +1,36 @@
#!/bin/bash
# Check if a file name is provided
if [ $# -eq 0 ]; then
echo "Error: No input file specified."
echo "Usage: $0 <input_file>"
exit 1
fi
# Get the script's directory
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# Get the project root directory (one level up from the script directory)
PROJECT_ROOT="$(dirname "$SCRIPT_DIR")"
# Set the data directory
DATA_DIR="$PROJECT_ROOT/data"
# Get the input file name from the command line argument
INPUT_FILE="$1"
# Construct the full path to the input file
FULL_PATH="$DATA_DIR/$INPUT_FILE"
# Check if the file exists
if [ ! -f "$FULL_PATH" ]; then
echo "Error: File '$FULL_PATH' not found."
exit 1
fi
echo "Processing file: $FULL_PATH"
# Use tail to skip the first line (header) and pipe to copyq
tail -n +2 "$FULL_PATH" | copyq copy -
echo "Contents of $INPUT_FILE (excluding header) have been copied to clipboard."

252
conf.d/negative-tlds.csv Normal file
View file

@ -0,0 +1,252 @@
tld
.ac
.ad
.ae
.af
.ag
.al
.am
.an
.ao
.aq
.ar
.as
.at
.au
.aw
.ax
.az
.ba
.bb
.bd
.be
.bf
.bg
.bh
.b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bl
29 .bm
30 .bn
31 .bo
32 .bq
33 .br
34 .bs
35 .bt
36 .bv
37 .bw
38 .by
39 .bz
40 .ca
41 .cc
42 .cd
43 .cf
44 .cg
45 .ch
46 .ci
47 .ck
48 .cl
49 .cm
50 .cn
51 .cr
52 .cu
53 .cv
54 .cw
55 .cx
56 .cy
57 .cz
58 .de
59 .dj
60 .dk
61 .dm
62 .do
63 .dz
64 .ec
65 .ee
66 .eg
67 .eh
68 .er
69 .es
70 .et
71 .eu
72 .fi
73 .fj
74 .fk
75 .fm
76 .fo
77 .fr
78 .ga
79 .gb
80 .gd
81 .ge
82 .gf
83 .gg
84 .gh
85 .gi
86 .gl
87 .gm
88 .gn
89 .gp
90 .gq
91 .gr
92 .gs
93 .gt
94 .gu
95 .gw
96 .gy
97 .hk
98 .hm
99 .hn
100 .hr
101 .ht
102 .hu
103 .id
104 .ie
105 .il
106 .im
107 .in
108 .iq
109 .ir
110 .is
111 .it
112 .je
113 .jm
114 .jo
115 .jp
116 .ke
117 .kg
118 .kh
119 .ki
120 .km
121 .kn
122 .kp
123 .kr
124 .kw
125 .ky
126 .kz
127 .la
128 .lb
129 .lc
130 .li
131 .lk
132 .lr
133 .ls
134 .lt
135 .lu
136 .lv
137 .ly
138 .ma
139 .mc
140 .md
141 .me
142 .mf
143 .mg
144 .mh
145 .mk
146 .ml
147 .mm
148 .mn
149 .mo
150 .mp
151 .mq
152 .mr
153 .ms
154 .mt
155 .mu
156 .mv
157 .mw
158 .mx
159 .my
160 .mz
161 .na
162 .nc
163 .ne
164 .nf
165 .ng
166 .ni
167 .nl
168 .no
169 .np
170 .nr
171 .nu
172 .nz
173 .om
174 .pa
175 .pe
176 .pf
177 .pg
178 .ph
179 .pk
180 .pl
181 .pm
182 .pn
183 .pr
184 .ps
185 .pt
186 .pw
187 .py
188 .qa
189 .re
190 .ro
191 .rs
192 .ru
193 .rw
194 .sa
195 .sb
196 .sc
197 .sd
198 .se
199 .sg
200 .sh
201 .si
202 .sj
203 .sk
204 .sl
205 .sm
206 .sn
207 .so
208 .sr
209 .ss
210 .st
211 .su
212 .sv
213 .sx
214 .sy
215 .sz
216 .tc
217 .td
218 .tf
219 .tg
220 .th
221 .tj
222 .tk
223 .tl
224 .tm
225 .tn
226 .to
227 .tp
228 .tr
229 .tt
230 .tv
231 .tw
232 .tz
233 .ua
234 .ug
235 .uk
236 .um
237 .uy
238 .uz
239 .va
240 .vc
241 .ve
242 .vg
243 .vi
244 .vn
245 .vu
246 .wf
247 .ws
248 .ye
249 .yt
250 .za
251 .zm
252 .zw

10
conf.d/positive-tlds.csv Normal file
View file

@ -0,0 +1,10 @@
tld
.com
.net
.org
.io
.biz
.agency
.legal
.co
.us
1 tld
2 .com
3 .net
4 .org
5 .io
6 .biz
7 .agency
8 .legal
9 .co
10 .us