Skip to content

Commit 1325504

Browse files
Merge pull request #6 from danila-schelkov/replace-requests
refactor: requests module replaced with built-in urllib
2 parents 7f6fc92 + 00ae2c4 commit 1325504

File tree

6 files changed

+32
-46
lines changed

6 files changed

+32
-46
lines changed

main.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# Refactored by Vorono4ka
2-
# Finished ~99%
32

43
import time
54

@@ -9,7 +8,14 @@
98
raise RuntimeError("Please, install loguru using pip")
109

1110
from system import clear
12-
from system.lib import config, locale, refill_menu, menu
11+
from system.lib import (
12+
config,
13+
locale,
14+
refill_menu,
15+
menu,
16+
check_auto_update,
17+
check_files_updated,
18+
)
1319
from system.lib.features.initialization import initialize
1420

1521

@@ -21,6 +27,9 @@ def main():
2127
initialize(True)
2228
exit()
2329

30+
check_auto_update()
31+
check_files_updated()
32+
2433
refill_menu()
2534

2635
while True:

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
sc-compression
2-
requests
32
colorama
43
pylzham
54
zstandard

system/lib/__init__.py

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,14 @@
3333
locale.load(config.language)
3434

3535

36-
try:
37-
# noinspection PyUnresolvedReferences
38-
import requests
39-
40-
del requests
41-
36+
def check_auto_update():
4237
if config.auto_update and time.time() - config.last_update > 60 * 60 * 24 * 7:
4338
check_update()
4439
config.last_update = int(time.time())
4540
config.dump()
4641

42+
43+
def check_files_updated():
4744
if config.has_update:
4845
logger.opt(colors=True).info(f'<green>{locale.update_done % ""}</green>')
4946
if Console.question(locale.done_qu):
@@ -56,8 +53,6 @@
5653
config.dump()
5754
else:
5855
exit()
59-
except ImportError:
60-
pass
6156

6257

6358
# noinspection PyUnresolvedReferences
@@ -131,19 +126,9 @@ def refill_menu():
131126
logger.warning(locale.install_to_unlock % "sc-compression")
132127

133128
other = Menu.Category(10, locale.other_features_label)
134-
try:
135-
import requests
136-
137-
del requests
138-
139-
other.add(
140-
Menu.Item(
141-
locale.check_update, locale.version % config.version, check_update
142-
)
143-
)
144-
except ImportError:
145-
logger.warning(locale.install_to_unlock % "requests")
146-
129+
other.add(
130+
Menu.Item(locale.check_update, locale.version % config.version, check_update)
131+
)
147132
other.add(Menu.Item(locale.check_for_outdated, None, check_for_outdated))
148133
other.add(
149134
Menu.Item(

system/lib/features/initialization.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,7 @@ def initialize(first_init=False):
4747
logger.info(locale.verifying)
4848

4949
config.initialized = True
50-
try:
51-
# noinspection PyUnresolvedReferences
52-
import requests
53-
54-
del requests
55-
config.version = get_tags("vorono4ka", "xcoder")[0]["name"][1:]
56-
except ImportError as exception:
57-
logger.exception(exception)
50+
config.version = get_tags("vorono4ka", "xcoder")[0]["name"][1:]
5851
config.dump()
5952

6053
if first_init:

system/lib/features/update/check.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import os
23

34
from loguru import logger
@@ -38,21 +39,20 @@ def get_pip_info(outdated: bool = False) -> list:
3839

3940
def get_tags(owner: str, repo: str):
4041
api_url = "https://api.github.com"
41-
tags = []
4242

43-
import requests
43+
import urllib.request
4444

45-
try:
46-
tags = requests.get(
45+
tags = json.loads(
46+
urllib.request.urlopen(
4747
api_url + "/repos/{owner}/{repo}/tags".format(owner=owner, repo=repo)
48-
).json()
49-
tags = [
50-
{key: v for key, v in tag.items() if key in ["name", "zipball_url"]}
51-
for tag in tags
52-
]
53-
except Exception:
54-
pass
55-
del requests
48+
)
49+
.read()
50+
.decode()
51+
)
52+
tags = [
53+
{key: v for key, v in tag.items() if key in ["name", "zipball_url"]}
54+
for tag in tags
55+
]
5656

5757
return tags
5858

system/lib/features/update/download.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ def download_update(zip_url):
2222
os.mkdir("updates")
2323

2424
try:
25-
import requests
25+
import urllib.request
2626

2727
with open("updates/update.zip", "wb") as f:
28-
f.write(requests.get(zip_url).content)
28+
f.write(urllib.request.urlopen(zip_url).read())
2929
f.close()
3030

3131
import zipfile

0 commit comments

Comments
 (0)