Skip to content

Commit a92bf15

Browse files
committed
revised to use pwiki
1 parent 355a3a5 commit a92bf15

File tree

6 files changed

+35
-258
lines changed

6 files changed

+35
-258
lines changed

requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
Pillow==7.2.0
2-
requests==2.24.0
1+
Pillow==8.1.0
2+
requests==2.25.1
3+
rich==9.6.2

scu/__init__.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
from .wiki import *
2-
from .wgen import *

scu/__main__.py

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,76 @@
11
import argparse
22
import getpass
3+
import logging
34
import re
5+
46
from datetime import date, datetime
57
from pathlib import Path
68
from textwrap import dedent
79

810
from PIL import ExifTags, Image
11+
from rich.logging import RichHandler
12+
13+
from pwiki import wgen
14+
from pwiki.wiki import Wiki
915

10-
from scu.wiki import Wiki, ColorLog
11-
from scu.wgen import Wgen
16+
log = logging.getLogger(__name__)
1217

18+
MTC_FILE = Path.home() / ".scu.px.txt"
1319

14-
def main():
15-
MTC_FILE = Path.home() / ".scu.px.txt"
1620

21+
def _main():
1722
cli_parser = argparse.ArgumentParser(description="Simple Commons Uploader")
18-
cli_parser.add_argument('dirs', metavar='folders', type=str, nargs='*', help='folders with files to upload')
1923
cli_parser.add_argument('--user', type=str, help="username to use")
2024
cli_parser.add_argument('--pw', type=str, help="password to use")
2125
cli_parser.add_argument("-i", action='store_true', help="force interactive login")
2226
cli_parser.add_argument("--wgen", action='store_true', help="run wgen password manager")
27+
cli_parser.add_argument('dirs', metavar='folders', type=Path, nargs='*', help='folders with files to upload')
2328
args = cli_parser.parse_args()
2429

2530
if args.wgen:
26-
Wgen.setup(MTC_FILE, False)
31+
wgen.setup_px(MTC_FILE, False)
2732
return
2833

2934
wiki = Wiki("commons.wikimedia.org")
3035
if args.i:
3136
wiki.login(input("Please login to continue.\nUsername: "), getpass.getpass())
3237
elif args.user:
3338
if not args.pw:
34-
ColorLog.error("You didn't specify a password, --pw")
39+
log.critical("No password specified, please pass the --pw flag with a pasword.")
3540
return
3641
wiki.login(args.user, args.pw)
3742
elif MTC_FILE.is_file():
38-
pxd = Wgen.load_px(MTC_FILE)
39-
if not pxd:
40-
ColorLog.error(f"Please run with --wgen option or remove {MTC_FILE} from your home directory")
41-
wiki.login(*pxd.popitem())
43+
wiki.login(*wgen.load_px(MTC_FILE).popitem())
4244
else:
4345
cli_parser.print_help()
4446
return
4547

4648
if not args.dirs:
47-
ColorLog.warn("You didn't specify and directories to upload! Goodbye.")
49+
log.critical("You didn't specify and directories to upload!")
4850
return
4951

50-
ext_list = {"." + e for e in wiki.acceptable_file_extensions()}
52+
ext_list = {"." + e for e in wiki.uploadable_filetypes()}
53+
fails = []
5154

52-
for d in args.dirs:
53-
base_dir = Path(d)
55+
for base_dir in args.dirs:
5456
if not base_dir.is_dir():
5557
continue
5658

5759
i = 1
58-
fails = []
5960
for f in base_dir.iterdir():
6061
ext = f.suffix.lower()
61-
if not f.is_file() or not ext in ext_list:
62+
if not f.is_file() or ext not in ext_list:
6263
continue
6364

6465
timestamp = None
6566
if ext in (".jpg", ".jpeg"):
6667
try:
6768
with Image.open(f) as img:
68-
exif = {ExifTags.TAGS[k]: v for k, v in img._getexif().items() if k in ExifTags.TAGS}
69-
if "DateTimeOriginal" in exif:
70-
dto = exif["DateTimeOriginal"]
71-
72-
if re.match(r"\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}", dto):
69+
if dto := next((v for k, v in img.getexif().items() if ExifTags.TAGS.get(k) == "DateTimeOriginal" and re.match(r"\d{4}:\d{2}:\d{2} \d{2}:\d{2}:\d{2}", v)), None):
7370
d, t = dto.split()
74-
timestamp = d.replace(":", "-") + " " + t
71+
timestamp = f"{d.replace(':', '-')} {t}"
7572
except Exception as e:
76-
ColorLog.warn(f"Could not parse EXIF for {f}: {e}")
73+
log.warn("Could not parse EXIF for %s", f, exc_info=True)
7774

7875
desc = f"""\
7976
=={{{{int:filedesc}}}}==
@@ -90,17 +87,19 @@ def main():
9087
[[Category:{base_dir.name}]]
9188
[[Category:Files by {wiki.username}]]"""
9289

93-
if not wiki.upload(str(f), f"{base_dir.name} {i} {date.today()}{ext}", dedent(desc), ""):
90+
if not wiki.upload(f, f"{base_dir.name} {i} {date.today()}{ext}", dedent(desc)):
9491
fails.append(f)
9592
i += 1
9693

9794
if fails:
98-
print("Failed to upload:")
99-
for f in fails:
100-
print(f)
95+
log.warn("Failed to upload %d files: %s", len(fails), fails)
10196
else:
102-
print("Complete, with no failures")
97+
log.info("Finished with no failures")
10398

10499

105100
if __name__ == '__main__':
106-
main()
101+
for lg in (logging.getLogger("pwiki"), log):
102+
lg.addHandler(RichHandler(rich_tracebacks=True))
103+
lg.setLevel("INFO")
104+
105+
_main()

scu/wgen.py

Lines changed: 0 additions & 53 deletions
This file was deleted.

scu/wiki.py

Lines changed: 0 additions & 167 deletions
This file was deleted.

setup.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
url="https://github.com/fastily/simple-commons-uploader",
1515
include_package_data=True,
1616
packages=setuptools.find_packages(),
17-
install_requires=['Pillow', 'requests'],
17+
install_requires=['Pillow', 'requests', 'rich'],
1818
entry_points={
1919
'console_scripts': [
2020
'scu = scu.__main__:main'
@@ -23,11 +23,10 @@
2323
classifiers=[
2424
"Natural Language :: English",
2525
"Programming Language :: Python :: 3",
26-
"Programming Language :: Python :: 3.6",
27-
"Programming Language :: Python :: 3.7",
2826
"Programming Language :: Python :: 3.8",
27+
"Programming Language :: Python :: 3.9",
2928
"License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
3029
"Operating System :: OS Independent",
3130
],
32-
python_requires='>=3.6',
31+
python_requires='>=3.8',
3332
)

0 commit comments

Comments
 (0)