Quantcast
Channel: Vimタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 5608

vim→Python→Slackに投稿

$
0
0

VIMからPython経由して、Slackに投稿

python.py
importsysimportrequestsimportjsonimportreclassSlackApiWrapper:def__init__(self,proxy_flg=None):```proxy_flg:True:Proxy使うFalse:Non-Proxy```# Your infomationself.token='<your token>'self.postWebhookUrl='<your webhookurl>'# Post infomationself.postSlackUrl='https://slack.com/api/chat.postMessage'self.channel='#general'self.username='vim_python'# if proxyifproxy_flgisNone:self.proxy_flg=Falseelse:self.proxy_flg=proxy_flgself.proxy_info={"http":"http://hogehoge:8080","https":"http://hogehoge:8080"}defpost_api(self,text):json_data={'token':self.token,'channel':self.channel,'text':text,'username':self.username,'unfurl_links':'false','pretty':1}ifself.proxy_flg:r=requests.post(self.postSlackUrl,params=json_data,proxies=self.proxy_info)else:r=requests.post(self.postSlackUrl,params=json_data)print(r.text)defpost_webhook(self,text):json_data={'text':text,'username':self.username,'link_names':1}ifself.proxy_flg:r=requests.post(self.postWebhookUrl,data=json.dumps(json_data),proxies=self.proxy_info)else:r=requests.post(self.postWebhookUrl,data=json.dumps(json_data))print(r.text)defmain(args):filename=Noneifargs:filename=str(args[0])data=NoneiffilenameisNone:data="test"else:try:withopen(filename,mode="r",encoding='utf-8')asfh:data=fh.readlines()exceptExceptionasex:raiseexdata="".join(data)data=re.sub(r"\n","",data)# None Porxy# sl = SlackApiWrapper(False)# Porxysl=SlackApiWrapper(True)# どちらでもsl.post_webhook(data)sl.post_api(data)if__name__=="__main__":main(sys.argv[1:])
_vimrc.local
"slack"{{{
function! s:post_slack_vim()"
    let s:file = tempname()
    let s:py_script = "<DLしたPython.pyのPATH>"
    silent execute ":write " . s:file
    silent execute '!python' . ' ' . s:py_script . ' ' . s:file
    call delete(s:file)
    unlet! s:file
    unlet! s:py_script
endfunction augroup END"
command! -nargs=0 PSlack call s:post_slack_vim()
"}}}

Viewing all articles
Browse latest Browse all 5608

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>