Skip to content

Commit ee4697f

Browse files
committed
add: Upload pictures to public image hosting server.
1 parent 1c3dc89 commit ee4697f

File tree

8 files changed

+164
-113
lines changed

8 files changed

+164
-113
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,11 @@ Commands:
4949
py Create python project scaffold.
5050
python Create python project scaffold.
5151
```
52+
53+
### Upload pictures to public image hosting server
54+
55+
```shell
56+
$ upsfortypora test.png
57+
Upload Success:
58+
http://dd-static.jd.com/ddimg/jfs/t1/132543/17/21538/145549/61fa87f9E883b9b32/f23efa3a806cab76.jpg
59+
```

image_hosting_service/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'''
2+
Date: 2022.02.02 14:10
3+
Description: Upload pictures to public image hosting server.
4+
LastEditors: Rustle Karl
5+
LastEditTime: 2022.02.02 14:10
6+
'''
7+
from .command import command_ups_for_typora

image_hosting_service/command.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Date: 2022.02.02 18:14
3+
Description: Omit
4+
LastEditors: Rustle Karl
5+
LastEditTime: 2022.02.02 18:14
6+
'''
7+
import os.path
8+
import sys
9+
10+
from .common import UploadResult, download_image
11+
from .jd import upload as upload_to_jd
12+
13+
14+
def upload_image(path: str) -> UploadResult:
15+
if path.startswith('http'):
16+
path = download_image(path)
17+
18+
if os.path.isfile(path):
19+
return upload_to_jd(path)
20+
21+
return UploadResult(message=f'file {path} not is file')
22+
23+
24+
def command_ups_for_typora():
25+
success_list = []
26+
27+
for path in sys.argv[1:]:
28+
result = upload_image(path)
29+
30+
if result.success:
31+
success_list.append(result.remote_url)
32+
else:
33+
success_list.append(result.message)
34+
35+
print('Upload Success:')
36+
print('\n'.join(success_list))

image_hosting_service/common.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'''
2+
Date: 2022.02.02 20:24
3+
Description: Omit
4+
LastEditors: Rustle Karl
5+
LastEditTime: 2022.02.02 20:24
6+
'''
7+
import os.path
8+
from dataclasses import dataclass
9+
from tempfile import gettempdir
10+
11+
import requests
12+
13+
headers = {
14+
'accept': 'application/json, text/javascript, */*; q=0.01',
15+
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 '
16+
'(KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36',
17+
'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,ca;q=0.7',
18+
}
19+
20+
21+
@dataclass
22+
class UploadResult(object):
23+
success: bool = False
24+
message: str = ''
25+
remote_url: str = ''
26+
27+
def __str__(self):
28+
return f'success={self.success}, message={self.message}, remote_url={self.remote_url}'
29+
30+
31+
def download_image(url: str) -> str:
32+
file = os.path.join(gettempdir(), os.path.basename(url))
33+
34+
with open(file, 'wb') as fp:
35+
fp.write(requests.get(url, headers=headers).content)
36+
37+
return file

image_hosting_service/jd.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
Date: 2022.02.02 19:56
3+
Description: Omit
4+
LastEditors: Rustle Karl
5+
LastEditTime: 2022.02.02 19:56
6+
'''
7+
import base64
8+
import json
9+
10+
import lxml.html
11+
import requests
12+
13+
from .common import UploadResult, headers
14+
15+
session = requests.Session()
16+
17+
session.headers = headers
18+
19+
20+
def upload(path) -> UploadResult:
21+
result = UploadResult()
22+
23+
response = session.post(
24+
'https://imio.jd.com/uploadfile/file/post.do',
25+
{
26+
'appId': 'im.customer',
27+
'clientType': 'comet',
28+
's': base64.b64encode(open(path, 'rb').read()),
29+
}
30+
)
31+
32+
if response.status_code == 200:
33+
html: lxml.html.HtmlElement = lxml.html.fromstring(response.content)
34+
body = json.loads(html.find('body').text)
35+
36+
if body['code'] == 0:
37+
result.success = True
38+
result.remote_url = body['path']
39+
40+
result.message = body['desc']
41+
42+
return result

image_hosting_service/upload_images.py

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

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
setuptools~=49.2.1
22
click~=8.0.3
3-
requests~=2.27.1
3+
requests~=2.27.1
4+
lxml~=4.6.3

setup.py

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'''
22
Date: 2020-09-21 23:48:26
33
LastEditors: Rustle Karl
4-
LastEditTime: 2021.05.22 20:32:35
4+
LastEditTime: 2022.02.02 17:43:15
55
'''
66
import os.path
77

@@ -18,45 +18,38 @@
1818
long_description = f.read()
1919

2020
setup(
21-
name='toolkit-py',
22-
version='0.1.10',
23-
url='https://github.com/fujiawei-dev/toolkit-py',
24-
keywords=['toolkit', 'toolset'],
25-
description='Personal toolkit implemented by Python.',
26-
long_description=long_description,
27-
long_description_content_type='text/markdown',
28-
author='White Turing',
29-
author_email='fujiawei@outlook.com',
30-
license='BSD',
31-
packages=find_packages(exclude=('tests', 'tests.*')),
32-
include_package_data=True,
33-
zip_safe=False,
21+
name='toolkit-py',
22+
version='0.2.0',
23+
url='https://github.com/fujiawei-dev/toolkit-py',
24+
keywords=['toolkit', 'toolset'],
25+
description='Personal toolkit implemented by Python.',
26+
long_description=long_description,
27+
long_description_content_type='text/markdown',
28+
author='White Turing',
29+
author_email='fujiawei@outlook.com',
30+
license='BSD',
31+
packages=find_packages(exclude=('tests', 'tests.*')),
32+
include_package_data=True,
33+
zip_safe=False,
34+
install_requires=requires,
3435

35-
# 必须附带的数据文件
36-
data_files=[('scripts',
37-
["scripts/user_agent/static/android_build.json",
38-
"scripts/user_agent/static/android_dev.json",
39-
"scripts/user_agent/static/ios.json",
40-
"scripts/user_agent/static/opera_build.json", ])],
36+
entry_points={
37+
'console_scripts': [
38+
'gua=user_agent:command_gua',
39+
'cfm=open_source_mirror:command_cfm',
40+
'cps=project_scaffold:command_cps',
41+
'upsfortypora=image_hosting_service:command_ups_for_typora',
42+
],
43+
},
4144

42-
entry_points={
43-
'console_scripts': [
44-
'rpd = scripts:script_rpd',
45-
'gua = scripts:script_gua',
46-
'chs = scripts:script_chs',
47-
'kieng = scripts:script_kieng',
45+
classifiers=[
46+
'Intended Audience :: Developers',
47+
'Environment :: Console',
48+
'License :: OSI Approved :: BSD License',
49+
'Operating System :: OS Independent',
50+
'Programming Language :: Python :: 3',
51+
'Programming Language :: Python :: Implementation :: CPython',
52+
'Programming Language :: Python :: Implementation :: PyPy',
53+
'Topic :: Software Development :: Libraries :: Python Modules',
4854
],
49-
},
50-
51-
classifiers=[
52-
'Intended Audience :: Developers',
53-
'Environment :: Console',
54-
'License :: OSI Approved :: BSD License',
55-
'Operating System :: OS Independent',
56-
'Programming Language :: Python :: 3',
57-
'Programming Language :: Python :: Implementation :: CPython',
58-
'Programming Language :: Python :: Implementation :: PyPy',
59-
'Topic :: Software Development :: Libraries :: Python Modules',
60-
],
61-
install_requires=requires,
6255
)

0 commit comments

Comments
 (0)