Skip to content

Commit 6a460ad

Browse files
committed
cap14: urls encurtadas
1 parent c5fb807 commit 6a460ad

File tree

8 files changed

+467
-155
lines changed

8 files changed

+467
-155
lines changed

links/FPY.LI.htaccess

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,3 +1291,21 @@ RedirectTemp /6v https://pt.wikipedia.org/wiki/Princ%C3%ADpio_da_segrega%C3%A7%C
12911291

12921292
# cap13: appended 2025-11-13 14:52:00
12931293
RedirectTemp /6v https://docs.python.org/pt-br/3.14/library/typing.html#protocols
1294+
1295+
# cap14: appended 2025-11-17 20:07:32
1296+
RedirectTemp /6w https://docs.python.org/pt-br/3/tutorial/classes.html
1297+
RedirectTemp /6x https://docs.python.org/pt-br/3/library/collections.html#ordereddict-examples-and-recipes
1298+
RedirectTemp /6y https://pt.wikipedia.org/wiki/Busca_em_largura
1299+
RedirectTemp /6z https://docs.python.org/pt-br/3/library/collections.abc.html
1300+
RedirectTemp /72 https://docs.python.org/pt-br/3/library/http.server.html
1301+
RedirectTemp /73 https://docs.python.org/pt-br/3/library/socketserver.html#socketserver.ForkingMixIn
1302+
RedirectTemp /74 https://docs.python.org/pt-br/3/library/os.html#os.fork
1303+
RedirectTemp /75 https://pt.wikipedia.org/wiki/Template_Method
1304+
RedirectTemp /76 https://docs.python.org/pt-br/3/library/tkinter.html
1305+
RedirectTemp /77 https://docs.python.org/pt-br/3/library/tkinter.ttk.html
1306+
RedirectTemp /78 https://docs.python.org/pt-br/3/library/socketserver.html
1307+
RedirectTemp /79 https://docs.python.org/pt-br/3/library/socketserver.html#socketserver.BaseServer
1308+
RedirectTemp /7a https://docs.python.org/pt-br/3/library/typing.html#typing.final
1309+
RedirectTemp /7b https://pt.wikipedia.org/wiki/Polimorfismo_(ci%C3%AAncia_da_computa%C3%A7%C3%A3o)
1310+
RedirectTemp /7c https://pt.wikipedia.org/wiki/POSIX
1311+
RedirectTemp /7d https://docs.python.org/pt-br/3/library/typing.html#typing.Final

links/encurtar.ipynb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "63ff70d0-34e4-426b-a455-4486922b0a75",
6+
"metadata": {},
7+
"source": [
8+
"# Processo para encurtar URLS"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"id": "565ac98b-4f9e-4fd7-b23d-7bddcf2dd60a",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"from list_urls import find_urls\n"
19+
]
20+
},
21+
{
22+
"cell_type": "code",
23+
"execution_count": 2,
24+
"id": "0f2ce1b8-1c45-41a0-9f38-675e6912d805",
25+
"metadata": {},
26+
"outputs": [
27+
{
28+
"name": "stdout",
29+
"output_type": "stream",
30+
"text": [
31+
"+ /7c\thttps://pt.wikipedia.org/wiki/POSIX\n",
32+
"+ /7d\thttps://docs.python.org/pt-br/3/library/typing.html#typing.Final\n"
33+
]
34+
},
35+
{
36+
"name": "stderr",
37+
"output_type": "stream",
38+
"text": [
39+
"2 directives appended to FPY.LI.htaccess\n"
40+
]
41+
}
42+
],
43+
"source": [
44+
"CAP = 14\n",
45+
"\n",
46+
"adoc_path = f'../online/cap{CAP}.adoc'\n",
47+
"\n",
48+
"with open(adoc_path) as adoc:\n",
49+
" lines = adoc.readlines()\n",
50+
"\n",
51+
"urls = find_urls(lines, fpy=False)\n",
52+
"\n",
53+
"htaccess_path = 'FPY.LI.htaccess'\n",
54+
"\n",
55+
"from shortener import main\n",
56+
"redirects = main(htaccess_path, urls)"
57+
]
58+
},
59+
{
60+
"cell_type": "code",
61+
"execution_count": 3,
62+
"id": "37c13b84-c4a4-4295-8987-205c8da7a2e5",
63+
"metadata": {},
64+
"outputs": [
65+
{
66+
"name": "stdout",
67+
"output_type": "stream",
68+
"text": [
69+
"+ /7c\thttps://pt.wikipedia.org/wiki/POSIX\n",
70+
"+ /7d\thttps://docs.python.org/pt-br/3/library/typing.html#typing.Final\n"
71+
]
72+
}
73+
],
74+
"source": [
75+
"for line in redirects:\n",
76+
" print(line)"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 4,
82+
"id": "25b20f6b-471f-4a14-bdd0-22eb21627962",
83+
"metadata": {},
84+
"outputs": [
85+
{
86+
"name": "stdout",
87+
"output_type": "stream",
88+
"text": [
89+
"/7c https://pt.wikipedia.org/wiki/POSIX\n",
90+
"/7d https://docs.python.org/pt-br/3/library/typing.html#typing.Final\n"
91+
]
92+
}
93+
],
94+
"source": [
95+
"import io\n",
96+
"\n",
97+
"from replace_urls import main\n",
98+
"\n",
99+
"pairs_file = io.StringIO('\\n'.join(redirects))\n",
100+
"main(pairs_file, adoc_path)\n",
101+
"\n"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": 5,
107+
"id": "a66fdca6-e1df-4b1d-8971-1baae54cb9ed",
108+
"metadata": {},
109+
"outputs": [],
110+
"source": [
111+
"!grep 'https://(?!f[^./]*\\.)[a-zA-Z0-9.-]+[^/]*' ../online/cap14.adoc"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"id": "0eac9caf-6793-41c4-8fa3-4f3511c1d515",
117+
"metadata": {},
118+
"source": [
119+
"Regex sugerida pelo DeepSeek, que funcionou no VS code para achar URLs de domínios sem inicial \"f\":\n",
120+
"\n",
121+
"```https://(?!f[^.]+\\.)[^/]+```"
122+
]
123+
},
124+
{
125+
"cell_type": "code",
126+
"execution_count": null,
127+
"id": "5980e0be-a2e2-42e5-8b6e-efc4a9fe0d3a",
128+
"metadata": {},
129+
"outputs": [],
130+
"source": []
131+
},
132+
{
133+
"cell_type": "code",
134+
"execution_count": null,
135+
"id": "b8cd03b9-1af6-46ca-a293-58fce7810581",
136+
"metadata": {},
137+
"outputs": [],
138+
"source": []
139+
}
140+
],
141+
"metadata": {
142+
"kernelspec": {
143+
"display_name": "Python 3 (ipykernel)",
144+
"language": "python",
145+
"name": "python3"
146+
},
147+
"language_info": {
148+
"codemirror_mode": {
149+
"name": "ipython",
150+
"version": 3
151+
},
152+
"file_extension": ".py",
153+
"mimetype": "text/x-python",
154+
"name": "python",
155+
"nbconvert_exporter": "python",
156+
"pygments_lexer": "ipython3",
157+
"version": "3.14.0"
158+
}
159+
},
160+
"nbformat": 4,
161+
"nbformat_minor": 5
162+
}

links/list_urls.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
URL_RE = re.compile(r"""(https?://[^\s[<>"']+)\[""")
77

88

9-
def find_urls(fpy=True, long=True):
10-
found = 0
11-
for line in (l.rstrip() for l in fileinput.input()):
12-
if match := URL_RE.search(line):
9+
def find_urls(lines, fpy=True, long=True):
10+
urls = []
11+
for line in lines:
12+
if match := URL_RE.search(line.rstrip()):
1313
url = match.groups()[0]
1414
is_fpy = '://fpy.li/' in url
1515
if (is_fpy and not fpy) or (not is_fpy and not long):
1616
continue
17-
print(url)
18-
found += 1
19-
# print('FOUND', found, 'URLs')
17+
urls.append(url)
18+
return urls
2019

2120

21+
def multi_find_urls(fpy=True, long=True):
22+
urls = find_urls(fileinput.input(), fpy, long)
23+
for url in urls:
24+
print(url)
25+
2226
if __name__ == '__main__':
23-
find_urls(fpy=False)
27+
multi_find_urls(fpy=False)

links/replace_urls.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,17 @@
22

33
import sys
44

5-
def main():
6-
pairs_name = sys.argv[1]
7-
adoc_name = sys.argv[2]
5+
def main(pairs_file, adoc_path):
86
pairs = []
9-
with open(pairs_name) as fp:
10-
for line in fp.readlines():
11-
pair = line.split()[-2:]
12-
assert len(pair) == 2, f'pair not found: {line}'
13-
assert pair[0].startswith('/'), f'no path: {line}'
14-
pairs.append(pair)
7+
for line in pairs_file.readlines():
8+
pair = line.split()[-2:]
9+
assert len(pair) == 2, f'pair not found: {line}'
10+
assert pair[0].startswith('/'), f'no path: {line}'
11+
pairs.append(pair)
1512

16-
assert len(pairs) > 0, f'no pairs found in {pairs_name}'
13+
assert len(pairs) > 0, f'no pairs found in {pairs_path}'
1714

18-
with open(adoc_name) as fp:
15+
with open(adoc_path) as fp:
1916
adoc = fp.read()
2017

2118
initial_adoc = adoc
@@ -25,18 +22,21 @@ def main():
2522
for path, url in pairs:
2623
if url in replaced:
2724
continue
28-
assert url in adoc, f'{url} not found in {adoc_name}'
25+
assert url in adoc, f'{url} not found in {adoc_path}'
2926
print(path, url)
30-
# append [ to get Asciidoc link syntax URLs, not explicit URLs in code etc.
27+
# append [ to match URLs in Asciidoc links `url[text]`, not URLs in code etc.
3128
short_url = f'https://fpy.li{path}['
3229
adoc = adoc.replace(url + '[', short_url)
3330
replaced.add(url)
3431

35-
assert len(initial_adoc) > len(adoc), f'{adoc_name}: {len(initial_adoc)=} {len(adoc)=}'
32+
assert len(initial_adoc) > len(adoc), f'{adoc_path}: {len(initial_adoc)=} {len(adoc)=}'
3633

37-
with open(adoc_name, 'w') as fp:
34+
with open(adoc_path, 'w') as fp:
3835
fp.write(adoc)
3936

4037

4138
if __name__ == '__main__':
42-
main()
39+
pairs_path = sys.argv[1]
40+
adoc_path = sys.argv[2]
41+
pairs_file = open(pairs_path)
42+
main(pairs_file, adoc_path)

links/shortener.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,14 @@ def gen_unused_short(redirects: dict) -> Iterator[str]:
156156
yield short
157157

158158

159-
def main():
160-
htaccess_path, urls_path = sys.argv[1:3]
159+
def main(htaccess_path, urls):
161160
with open(htaccess_path) as f:
162161
hta = f.read()
163162
assert 'RedirectTemp' in hta, 'No RedirecTemp in {htaccess_path}'
164-
with open(urls_path) as f:
165-
urls = [u.rstrip() for u in f.readlines()]
166163

167164
redirects, targets = load_redirects(parse_htaccess(hta))
168165
path_urls = []
166+
changes = []
169167
path_gen = gen_unused_short(redirects)
170168
for url in urls:
171169
if url in DO_NOT_SHORTEN:
@@ -174,12 +172,17 @@ def main():
174172
path_urls.append(path_url)
175173
path, url, new = path_url
176174
flag = '+' if new else '='
177-
print(f'{flag} /{path}\t{url}')
175+
line = f'{flag} /{path}\t{url}'
176+
print(line)
177+
changes.append(line)
178178

179179
with open(htaccess_path, 'a') as f:
180180
count = update_htaccess(f, path_urls)
181181
print(f'{count} directives appended to {htaccess_path}', file=sys.stderr)
182-
182+
return changes
183183

184184
if __name__ == '__main__':
185-
main()
185+
htaccess_path, urls_path = sys.argv[1:3]
186+
with open(urls_path) as f:
187+
urls = [u.rstrip() for u in f.readlines()]
188+
main(htaccess_path, urls)

online/cap13.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2095,7 +2095,7 @@ adiantada. Essa importação de `+__future__+` foi introduzida na
20952095
https://fpy.li/pep563[PEP 563—Postponed Evaluation of Annotations], implementada
20962096
no Python 3.7. Aquele comportamento estava marcado para se tornar default no
20972097
3.10, mas a mudança foi adiada para uma versão futura.footnote:[Leia a
2098-
https://fpy.li/13-32[decisão] (EN) de Python Steering Council no python-dev.]
2098+
https://fpy.li/13-32[decisão] do Python Steering Council na lista _python-dev_.]
20992099
Quando isso acontecer, a importação será redundante mas inofensiva.
21002100

21012101
Agora vamos criar—e depois estender—um novo protocolo estático.((("",

0 commit comments

Comments
 (0)