1- from PIL import ImageGrab , ImageFile
2- from configparser import ConfigParser
3- from uploader .QiniuUploader import QiniuUploader
4- import time
1+ from ImageGrab import ImageGrab
2+ from uploader .SmUploader import Uploader
53import sys
64import os
7-
8- try :
9- import pythoncom
10- except ImportError :
11- print ('maybe you have not install pywin32 or not in windows system.' )
12- pythoncom = None
135try :
14- import pyHook
15- except ImportError :
16- print ('maybe you have not install pyHook.' )
17- pyHook = None
18-
19- ImageFile .LOAD_TRUNCATED_IMAGES = True
6+ from config import read_config
7+ except Exception as _ :
8+ print ('read config error, use default info.' )
9+ config = None
2010
21- __version__ = '0.2.2 '
11+ __version__ = '1.0.0 '
2212__author__ = 'kingname'
2313
24- from config import read_config
25-
2614
2715class MarkrdownPicPicker (object ):
28- CONFIG_PATH = 'config.ini'
2916
3017 def __init__ (self , link_only = False ):
31-
32- self .method = ''
33- self .picture_folder = ''
34- self .picture_suffix = ''
35- self .picture_bed = ''
36- self .short_key_one = ''
37- self .short_key_two = ''
38- self .key_one = False
39- self .key_two = False
18+ self .cwd = ''
19+ self .picture_folder = 'pic'
20+ self .picture_suffix = 'png'
21+ self .picture_host = ''
4022 self .uploader = None
4123 self .link_only = link_only
42-
24+ self .config_path = ''
25+ self .uploader_info = {}
26+ self .imageGrab = None
4327 self .init_environment ()
4428
45- if self .method == 'bat' :
46- self .upload_picture ()
47- elif self .method == 'global_listen' :
48- self .keyboard_listen ()
29+ self .upload_picture ()
4930
5031 def _to_string (self ):
5132 """
@@ -54,68 +35,43 @@ def _to_string(self):
5435 """
5536 print ("folder" , self .picture_folder )
5637 print ("suffix" , self .picture_suffix )
57- print ("picture_bed" , self .picture_bed )
38+ print ("picture_bed" , self .picture_host )
5839
5940 def init_environment (self ):
60- self .__dict__ .update (read_config ())
61- if not self .method \
62- or not self .picture_folder \
63- or not self .picture_suffix \
64- or not self .picture_bed :
65- print ('there must be something wrong in config, please check.' )
66- exit ()
41+ if not config :
42+ self .uploader = Uploader ()
43+ else :
44+ self .__dict__ .update (read_config ())
45+ self .cwd = os .path .dirname (os .path .dirname (self .config_path ))
46+ uploader_list = self ._find_uploader ()
47+ if self .picture_host and self .picture_host in uploader_list :
48+ self .uploader = __import__ ('uploader.' + self .picture_host ,
49+ globals (), locals (), ['Uploader' ], 0 ).Uploader (self .uploader_info )
50+
6751 if not os .path .exists (self .picture_folder ):
6852 os .makedirs (self .picture_folder )
69- if self .picture_bed :
70- self .uploader = QiniuUploader (self .uploader_info )
71-
72- def keyboard_listen (self ):
73- if not pythoncom or not pyHook :
74- print ('as pythoncom or pyHook is not exists, please use bat method.' )
75- exit ()
76- if not self .short_key_one or not self .short_key_two :
77- print ('there must be something wrong in the config, please check.' )
53+ self .imageGrab = ImageGrab (self .picture_folder , self .picture_suffix ) if ImageGrab else None
54+ if not self .imageGrab :
55+ print ('can not find image grab, exit.' )
7856 exit ()
79- hm = pyHook .HookManager ()
80- hm .KeyDown = self .keyboard_event
81- hm .HookKeyboard ()
82- pythoncom .PumpMessages ()
83-
84- def keyboard_event (self , event ):
85- if event .Key == self .short_key_one and not self .key_one :
86- self .key_one = True
87- elif event .Key == self .short_key_two and not self .key_two :
88- self .key_two = True
89- if self .key_one and self .key_two :
90- self .upload_picture ()
91- self .key_one = False
92- self .key_two = False
93- return True
9457
9558 def upload_picture (self ):
96- picture_path , picture_name = self .save_picture ()
59+ picture_path = self . imageGrab .save_picture ()
9760 if not picture_path :
9861 return False
9962 else :
100- self .uploader .upload (picture_path , picture_name )
101- self .uploader .write_markdown_picture_url (picture_name , link_only = True if self .link_only else False )
63+ self .uploader .upload (picture_path , link_only = True if self .link_only else False )
10264 return True
10365
104- def save_picture (self ):
105- date_time = time .strftime ('%Y-%m-%d-%H-%M-%S' , time .localtime (time .time ()))
106- picture_name = date_time + '.' + self .picture_suffix
107- picture_path = os .path .join (self .picture_folder , picture_name )
108- try :
109- picture_data = ImageGrab .grabclipboard ()
110- if picture_data :
111- picture_data .save (picture_path , self .picture_suffix )
112- return picture_path , picture_name
113- else :
114- print ('there is no picture in clipboard!' )
115- except Exception as e :
116- print ('get picture from clipboard error because: {}' .format (e ))
117- return '' , ''
66+ def _find_uploader (self ):
67+ uploader_folder = os .path .join (self .cwd , 'uploader' )
68+ if os .path .isdir (uploader_folder ):
69+ uploader_list = [uploader_file .split ('.' )[0 ] for uploader_file in os .listdir (uploader_folder )]
70+ if uploader_list :
71+ return uploader_list
11872
73+ print ('can not find the uploader folder.' )
74+ exit ()
11975
12076if __name__ == '__main__' :
12177 arg = sys .argv [- 1 ]
0 commit comments