-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathuploader.py
More file actions
76 lines (56 loc) · 1.81 KB
/
uploader.py
File metadata and controls
76 lines (56 loc) · 1.81 KB
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
67
68
69
70
71
72
73
74
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
import sys
import helper
import commands
import hashlib
import time
class Uploader:
__file = None
project_path = ''
github_username = ''
github_repo = ''
# default master branch
# __MARKDOWN_IMG_URL = '';
__MARKDOWN_IMG_URL = '';
def __init__(self, file):
self.__file = file
self.project_path = os.environ.get('project_path')
self.github_username = os.environ.get('github_username')
self.github_repo = os.environ.get('github_repo')
self.run()
def run(self):
helper.notify('Uploading','Please wait for a while')
# Image suffix
a,b,suffix = self.__file.filename.rpartition('.')
# Get image
filename = str(hashlib.md5(self.__file.filename).hexdigest())+str(int(time.time()))+'.'+suffix
self.__file.save(self.project_path+'/'+filename)
# Git
cmd = '''
cd {}
git add .
git commit -m 'clipboard'
git push'''.format(self.project_path)
a,b = commands.getstatusoutput(cmd)
if a == 0:
self.__write_to_doc(filename)
helper.notify('Success','Upload success')
else:
# Alfred workflow debugger console
sys.stderr.write(str(b))
helper.notify('Error','Git error')
def __write_to_doc(self, filename):
remote_url = self.__MARKDOWN_IMG_URL.format(filename,self.github_username,self.github_repo,filename)
os.system('echo "{}"|pbcopy'.format(remote_url))
a,b = commands.getstatusoutput('pbpaste')
self.print_pasteboard_content()
# this func is forked from `kaito-kidd/markdown-image-alfred` thanks
def print_pasteboard_content(self):
"""从剪贴板打印出内容"""
write_command = (
'osascript -e \'tell application '
'"System Events" to keystroke "v" using command down\''
)
os.system(write_command)