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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
import os, time, sys, configparser, platform, urllib.parse, qiniu, pyperclip, signal, threading from mimetypes import MimeTypes from os.path import expanduser from watchdog.observers import Observer from watchdog.events import PatternMatchingEventHandler
def job(file, mode): if mode == 1: url = upload_with_full_Path(file) if mode == 2: url = upload_with_full_Path_cmd(file) pyperclip.copy(url) pyperclip.paste() print(url) with open('MARKDOWN_FORMAT_URLS.txt', 'a') as f: image = '![' + url + ']' + '(' + url + ')' + '\n' f.write(image + '\n')
homedir = expanduser("~") config = configparser.RawConfigParser() config.read(homedir + '/qiniu.cfg') mime = MimeTypes() threadLock = threading.Lock()
def exit_gracefully(signum, frame): signal.signal(signal.SIGINT, original_sigint) try: if input("\nReally quit? (y/n)> ").lower().startswith('y'): sys.exit(1) except KeyboardInterrupt: print("Ok ok, quitting") sys.exit(1) signal.signal(signal.SIGINT, exit_gracefully)
original_sigint = signal.getsignal(signal.SIGINT) signal.signal(signal.SIGINT, exit_gracefully)
try: bucket = config.get('config', 'bucket') accessKey = config.get('config', 'accessKey') secretKey = config.get('config', 'secretKey') path_to_watch = config.get('config', 'path_to_watch') enable = config.get('custom_url', 'enable')
if enable == 'false': print('custom_url not set') else: addr = config.get('custom_url', 'addr') except configparser.NoSectionError as err: print ('Error Config File:', err)
def parseRet(retData, respInfo): if retData != None: for k, v in retData.items(): if k[:2] == "x:": print(k + ":" + v) for k, v in retData.items(): if k[:2] == "x:" or k == "hash" or k == "key": continue else: print(k + ":" + str(v)) else: print("Upload file failed!")
def upload_without_key(bucket, filePath, uploadname): auth = qiniu.Auth(accessKey, secretKey) upToken = auth.upload_token(bucket, key=None) key = uploadname retData, respInfo = qiniu.put_file(upToken, key, filePath, mime_type=mime.guess_type(filePath)[0]) parseRet(retData, respInfo)
def upload_with_full_Path(filePath): if platform.system() == 'Windows': fileName = "/".join("".join(filePath.rsplit(path_to_watch))[1:].split("\\")) else: fileName = "".join(filePath.rsplit(path_to_watch))[1:] upload_without_key(bucket, filePath, fileName) if enable == 'true': return addr + '/' + urllib.parse.quote(fileName) else: return 'http://' + bucket + '.qiniudn.com/' + urllib.parse.quote(fileName)
def upload_with_full_Path_cmd(filePath): if platform.system() == 'Windows': fileName = os.path.basename("/".join((filePath.split("\\")))) else: fileName = os.path.basename(filePath) upload_without_key(bucket, filePath, fileName) if enable == 'true': return addr + urllib.parse.quote(fileName) else: return 'http://' + bucket + '.qiniudn.com/' + urllib.parse.quote(fileName)
def get_filepaths(directory): file_paths = [] for root, directories, files in os.walk(directory): for filename in files: filepath = os.path.join(root, filename) file_paths.append(filepath) return file_paths
def set_clipboard(image_list): markdown_photo = "" for img in image_list: markdown_photo = markdown_photo + img pyperclip.copy(markdown_photo) spam = pyperclip.paste()
def window_main(): if len(sys.argv) > 1: url_list = [] image_list = [] for i in sys.argv[1:]: url_list.append(upload_with_full_Path_cmd(i)) for url in url_list: image = '![' + url + ']' + '(' + url + ')' + '\n' print(url, '\n') image_list.append(image) set_clipboard(image_list) sys.exit(-1) print("running ... ... \nPress Ctr+C to Stop") before = get_filepaths(path_to_watch) while 1: time.sleep(1) after = get_filepaths(path_to_watch) added = [f for f in after if not f in before] removed = [f for f in before if not f in after] if added: url_list = [] image_list = [] for i in added: url_list.append(upload_with_full_Path(i)) for url in url_list: image = '![' + url + ']' + '(' + url + ')' +'\n' image_list .append(image) print(url, '\n') set_clipboard(image_list) if removed: pass before = after
def main(): if os.name == 'nt' or platform.system() == 'Windows': window_main() else: print("Windows only!")
if __name__ == "__main__": main()
|