Skip to content

Instantly share code, notes, and snippets.

@crosstyan
Last active November 26, 2022 16:26
Show Gist options
  • Save crosstyan/1960862813c2624ee73c6b21942d02c8 to your computer and use it in GitHub Desktop.
Save crosstyan/1960862813c2624ee73c6b21942d02c8 to your computer and use it in GitHub Desktop.
moving file and its txt to certain dir
// @name ブックファイルを移動する
// @description 開いているブックのファイルを移動するサンプルです。ブックがディレクトリの場合は機能しません。
const distDirectory = "D:\\dataset\\good\\"
const distDirInfo = new System.IO.DirectoryInfo(distDirectory)
if (distDirInfo.Exists == false) {
distDirInfo.Create()
nv.ShowToast("Created directory: " + distDirectory)
}
if (nv.Book.Path == null) return
const currentPath = nv.Book.ViewPages[0].Path
if (currentPath == null) return
const file = new System.IO.FileInfo(currentPath)
if (file.Exists){
// Text ファイルの移動
const txtPath = System.IO.Path.ChangeExtension(file, "txt")
const txtFile = new System.IO.FileInfo(txtPath)
if (txtFile.Exists) {
const newTxtPath = System.IO.Path.Combine(distDirectory, txtFile.Name)
const txt = new System.IO.FileInfo(txtPath)
txt.MoveTo(newTxtPath)
}
const distPath = System.IO.Path.Combine(distDirectory, file.Name)
file.MoveTo(distPath)
nv.Command.NextPage.Execute()
nv.ShowToast("move to " + distPath)
} else{
nv.ShowToast("File is not found: " + file.Name);
}
// @name Run Python
// @description Run Python with it
const currentPath = nv.Book.ViewPages[0].Path
/// the path of python interpreter
const pythonPath = String.raw`C:\Users\cross\AppData\Local\Programs\Python\Python310\python.exe`
/// the path of python script
const pythonScriptPath = String.raw`C:\Users\cross\Documents\NeeLaboratory\NeeView\Scripts\test.py`
const startInfo = new System.Diagnostics.ProcessStartInfo()
startInfo.FileName = pythonPath
startInfo.Arguments = [pythonScriptPath, "--path", currentPath].join(" ")
startInfo.UseShellExecute = false
const process = System.Diagnostics.Process.Start(startInfo)
import asyncio
import os
import argparse
from pprint import pprint
from pathlib import Path
from PicImageSearch import SauceNAO, Network
from loguru import logger
from PicImageSearch.model import SauceNAOResponse, SauceNAOItem
import json
from pygments import highlight, lexers, formatters
# https://github.com/kitUIN/PicImageSearch
# sauce = SauceNao(api_key=os.environ.get('SAUCENAO_API_KEY'))
def show_result(resp: SauceNAOResponse) -> None:
logger.info(resp.status_code)
# logger.info(resp.origin) # Original response
logger.info(resp.url)
logger.info(resp.long_remaining)
logger.info(resp.short_remaining)
raw = resp.raw
t = json.dumps(raw, indent=2, ensure_ascii=False, default=lambda obj: obj.__dict__)
colorful_json = highlight(t, lexers.JsonLexer(), formatters.TerminalFormatter())
print(colorful_json)
async def get_sauce(file:Path):
async with Network(proxies='scheme://127.0.0.1:36000') as client:
sauce = SauceNAO(client=client, api_key="API_KEY")
res = await sauce.search(file=str(file))
show_result(res)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--path', type=str, default='.')
args = parser.parse_args()
p = Path(args.path)
loop = asyncio.get_event_loop()
if p.exists():
loop.run_until_complete(get_sauce(p))
else:
print('File not found')
input("Press Enter to continue...")
import os
import argparse
from pathlib import Path
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--path', type=str, default='.')
args = parser.parse_args()
home = Path.home()
txt = home / "test.txt"
with open(txt, 'w') as f:
f.write(args.path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment