Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitec03da7

Browse files
committed
short.py appends timestamps to short.htaccesss
1 parent5b743b5 commitec03da7

File tree

2 files changed

+26
-22
lines changed

2 files changed

+26
-22
lines changed

‎links/short.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
This script reads lines from stdin or files named as arguments, then:
77
88
1. retrieves or creates new short URLs, taking into account existing RedirectTemp
9-
directives in custom.htacess or short.htacess;
10-
2. appends RedirectTemp directives for newly created short URLs to short.htacess;
9+
directives in custom.htaccess or short.htaccess;
10+
2. appends RedirectTemp directives for newly created short URLs to short.htaccess;
1111
3. outputs the list of (short, long) URLs retrieved or created.
1212
1313
"""
@@ -17,21 +17,25 @@
1717
fromcollections.abcimportIterator
1818
fromtimeimportstrftime
1919

20+
HTACCESS_CUSTOM='custom.htaccess'
21+
HTACCESS_SHORT='short.htaccess'
22+
HTACCESS_FILES= (HTACCESS_CUSTOM,HTACCESS_SHORT)
2023
BASE_DOMAIN='fpy.li'
2124

22-
defload_redirects():
25+
26+
defload_redirects()->tuple[dict,dict]:
2327
redirects= {}
2428
targets= {}
25-
forfilenamein('custom.htaccess','short.htaccess'):
29+
forfilenameinHTACCESS_FILES:
2630
withopen(filename)asfp:
2731
forlineinfp:
2832
ifline.startswith('RedirectTemp'):
2933
_,short,long=line.split()
3034
short=short[1:]# Remove leading slash
31-
assertshortnotinredirects,f"{filename}: duplicate redirect from{short}"
32-
# custom is live since 2022, wecannot change it remove duplicate targets
33-
ifnotfilename.startswith('custom'):
34-
assertlongnotintargets,f"{filename}:Duplicate redirect to{long}"
35+
assertshortnotinredirects,f'{filename}: duplicate redirect from{short}'
36+
#htaccess.custom is live since 2022, wecan't change it remove duplicate targets
37+
iffilename!=HTACCESS_CUSTOM:
38+
assertlongnotintargets,f'{filename}:duplicate redirect to{long}'
3539
redirects[short]=long
3640
targets[long]=short
3741
returnredirects,targets
@@ -41,9 +45,7 @@ def load_redirects():
4145

4246

4347
defgen_short(start_len=1)->Iterator[str]:
44-
"""
45-
Generate every possible sequence of SDIGITS, starting with start_len
46-
"""
48+
"""Generate every possible sequence of SDIGITS, starting with start_len"""
4749
length=start_len
4850
whileTrue:
4951
forshortinitertools.product(SDIGITS,repeat=length):
@@ -52,22 +54,20 @@ def gen_short(start_len=1) -> Iterator[str]:
5254

5355

5456
defgen_unused_short(redirects:dict)->Iterator[str]:
55-
"""
56-
Generate next available short URL of len >= 2.
57-
"""
57+
"""Generate next available short URL of len >= 2."""
5858
forshortingen_short(2):
5959
ifshortnotinredirects:
6060
yieldshort
6161

6262

63-
defshorten(urls:list[str],redirects:dict,targets:dict)->list[tuple[str,str]]:
64-
"""return (short, long) pairs, appending directives toshort.htaccess as needed"""
63+
defshorten(urls:list[str],redirects:dict,targets:dict)->list[tuple[str,str]]:
64+
"""Return (short, long) pairs, appending directives toHTACCESS_SHORT as needed."""
6565
iter_short=gen_unused_short(redirects)
6666
pairs= []
6767
timestamp=strftime('%Y-%m-%d %H:%M:%S')
68-
withopen('short.htaccess','a')asfp:
68+
withopen(HTACCESS_SHORT,'a')asfp:
6969
forlonginurls:
70-
assertBASE_DOMAINnotinlong,f"{long} is a{BASE_DOMAIN} URL"
70+
assertBASE_DOMAINnotinlong,f'{long} is a{BASE_DOMAIN} URL'
7171
iflongintargets:
7272
short=targets[long]
7373
else:
@@ -79,16 +79,16 @@ def shorten(urls: list[str], redirects: dict, targets: dict) -> list[tuple[str,s
7979
timestamp=None
8080
fp.write(f'RedirectTemp /{short}{long}\n')
8181
pairs.append((short,long))
82-
82+
8383
returnpairs
8484

8585

86-
defmain():
86+
defmain()->None:
8787
"""read URLS from filename arguments or stdin"""
88-
urls= [line.strip()forlineinfileinput.input(encoding="utf-8")]
88+
urls= [line.strip()forlineinfileinput.input(encoding='utf-8')]
8989
redirects,targets=load_redirects()
9090
forshort,longinshorten(urls,redirects,targets):
91-
print(f'{BASE_DOMAIN}/{short}\t{long}')
91+
print(f'{BASE_DOMAIN}/{short}\t{long}')
9292

9393

9494
if__name__=='__main__':

‎ruff.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
line-length =100
2+
[format]
3+
# Like Python's repr(), use single quotes for strings.
4+
quote-style ="single"

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp