-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_uploader.py
66 lines (61 loc) · 1.8 KB
/
image_uploader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3
import sys
import requests
import json
import uuid
import base64
from pprint import pprint
from pathlib import Path
def read_json( file_path ):
with open( file_path ) as f:
return json.load( f )
def write_text( file_path , text_lines_list ):
#with open( file_path , 'a', encoding='utf-8' ) as f:
with open( file_path , 'w', encoding='utf-8' ) as f:
f.writelines( text_lines_list )
def upload_file_to_imgur( imgur_client_id , file_path ):
response = requests.post(
"https://api.imgur.com/3/upload.json" ,
headers={ "Authorization": f"Client-ID {imgur_client_id}" } ,
data={
"image": base64.b64encode( open( file_path , "rb" ).read() ) ,
"type": "base64" ,
"name": Path( file_path ).stem ,
}
)
response.raise_for_status()
data = response.json()
# pprint( data )
if "data" not in data:
return
if "link" not in data["data"]:
return
print( data["data"]["link"] )
def upload_url_to_imgur( imgur_client_id , image_url ):
imgur_client_id = read_json( Path.home().joinpath( ".config" , "personal" , "imgur_uploader.json" ) )[ "client_id" ]
response = requests.post(
"https://api.imgur.com/3/upload.json" ,
headers={ "Authorization": f"Client-ID {imgur_client_id}" } ,
data={
"image": image_url ,
"type": "URL" ,
"name": uuid.uuid4().hex[:10] ,
}
)
response.raise_for_status()
data = response.json()
# pprint( data )
if "data" not in data:
return
if "link" not in data["data"]:
return
print( data["data"]["link"] )
def upload( input_path , config ):
global Personal
with open( input_path , "rb" ) as upload_file:
file_list = { "file": upload_file }
headers = { 'key': config["key"] }
response = requests.post( config[ "endpoint_url" ] , headers=headers , files=file_list )
response.raise_for_status()
print( response.text )
return response.text