reykit 1.1.73__py3-none-any.whl → 1.1.75__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
reykit/rnet.py CHANGED
@@ -31,8 +31,8 @@ from filetype import guess as filetype_guess
31
31
  from datetime import datetime
32
32
 
33
33
  from .rbase import Base, throw, check_response_code
34
- from .ros import File
35
- from .rre import search
34
+ from .ros import File, get_md5
35
+ from .rre import search, split
36
36
 
37
37
 
38
38
  __all__ = (
@@ -42,6 +42,7 @@ __all__ = (
42
42
  'split_cookie',
43
43
  'get_content_type',
44
44
  'request',
45
+ 'get_response_file_name',
45
46
  'download',
46
47
  'compute_stream_time',
47
48
  'listen_socket',
@@ -402,6 +403,52 @@ def request(
402
403
  return response
403
404
 
404
405
 
406
+ def get_response_file_name(response: Response, default_name: str | None = None) -> str:
407
+ """
408
+ Get file name from response.
409
+
410
+ Parameters
411
+ ----------
412
+ response : `Response` instance.
413
+ default_name : Default file name.
414
+ - `None`: Use MD5 value join automatic judge file type.
415
+
416
+ Returns
417
+ -------
418
+ File name.
419
+ """
420
+
421
+ # Handle parameter.
422
+ content = response.content
423
+
424
+ # Get.
425
+ file_name = None
426
+ Content_disposition = response.headers.get('Content-Disposition', '')
427
+ if 'filename' in Content_disposition:
428
+ file_name: str | None = search(
429
+ 'filename=[\'"]?([^\\s\'"]+)',
430
+ Content_disposition
431
+ )
432
+ if file_name is None:
433
+ pattern = r'[/\\]'
434
+ url_parts = split(pattern, response.request.url)
435
+ if (
436
+ len(url_parts) != 1
437
+ and '.' in url_parts[-1]
438
+ ):
439
+ file_name = url_parts[-1]
440
+ if file_name is None:
441
+ if default_name is None:
442
+ default_name = get_md5(content)
443
+ file_type_obj = filetype_guess(content)
444
+ if file_type_obj is not None:
445
+ default_name += f'.{file_type_obj.EXTENSION}'
446
+ file_name = f'{default_name}.' + file_type_obj.EXTENSION
447
+ file_name = default_name
448
+
449
+ return file_name
450
+
451
+
405
452
  def download(url: str, path: str | None = None) -> str:
406
453
  """
407
454
  Download file from URL.
@@ -410,7 +457,7 @@ def download(url: str, path: str | None = None) -> str:
410
457
  ----------
411
458
  url : Download URL.
412
459
  path : Save path.
413
- - `None`: File name is get from response, or is 'download' join automatic judge file type.
460
+ - `None`: File name use MD5 value join automatic judge file type.
414
461
 
415
462
  Returns
416
463
  -------
@@ -419,28 +466,15 @@ def download(url: str, path: str | None = None) -> str:
419
466
 
420
467
  # Download.
421
468
  response = request(url)
422
- content = response.content
423
469
 
424
470
  # File name.
425
471
  if path is None:
426
- file_name = None
427
- Content_disposition = response.headers.get('Content-Disposition', '')
428
- if 'filename' in Content_disposition:
429
- file_name: str | None = search(
430
- 'filename=[\'"]?([^\\s\'"]+)',
431
- Content_disposition
432
- )
433
- if file_name is None:
434
- file_type_obj = filetype_guess(content)
435
- if file_type_obj is not None:
436
- file_name = 'download.' + file_type_obj.EXTENSION
437
- else:
438
- file_name = 'download'
472
+ file_name = get_response_file_name(response)
439
473
  path = os_abspath(file_name)
440
474
 
441
475
  # Save.
442
476
  file = File(path)
443
- file(content)
477
+ file(response.content)
444
478
 
445
479
  return path
446
480
 
reykit/ros.py CHANGED
@@ -2078,7 +2078,7 @@ class FileCache(Base):
2078
2078
  ----------
2079
2079
  url : Download URL.
2080
2080
  name : File name.
2081
- - `None`: Get from response, or is MD5 value join automatic judge file type.
2081
+ - `None`: Use MD5 value join automatic judge file type.
2082
2082
 
2083
2083
  Returns
2084
2084
  -------
@@ -2086,30 +2086,17 @@ class FileCache(Base):
2086
2086
  """
2087
2087
 
2088
2088
  # Import.
2089
- from .rnet import request
2089
+ from .rnet import request, get_response_file_name
2090
2090
 
2091
2091
  # Download.
2092
2092
  response = request(url)
2093
- content = response.content
2094
2093
 
2095
2094
  # File name.
2096
2095
  if name is None:
2097
- Content_disposition = response.headers.get('Content-Disposition', '')
2098
- if 'filename' in Content_disposition:
2099
- name: str | None = search(
2100
- 'filename=[\'"]?([^\\s\'"]+)',
2101
- Content_disposition
2102
- )
2103
- if name is None:
2104
- file_md5 = get_md5(content)
2105
- file_type_obj = filetype_guess(content)
2106
- if file_type_obj is not None:
2107
- name = f'{file_md5}.{file_type_obj.EXTENSION}'
2108
- else:
2109
- name = file_md5
2096
+ name = get_response_file_name(response)
2110
2097
 
2111
2098
  # Store.
2112
- path = self.store(content, name)
2099
+ path = self.store(response.content, name)
2113
2100
 
2114
2101
  return path
2115
2102
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: reykit
3
- Version: 1.1.73
3
+ Version: 1.1.75
4
4
  Summary: Kit method set.
5
5
  Project-URL: homepage, https://github.com/reyxbo/reykit/
6
6
  Author-email: Rey <reyxbo@163.com>
@@ -6,9 +6,9 @@ reykit/remail.py,sha256=l4HGKXdfHNBxyBT3YxeZyQhfecbElqTqSAGInwWhap8,6723
6
6
  reykit/rimage.py,sha256=lNN2iMpvSMqh-nPTpxrA9yHy43EA5WoYdxKYhqPwMgk,6154
7
7
  reykit/rlog.py,sha256=TRAWaVG9KTgzeNjN-FXkcvBmvq1IhICgawllQEGoUdg,25745
8
8
  reykit/rmonkey.py,sha256=Dj2GBzBDFXbo0Z-5f8Zep4dfbaIw1bo1FUmC31xvDuk,7929
9
- reykit/rnet.py,sha256=rKCrIlQnU8yAEOIbnVz6NPKBOWb_RR2vFU1ibvNsxZo,16912
9
+ reykit/rnet.py,sha256=ql1L-QCoa_nZ2Vp8cpZrWl4AoxfZq9dhPdistiAdM64,17767
10
10
  reykit/rnum.py,sha256=PhG4V_BkVfCJUsbpMDN1umGZly1Hsus80TW8bpyBtyY,3653
11
- reykit/ros.py,sha256=ej3sQf5PZqUXWNIWFprV3TAiHffAQ51dSWf1DJUgzMw,48220
11
+ reykit/ros.py,sha256=8ok5ZrhKMVSWUtHgmDq7eOFevc9_M9IcA9lCqPclneo,47652
12
12
  reykit/rrand.py,sha256=4VwooITgox54_GonELcJfcIpStDi-UJchpnyWKnyeIA,8606
13
13
  reykit/rre.py,sha256=1qva7xatKVE9qC2j7IujjXSM59qxHWwTYpiizFFQ8Xo,6024
14
14
  reykit/rschedule.py,sha256=HuQRSNF6yd397P9knIX9-z8Ii1tUXFUL_-zxTIkxir0,14804
@@ -22,7 +22,7 @@ reykit/rwrap.py,sha256=FEmeK_fboJ-OyXeJf8bilc7U2ph8xIbZGNHb6fLCy2c,15063
22
22
  reykit/rzip.py,sha256=BGEONswuBZxQ-zcgd_xp2fcvYesC9AmKaaXWvnT3bTI,3456
23
23
  reykit/rdll/__init__.py,sha256=nLSb8onBm2ilyoxzpDzUeGfSCKwkLEesIhzK3LiJ8mk,701
24
24
  reykit/rdll/rdll_core.py,sha256=o6-rKcTQgxZQe0kD3GnwyNb3KL9IogzgCQNOmYLMm7A,5086
25
- reykit-1.1.73.dist-info/METADATA,sha256=U59vJYIz0jm08eMamUgrwctejLSQyW6oIcCwFQ00oO4,1872
26
- reykit-1.1.73.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
27
- reykit-1.1.73.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
28
- reykit-1.1.73.dist-info/RECORD,,
25
+ reykit-1.1.75.dist-info/METADATA,sha256=G7l2GpQpAgoT0wO_HOuAfvCj2lkorviX-enuViPEotA,1872
26
+ reykit-1.1.75.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
27
+ reykit-1.1.75.dist-info/licenses/LICENSE,sha256=UYLPqp7BvPiH8yEZduJqmmyEl6hlM3lKrFIefiD4rvk,1059
28
+ reykit-1.1.75.dist-info/RECORD,,