I tried to write the code using requests and some help from a github page but I couldn't succeed.
import sys
if len(sys.argv) < 2:
print("You must specify a file name.")
sys.exit(-1)
filePath = sys.argv[1]
with open(filePath , 'rb') as f:
print(
requests.post(
'http://telegra.ph/upload',
files={'file': ('file', f, 'image/jpeg')} # image/gif, image/jpeg, image/jpg, image/png, video/mp4
).json()
)
It gives me the following error {'error': 'No files passed'}
CodePudding user response:
Here is a working solution, Hope this helps.
import requests
import sys
# entry point
if __name__ == '__main__':
if len(sys.argv) < 2:
print("You must specify a file name.")
sys.exit(-1)
filePath = sys.argv[1]
with open(filePath, 'rb') as f:
# Upload File
json_data = requests.post('https://telegra.ph/upload', files={'file': ('file', f, 'image/jpeg')}).json() # image/gif, image/jpeg, image/jpg, image/png, video/mp4
# get file URL
filename = "https://telegra.ph" json_data[0]['src']
print(filename)
usage
C:\Projects\other>python test.py "C:\Projects\other\test.jpg"
https://telegra.ph/file/cb0f19a99be9c35bf6ae1.jpg
C:\Projects\other>