01 视频脚本
在 CSDN 中编写的视频脚本文件,在导入 TEASOFT 的过程中,现在还需要一节一节进行手工导入,极大限制导入速度。现在编写 Python 程序实现一键 CSDN 视频脚本文件导入 TEASOFT 的功能。
一、视频脚本文件格式
根据 CSDN 视频脚本文件的以下格式定义,先将 脚本文件对应的 Markdown 内容进行分割,对于能够导入的内容提炼出来。然后在利用 DOPASTE Python 脚本文件导入 TEASOFT 软件。 脚本视频文件的格式:
- 分别所有一级、二级、三级级标题之内的内容;
- 对于每一段之间,判断是否存在如下 Markdown 文档标致:
- 具有 &thinsp 或者 “ ”、“【】” 等表示符
- 具有 #pic_left 表示。
对于每一段分割之后,在调用 “dopaste” 软件通过 clipboard 进行内容传送。
二、Python 程序
导入软件为 “CSDN2DOP”。
1、Python代码
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# CSDN2DOP.PY -- by Dr. ZhuoQing 2022-10-01
#
# Note:
#============================================================
from head import *
csdn_title = '写文章-CSDN博客'
#------------------------------------------------------------
tspsendwindowkey(csdn_title, " ", noreturn=1)
tspsendwindowkey(csdn_title, "ac", control=1,noreturn=1)
strall = clipboard.paste()
tspsendwindowkey(csdn_title, "z", control=1)
#------------------------------------------------------------
segdim = []
tempdim = []
strdim = strall.split('\n')
#------------------------------------------------------------
def titlestring(s):
if s[:1] != '#': return 0
ss = s.split(' ')
if len(ss) < 2: return 0
if ss[0] == '#': return 1
if ss[0] == '##': return 2
if ss[0] == '###': return 3
if ss[0] == '####': return 4
if ss[0] == '#####': return 5
if ss[0] == '######': return 6
return 0
def spaceofflen(s):
return len(s.replace(' ','').replace('\n', '').replace('\r',''))
def stringflag(s):
if s.find(' ') >= 0: return 1
if s.find(' ') >= 0: return 1
if s.find('【】') >= 0: return 1
  if s.find('#pic_left') >= 0: return 1
return 0
#------------------------------------------------------------
addflag = 0
for s in strdim:
if spaceofflen(s) == 0: continue
sharpnum = titlestring(s)
if sharpnum == 1:
if len(tempdim) > 1:
segdim.append(tempdim)
tempdim = []
addflag = 1
tempdim.append(s)
continue
if sharpnum == 2:
if len(tempdim) > 1:
segdim.append(tempdim)
tempdim = []
addflag = 2
tempdim.append(s)
continue
if sharpnum == 3:
if len(tempdim) < 2 or addflag >= 3:
if len(tempdim) > 1:
segdim.append(tempdim)
tempdim = []
addflag = 3
tempdim.append(s)
continue
if addflag > 0:
if stringflag(s):
tempdim.append(s)
#------------------------------------------------------------
for sd in segdim:
printf(sd)
printf(' \a')
clipboard.copy('\n'.join(sd))
tspexecutepythoncmd('dopaste # *')
for i in range(20):
time.sleep(1)
s = clipboard.paste()
if s.find('DOPASTE END') >= 0:
clipboard.copy("")
break
tspbeep(2200, 25)
printf('CSDN2DOP END.\a')
#------------------------------------------------------------
# END OF FILE : CSDN2DOP.PY
#============================================================
2、DOPASTE
需要对于DOPASTE 软件进行修改。具体代码为:
#!/usr/local/bin/python
# -*- coding: gbk -*-
#============================================================
# DOPASTE.PY -- by Dr. ZhuoQing 2021-10-07
#
# dopaste <ssss # 【ssss.zip】
# dopaste =ssss # 【ssss】
#
# Note:
#============================================================
from head import *
import urllib.request
from PIL import Image
from io import BytesIO
import win32clipboard
import requests
import zipfile
import cv2
csdn_title = '写文章-CSDN博客'
#------------------------------------------------------------
outdir = r'd:\temp'
microvideo_dir = r'd:\Nutdisk\MicroVideo'
microvideo_mooc = r'd:\MooC\MicroVideo'
MAX_LENGTH = 10
picselflag = 0
FILENAME_LENGTH = 128
#------------------------------------------------------------
def data82data(data):
data[data < 0x80] = 0
data[data != 0] = 1
datashape = shape(data)
W = datashape[-1]//8
H = datashape[0]
data = data.reshape(size(data))
bits = array([1,2,4,8,16,32,64,128])
data = data.reshape(-1, 8) * bits
data8 = sum(data.T,0).reshape(H,W)
return data8
#------------------------------------------------------------
def img2databuf(imagefile):
img = cv2.imread(imagefile)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY).astype(uint8)
data = data82data(gray).astype(uint8)
data = data.reshape(size(data)).tobytes()
datalen = int.from_bytes(data[:4], 'little')
filename = data[4:4+FILENAME_LENGTH].decode('gbk').strip('\x00')
return datalen, filename, data[4+FILENAME_LENGTH:]
def img2datafile(imagefile, datafile):
datalen, filename, data = img2databuf(imagefile)
printff(datalen, filename)
with open(datafile, 'wb') as f:
f.write(data[:datalen])
return filename
#------------------------------------------------------------
def extractGIFimage(giffile, outdir):
try:
im = Image.open(giffile)
except IOError:
printf("Can not load %s ."%infile)
sys.exit()
i = 0
mypalette = im.getpalette()
firstfile = ''
basenameonly = os.path.basename(giffile).split('.')[0]
strid = basenameonly.find('_-')
if strid > 0: basenameonly = basenameonly[:strid]
try:
while True:
im.putpalette(mypalette)
new_im = Image.new("RGB", im.size)
new_im.paste(im)
savefile = os.path.join(outdir, '%s_%d.JPG'%(basenameonly, i))
new_im.save(savefile)
if i == 0: firstfile = savefile
i += 1
im.seek(im.tell() + 1)
except EOFError:
pass # end of sequence
#
im.close()
return firstfile
#------------------------------------------------------------
def str2bsim(s):
if s.find('![N]') >= 0: return 0
if s.find('![n]') >= 0: return 0
if s.find('![O]') >= 0: return 1
if s.find('![o]') >= 0: return 1
if s.find('![L]') >= 0: return 2
if s.find('![l]') >= 0: return 2
if s.find('![S]') >= 0: return 1
if s.find('![s]') >= 0: return 1
if s.find('![A]') >= 0: return 4
if s.find('![a]') >= 0: return 4
if s.find('![F]') >= 0: return 4
if s.find('![f]') >= 0: return 4
if s.find('![R]') >= 0: return 3
if s.find('![r]') >= 0: return 3
if s.find('![P]') >= 0: return 5
if s.find('![p]') >= 0: return 5
if s.find('![Q]') >= 0: return 6
if s.find('![q]') >= 0: return 6
if s.find('![S]') >= 0: return 7
if s.find('![s]') >= 0: return 7
if s.find('![1]') >= 0: return 9
if s.find('![2]') >= 0: return 10
if s.find('![3]') >= 0: return 11
if s.find('![4]') >= 0: return 12
if s.find('![5]') >= 0: return 13
if s.find('![6]') >= 0: return 14
if s.find('![7]') >= 0: return 15
if s.find('![8]') >= 0: return 16
if s.find('![9]') >= 0: return 17
if s.find('![0]') >= 0: return 18
return -1
#------------------------------------------------------------
def extractMPGimage(mpegfile, outdir):
fnonly = os.path.basename(mpegfile).split('.')[0]
cam = cv2.VideoCapture(mpegfile)
printf(mpegfile)
currentframe = 0
while True:
ret, frame = cam.read()
if ret:
filename = '%s_%04d.JPG'%(fnonly, currentframe)
outfile = os.path.join(outdir, filename)
cv2.imwrite(outfile, frame)
currentframe += 1
else:
break
cam.release()
return fnonly
#------------------------------------------------------------
def send_to_clipboard(clip_type, data):
win32clipboard.OpenClipboard()
win32clipboard.EmptyClipboard()
win32clipboard.SetClipboardData(clip_type, data)
win32clipboard.CloseClipboard()
def image2clipboard(filename):
image = Image.open(filename)
output = BytesIO()
image.convert('RGB').save(output, 'BMP')
data = output.getvalue()[14:]
output.close()
send_to_clipboard(win32clipboard.CF_DIB, data)
#------------------------------------------------------------
def zip2dir(filename, dirs):
dopfile = ''
basedir = ''
bsirfilename = ''
with zipfile.ZipFile(filename, 'r') as f:
for fn in f.infolist():
fns = str(fn).split()[1]
if fns.find('.bsir') >= 0 or fns.find('.BSIR') >= 0:
fnss = fns.split('\'')
basedir = fnss[1].split('/')[0]
bsirfilename = fnss[1]
dopfile = os.path.join(dirs, fnss[1])
break
f.extractall(dirs)
bsirname = dopfile.replace('/', '\\')
newbasedir = ''
for i in range(1000):
newbasedir = '%s%d'%(basedir, i)
newdirs = os.path.join(dirs, newbasedir)
if not os.path.isdir(newdirs):
break
if not os.path.isdir(newdirs):
origindir = os.path.join(dirs, basedir)
os.rename(origindir, newdirs)
dopfile = os.path.join(dirs, bsirfilename.replace(basedir, newbasedir))
bsirname = dopfile.replace('/', '\\')
# printf(bsirname)
# exit()
return bsirname
#------------------------------------------------------------
def deleteformular(str):
sdim = str.split('$$')
if len(sdim) < 2: return str
snew = []
for s in sdim:
flag = 0
for a in s:
if ord(a) > 0x100:
flag = 1
break
if flag > 0: snew.append(s)
sdim = (' '.join(snew)).split('$')
snew = []
for s in sdim:
flag = 0
for a in s:
if ord(a) > 0x100:
flag = 1
break
if flag > 0: snew.append(s)
else: snew.append(' ')
sdim = (''.join(snew)).split('<')
snew = []
for s in sdim:
ssid = s.find('>')
if ssid >= 0: snew.append(s[ssid+2:])
else: snew.append(s)
strnew = ''.join(snew)
return strnew
#------------------------------------------------------------
strall = clipboard.paste().split('\n')
printf(sys.argv)
if sys.argv[-1] != '*':
tspsendwindowkey(csdn_title, "c", control=1)
csdnall = clipboard.paste()
else:
sys.argv = sys.argv[:-1]
csdnall = clipboard.paste()
if len(sys.argv) > 1:
if sys.argv[1][:1] == '<':
fname = os.path.join(microvideo_mooc, '%s.ZIP'%sys.argv[1][1:])
if os.path.isfile(fname):
csdnall = "【%s.ZIP】"%sys.argv[1][1:]
strall = ""
sys.argv = sys.argv[:1]
elif sys.argv[1][:1] == '=':
fdim = os.listdir(microvideo_mooc)
fname = sys.argv[1][1:]
flen = len(fname)
if flen > 0:
for f in fdim:
if f[:flen] == fname:
csdnall = "【%s】"%fname
strall = ""
break
sys.argv = sys.argv[:1]
#------------------------------------------------------------
if csdnall.count('|') > 3 and csdnall.count('-') < 6 and csdnall.find('M1') >= 0 and csdnall.find('M2') >= 0:
firstline = csdnall
tspsendwindowkey(csdn_title, "%c"%VK_RIGHT, vk=1, noreturn=1)
tspsendwindowkey(csdn_title, " ", noreturn=1)
tspsendwindowkey(csdn_title, "ac", control=1, noreturn=1)
tspsendwindowkey(csdn_title, "c", control=1, noreturn=1)
csdnall = clipboard.paste().split('\n')
tspsendwindowkey(csdn_title, "z", control=1)
firstflag = 0
nlen = len(firstline)
section = []
for s in csdnall:
if s.find(firstline) >= 0:
firstflag = 1
if firstflag > 0:
if len(s) == 0 or s.count('|') == 0:
break
else: section.append(s)
csdnall = '\n'.join(section)
printf('len:%d'%len(csdnall))
#------------------------------------------------------------
if csdnall.count('\n') < 2 and csdnall[:2] == '##':
firstline = csdnall
tspsendwindowkey(csdn_title, "%c"%VK_RIGHT, vk=1, noreturn=1)
tspsendwindowkey(csdn_title, " ", noreturn=1)
tspsendwindowkey(csdn_title, "ac", control=1, noreturn=1)
tspsendwindowkey(csdn_title, "c", control=1, noreturn=1)
csdnall = clipboard.paste().split('\n')
tspsendwindowkey(csdn_title, "z", control=1)
firstflag = 0
nlen = len(firstline)
section = []
for s in csdnall:
if s.find(firstline) >= 0:
firstflag = 1
section.append(s)
continue
if firstflag > 0:
if s[:1] == '#' or s[:1] == '-':
break
else: section.append(s)
csdnall = '\n'.join(section)
tspaction(9999)
#------------------------------------------------------------
if csdnall.count('\n') < 2 and csdnall.find('<center><font') >= 0 \
and csdnall.find('。') < 0 and csdnall.find('【') < 0:
firstline = csdnall
tspsendwindowkey(csdn_title, "%c"%VK_RIGHT, vk=1, noreturn=1)
tspsendwindowkey(csdn_title, " ", noreturn=1)
tspsendwindowkey(csdn_title, "ac", control=1, noreturn=1)
tspsendwindowkey(csdn_title, "c", control=1, noreturn=1)
csdnall = clipboard.paste().split('\n')
tspsendwindowkey(csdn_title, "z", control=1)
firstflag = 0
nlen = len(firstline)
section = []
for s in csdnall:
if s.find(firstline) >= 0:
firstflag = 1
section.append(s)
continue
if firstflag > 0:
if s[:1] == '#':
break
else: section.append(s)
csdnall = '\n'.join(section)
tspaction(9999)
#------------------------------------------------------------
greatflag = 0
lessflag = 0
sharpflag = 0
bsirnextflag = 0
if len(sys.argv) > 1:
if sys.argv[1][:1] == '#':
sharpflag = 1
sys.argv[1] = sys.argv[1][1:]
bsirnextflag = 1
if sys.argv[1] == '>' or sys.argv[1] == '》':
greatflag = 1
elif sys.argv[1] == '<' or sys.argv[1] == '《':
lessflag = 1
elif sys.argv[1] == '#':
sharpflag = 1
#------------------------------------------------------------
if greatflag > 0 and csdnall.count('\n') < 2 and csdnall.find('。') < 0 and csdnall.find('【') < 0:
firstline = csdnall
tspsendwindowkey(csdn_title, "%c"%VK_RIGHT, vk=1, noreturn=1)
tspsendwindowkey(csdn_title, " ", noreturn=1)
tspsendwindowkey(csdn_title, "ac", control=1, noreturn=1)
tspsendwindowkey(csdn_title, "c", control=1, noreturn=1)
csdnall = clipboard.paste().split('\n')
tspsendwindowkey(csdn_title, "z", control=1)
firstflag = 0
nlen = len(firstline)
section = []
for s in csdnall:
s = s.strip(' ')
if s[:2] == '>*' or s[:2] == '>>': continue
if s.find(firstline) >= 0:
firstflag = 1
section.append(s)
continue
if firstflag > 0:
if s[:1] != '>':
break
else: section.append(s)
csdnall = '\n'.join(section)
tspaction(9999)
#------------------------------------------------------------
if len(csdnall) > 0:
strall = csdnall.split('\n')
#printf(strall)
#printff(greatflag, lessflag)
if greatflag == 0:
sdim = []
centerflag = 0
for id,s in enumerate(strall):
if s[:1] == '>':
if s[:2] == '>>':continue
if s[:2] == '>*':continue
if lessflag > 0: continue
sdim.append(s[1:].replace('<br><br>', ' ').replace('<br>','').replace(' ',''))
continue
if s[:1] != '#':
sdim.append(s)
continue
if sharpflag == 0: continue
if s.find('# <center><') >= 0:
redid = s.find('=red>')
if redid < 0: continue
s = s[redid+5:]
fontid = s.find('</center')
if fontid < 0: continue
s = s[:fontid].replace('</font>','').replace(' ','').replace('※','')
# strall[id] = '- [ ] %s'%s
sdim.append('- [ ] %s'%s)
sharpflag = 0
centerflag = 1
else:
startid = s.find('>')
if startid < 0: continue
s = s[startid+1:].replace(' ', '')
startid = s.find('<')
if startid > 0: s = s[:startid]
if s.find(' ') >= 0: s = s.split(' ')[-1]
if s.find(')') >= 0: s = s.split(')')[-1]
if s.find('.') >= 0: s = s.split('.')[-1]
if s.find('、') >= 0: s = s.split('、')[-1]
# strall[id] = '- [ ] %s'%s
if centerflag == 0:
sdim.append('- [ ] %s'%s)
sharpflag = 0
strall = sdim
else:
sdim = []
centerflag = 0
for id,s in enumerate(strall):
if s[:1] == '>':
if s[:2] == '>>':continue
if s[:2] == '>*':continue
sdim.append(s[1:].replace('<br><br>', ' ').replace('<br>','').replace(' ',''))
continue
if s[:1] != '#':
  if s.find('#pic_left') >= 0:
sdim.append(s.replace(" ", ""))
continue
if sharpflag == 0: continue
if s.find('# <center><') >= 0:
redid = s.find('=red>')
if redid < 0: continue
s = s[redid+5:]
fontid = s.find('</center')
if fontid < 0: continue
s = s[:fontid].replace('</font>','').replace(' ','').replace('※','')
sdim.append('- [ ] %s'%s)
sharpflag = 0
centerflag = 1
else:
startid = s.find('>')
if startid < 0: continue
s = s[startid+1:].replace(' ', '')
startid = s.find('<')
if startid > 0: s = s[:startid]
if s.find(' ') >= 0: s = s.split(' ')[-1]
if s.find(')') >= 0: s = s.split(')')[-1]
if s.find('.') >= 0: s = s.split('.')[-1]
if centerflag == 0:
sdim.append('- [ ] %s'%s)
sharpflag = 0
strall = sdim
#------------------------------------------------------------
  if csdnall.find('#pic_left') > 0:
sdim = []
for s in strall:
if len(s.strip(' ')) == 0:
continue
#----------------------------------------------------
flag = 0
if s[0] == '#':
if s[:2] == '##':
startid = s.find('>')
if startid > 0:
endid = s[startid+1:].find('<')
if endid > 0:
titles = s[startid+1:][:endid].split(' ')[-1]
titles = titles.split('、')[-1]
s = "- [ ] " + titles
flag = 1
if flag == 0: continue
#----------------------------------------------------
s = s.replace(' ', '')
if s.find('**](http') >= 0:
ss = s.split('(http')
ssdim = []
for sss in ss:
id = sss.find(')')
if id > 0:
sss = sss[id+1:]
ssdim.append(sss)
s = ''.join(ssdim)
  if s.find('#pic_left') < 0:
sdim.append(s)
continue
ss = s.split('x)')
ssall = ""
for sss in ss:
if len(sss.strip(' ')) == 0: continue
modestr = ''
startid = sss.find('![')
endid = sss.find(']')
if startid < endid:
modestr = sss[startid+2:endid]
#------------------------------------------------
numid = modestr.find('_-')
if numid > 0:
num = int(modestr[numid+2:])
modestr = modestr[:numid]
ssid = modestr.find('|')
if ssid > 0:
mpgfile = modestr[:ssid]
modestr = modestr[ssid+1:]
mpgfile = os.path.join(microvideo_dir, mpgfile)
numpicfile = extractMPGimage(mpgfile, outdir)
  sssend = sss[endid:].replace('#pic_left', "#pic_center")
lastc = ''
for i in range(num):
if i < len(modestr): lastc = modestr[i]
if lastc == '#': lastc = ''
ssall = ssall + '|![%s-%s_%04d.JPG'%(lastc, numpicfile, i) + sssend + 'x)|%s'%lastc
continue
#------------------------------------------------
numid = modestr.find('_=')
if numid > 0:
numdim = modestr[numid+2:].split('-')
numnum = [int(n) if n.isdigit() else 0 for n in numdim]
printf(numnum)
modestr = modestr[:numid]
ssid = modestr.find('|')
if ssid > 0:
mpgfile = modestr[:ssid]
modestr = modestr[ssid+1:]
  sssend = sss[endid:].replace('#pic_left', "#pic_center")
for i in range(len(numdim)):
if i == 0:
ssall = ssall + '|![%s'%mpgfile + sssend + 'x)|%s'%modestr
else:
ssall = ssall + '|http{%d}'%sum(numnum[:i]) + '|%s'%modestr
continue
#--------------------------------------------
if len(modestr) > 2 and modestr.find('-') >=0 and sss.find('.gif#pic') > 0:
gifmodestr = ''
if modestr[0] == '-':
ssid = modestr[1:].find('-')
if ssid > 0 and modestr[0] == '-':
gifmodestr = modestr[1:][ssid+1:]
modestr = '-' + modestr[1:][:ssid+1]
if modestr[0] == '-' and modestr[-1] == '-' and modestr[1:-1].isdigit():
num = int(modestr[1:-1])
  s1 = '|![%s'%gifmodestr + sss[endid:].replace('#pic_left', "#pic_center")
s1id = s1.find('.gif#pic')
if s1id > 0:
lastc = ''
for i in range(num):
if i < len(gifmodestr): lastc = gifmodestr[i]
if lastc == '#': lastc = ''
if i > 0: ssall = ssall + s1[:s1id] + '_%d'%i + s1[s1id:]
else: ssall = ssall + s1[:s1id] + '_%s'%modestr[:-1] + s1[s1id:]
ssall = ssall + 'x)|%s'%lastc
continue
#----------------------------------------
pagedim = modestr.split('-')
  s1 = '|![' + sss[endid:].replace('#pic_left', "#pic_center") + 'x)|'
s1id = s1.find('.gif#pic')
if s1id > 0:
for i in range(len(pagedim)):
if i != 0:
ssall = ssall + s1[:s1id] + '_%d'%i + s1[s1id:]
else:
ssall = ssall + s1[:s1id] + '_%s'%modestr + s1[s1id:]
continue
if len(modestr) > 5:
iidd = modestr.find('|')
if iidd > 0:
sss = '![' + modestr[:iidd] + sss[endid:]
  sss = sss.replace('#pic_left', "#pic_center")
modestr = modestr[iidd+1:]
if modestr == '>' or modestr == '》': modestr = '>>'
else:
modestr = ''
else:
sss = '![' + sss[endid:]
  sss = sss.replace('#pic_left', "#pic_center")
if modestr == 'PS' or modestr == 'ps':
modestr = ''
picselflag = 1
#------------------------------------------------
ssall = ssall + '|%sx)|%s'%(sss,modestr)
num = len(sdim) - 1
if num >= 0:
if sdim[num][:2] == '* ':
sdim.append(ssall)
else:
sdim[num] = sdim[num].replace('【】','<br><br>').replace(' ', '<br><br>').replace(' ', '<br><br>') + ssall
else:
sdim.append(' '+ssall)
strall = sdim
#printf(strall)
#exit()
#------------------------------------------------------------
def mode2script(script, mode):
sss = script.strip(' ')
ssl = mode.strip(' ')
if ssl[:1] == '*':
sss = '{65,1500,50,50}' + sss
elif ssl[:1] == '+':
sss = '{150,1500,50,50}' + sss
elif ssl[-3:] == '>>>' or ssl[-3:] == ' 》' or ssl[-2:] == '>>' or ssl[-2:] == ' ':
sss = sss + "【】>>>"
# sss = sss + "  >>>"
elif ssl[-3:] == '<<<' or ssl[-3:] == '《《《' or ssl[-2:] == '<<' or ssl[-2:] == '《《':
sss = sss + "【】<<<"
elif ssl[-2:] == '<>' or ssl[:-2] == '<br><br>':
sss = sss + "<>"
elif ssl[-2:] == '><' or ssl[:-2] == '》《':
sss = sss + "><"
elif ssl[:1] == '<' or ssl[:1] == '《':
sss = '{80,1500,1,0}' + sss
elif ssl[:1] == '>' or ssl[:1] == '》':
sss = '{80,1500,-1,0}' + sss
elif ssl[:1] == '_' or ssl[:1] == '=':
sss = '{80,1500,0,1}' + sss
elif ssl[:1] == '-':
sss = '{80,1500,0,-1}' + sss
elif ssl[:1] == 'N' or ssl[:1] == 'n':
sss = '{N}' + sss
elif ssl[:1] == 'Q' or ssl[:1] == 'q':
sss = '{Q}' + sss
elif ssl[:1] == 'L' or ssl[:1] == 'l':
sss = '{L}' + sss
elif ssl[:1] == 'A' or ssl[:1] == 'a':
sss = '{A}' + sss
elif ssl[:1] == 'F' or ssl[:1] == 'f':
sss = '{A}' + sss
elif ssl[:1] == 'R' or ssl[:1] == 'r':
sss = '{R}' + sss
elif ssl[:1] == 'P' or ssl[:1] == 'p':
sss = '{P}' + sss
elif ssl[:1] == 'S' or ssl[:1] == 's':
sss = '{S}' + sss
return sss
#------------------------------------------------------------
rdim = []
insertid = 0
appendcck = 0
sharpnum = 0
#printf(strall)
lastnullline = -1
for s in strall:
if s.find('<span id=999000>') >= 0:
break
#----------------------------------------------------
if s.find('size=5>') >= 0:
id5 = s.find('=5>')
s = s[id5+3:].replace('</font>','')
#----------------------------------------------------
if s[:2] == '* ':
continue
sid = s.find('- [ ]')
if sid < 0: sid = s.find('- [x]')
if sid >= 0:
tspcopystring('##' + s[sid+5:].strip(' '))
continue
#--------------------------------------------------------
if len(s.strip('\r').strip('\n')) == 0:
lastnullline = len(rdim)
continue
#--------------------------------------------------------
musicflag = 0
id = s.find('BGM:**')
if id >= 0:
s = '&#&%s#'%(s[id+6:].strip(' '))
musicflag = 1
id = s.find('SND:**')
if id >= 0:
s = '&##%s#'%(s[id+6:].strip(' '))
musicflag = 1
id = s.find('BGM:**')
if id >= 0:
s = '&#&%s#'%(s[id+6:].strip(' '))
musicflag = 1
id = s.find('SND:**')
if id >= 0:
s = '&##%s#'%(s[id+6:].strip(' '))
musicflag = 1
id = s.find('MUSIC:**')
if id >= 0:
s = '&#%s#'%(s[id+8:].strip(' '))
musicflag = 1
id = s.find('MUSIC:**')
if id >= 0:
s = '&#%s#'%(s[id+8:].strip(' '))
musicflag = 1
id = s.find('CMD:**')
if id >= 0:
s = '&#%s#'%(s[id+6:].strip(' '))
musicflag = 1
id = s.find('CMD:**')
if id >= 0:
s = '&#%s#'%(s[id+6:].strip(' '))
musicflag = 1
#--------------------------------------------------------
# printf('%d:%s'%(len(s), s))
s = deleteformular(s)
# printf('%d:%s'%(len(s), s))
#--------------------------------------------------------
if s[:1] in "*-": continue
if s.find('--|--') >= 0: continue
if s.find('|P') >= 0:
picselflag = 1
continue
# if s.find('|N') >= 0: continue
if s.find('|I') >= 0:
if picselflag > 0:
tspcopystring('PICSELCLOSE')
time.sleep(1)
picselflag = 0
continue
if s.find('|M') >= 0: continue
# if s.find('|A') >= 0: continue
if s.find('|备注') >= 0: continue
if s.find('|图像') >= 0: continue
if s.find('<div') >= 0: continue
# s = s.replace(" ", "<br><u>01</u> <br>")
#--------------------------------------------------------
s = s.replace("[**", "").replace("**]", "")
startid = s.find('(http')
if startid >= 0 and s.find('![') < 0:
sdim = s.split('(http')
if len(sdim) > 1:
for id, ss in enumerate(sdim):
endid = ss.find(')')
if endid >= 0:
sdim[id] = ss[endid+1:]
s = ''.join(sdim)
#--------------------------------------------------------
if s.count('|') >= 1 and s.find('http') > 0:
ss = s.split('|')
ss[0] = ss[0].replace('<br><br>', "<br><br>")
ss[0] = ss[0].replace('<br><br>', "<br>")
script = ss[0].split('<br>')
pic = ss[1::2]
mode = ss[2::2]
for id,sss in enumerate(script):
if id < len(pic):
ps = pic[id].strip(' ')
if ps.find('http{') >= 0:
ids = ps.find('http{')
rdim.append(ps[4:]+sss)
continue
if ps.find('【') >= 0:
sss = ps+sss
elif len(ps) > 0: rdim.append(ps)
mstr = ''
if id < len(mode): mstr = mode[id]
sss = mode2script(sss, mstr)
if len(sss) > 0: rdim.append(sss)
insertid = len(rdim)
for i in range(len(pic) - len(script)):
id = len(script) + i
ps = pic[id]
if ps.find('http(') >= 0: continue
sss = ''
if ps.find('【') >= 0:
sss = ps+sss
elif len(ps) > 0: rdim.append(ps)
mstr = ''
if id < len(mode): mstr = mode[id]
sss = mode2script(sss, mstr)
if len(sss) > 0: rdim.append(sss)
insertid = len(rdim)
continue
#--------------------------------------------------------
if s[:1] == '#':
if sharpnum == 0:
rdim = []
insertid = 0
appendcck = 0
sharpnum += 1
#--------------------------------------------------------
appendcck = 0
#--------------------------------------------------------
spanid = s.find('</span>')
if spanid > 0: s = s[spanid+7:]
s = s.replace(' ', '')
s = s.replace('[**','·').replace('**]','·').replace('“**','').replace('**”','').replace('`','')
s = s.replace('*-*','~ ').replace(' ', '~ ').replace('**-**','~ ')
s = s.replace('</font>', '').replace('<br>', ' ')
size5id = s.find('size=5>')
if size5id > 0:
s = s[size5id+7:]
#--------------------------------------------------------
if s.find('#pic_center') < 0:
sdim = []
ss = s.split('(')
for ss1 in ss:
sstartid = ss1.find(')')
if sstartid > 0: ss1 = ss1[sstartid+1:]
sdim.append(ss1)
if s.find('http') > 0:
s = ''.join(sdim)
else:
centerid = s.find('<center>')
if s[-1] in '+-*':
appendcck = 1
if centerid > 0: s = s[:centerid]
#--------------------------------------------------------
# if len(s) > 0: printf(s)
sdim = s.split(')![')
flag = 0
if len(sdim) > 1:
s = sdim[0]+")"
flag = 1
if s.find('![▲') >= 0:
startid = s.find('](')
if startid > 0:
s = '![](%s'%s[startid+2:]
rdim.insert(insertid, s)
if flag > 0: rdim.insert(insertid+1, ">>>")
insertid = len(rdim)
elif s.find('![GM') >= 0 or s.find('![AV') >= 0:
rdim.insert(insertid, s)
if appendcck > 0 or flag > 0:
if insertid+1 < len(rdim):
rdim[insertid+1] = rdim[insertid+1] + ">>>"
insertid = len(rdim)
elif s.find('![') >= 0 and s.find('![ ]') < 0:
startid = s.find('](')
if startid > 0:
s = '![](%s'%s[startid+2:]
rdim.insert(insertid, s)
insertid = len(rdim)
else:
rdim.append(s)
if lastnullline >= 0: insertid = lastnullline
lastnullline = -1
if s[:1] == '#':
insertid = len(rdim)
if len(sdim) > 1:
for id,s in enumerate(sdim[1:]):
s = '![' + s
flag = 0
if id < len(sdim[1:]) - 1:
s = s + ')'
flag = 1
if s.find('![GM') >= 0 or s.find('![AV') >= 0:
rdim.append(s)
if flag: rdim.append('>>>')
insertid = len(rdim)
else:
rdim.append(s)
if s[:1] == '#':
insertid = len(rdim)
if musicflag > 0: insertid = len(rdim)
#------------------------------------------------------------
#for s in rdim:
# if len(s) > 0: printf(s)
#exit()
#------------------------------------------------------------
def divideSentence(s):
sdim = [s]
for ss in ["。", ",", "?", "!", ";", ":", "、", ","]:
tdim = []
largeflag = 0
for l in sdim:
if len(l.strip(' ')) == 0: continue
if len(l) < MAX_LENGTH:
tdim.append(l)
printf(l)
continue
ll = l.split(ss)
if len(ll) == 1:
tdim.append(l)
continue
tdim.extend([sss+ss if id < len(ll)-1 else sss for id,sss in enumerate(ll) if len(sss) > 0])
largeflag += 1
sdim = tdim
if largeflag == 0: break
while True:
tdim = []
appendstr = ''
breakflag = 1
for id,l in enumerate(sdim):
if id == len(sdim) - 1:
if len(appendstr) > 0:
if len(appendstr) + len(l) < MAX_LENGTH*2 or len(appendstr) < 6:
tdim.append(appendstr + l)
breakflag = 0
else:
tdim.append(appendstr)
tdim.append(l)
else:
tdim.append(l)
appendstr = ''
else:
if len(appendstr) > 0:
if len(appendstr) + len(l) < MAX_LENGTH*2 or len(appendstr) < 6:
appendstr = appendstr + l
breakflag = 0
else:
if len(appendstr) > 0:
tdim.append(appendstr)
appendstr = l
else: appendstr = l
sdim = tdim
if breakflag > 0: break
return sdim
#------------------------------------------------------------
strall = rdim
#printf(strall)
linecount = 0
pngappendshift = ''
for s in strall:
s = s.strip(' ').replace(' ~ ','~')
s = s.strip('\r').strip('\r\n')
s = s.replace('<br>', ' ')
if s.find('D:\\') >= 0: continue
#-------------------------------------------------------
if s[:2] == '> ':
s = s[2:]
#--------------------------------------------------------
if sharpflag == 0:
if s[:1] == '#':
continue
#--------------------------------------------------------
if s[:1] == '>' or s[:1] == '#':
if s[:1] == '#':
tspaction(9999)
continue
if s[:3] != '>>>':
if linecount > 0:
tspcopystring('@@@') # Skip help action
continue
if s[:6] == ' ':
continue
#--------------------------------------------------------
if len(s) > 0: linecount += 1
endid = s.find('#pic_center')
startid = s.find('![](')
if startid < 0:
startid = s.find("](http")
if startid >= 2:
startid -= 2
mpgid = s.find('.MPG]')
aviid = s.find('.AVI]')
mp4id = s.find('.MP4]')
jpgid = s.find('.JPG]')
bsim = str2bsim(s)
printf('%d:%s'%(len(s),s))
#--------------------------------------------------------
if endid >= 0 and startid >= 0 and mpgid < 0 and aviid < 0 and jpgid < 0:
picurl = s[startid+4:endid]
ext = picurl.split('.')[-1]
filename = picurl.split('/')[-1].split('.')[0]
outfile = ''
if ext == 'gif':
outfile = os.path.join(outdir, '%s.gif'%filename)
outfilejpg = os.path.join(r'd:\temp', '%s.jpg'%filename)
#------------------------------------------------
urlid = picurl.find('_-')
if urlid > 0:
img_data = requests.get(picurl[:urlid]+'.gif').content
with open(outfile, 'wb') as handler:
handler.write(img_data)
if os.path.isfile(outfile):
outfile1 = extractGIFimage(outfile, r'd:\temp')
if os.path.isfile(outfile1):
filestr = tspimagesetstring(outfile1, '')
microfile = os.path.join(microvideo_dir, filestr)
if not os.path.isfile(microfile):
microfile = os.path.join(microvideo_mooc, filestr)
narg = 99
if os.path.isfile(microfile):
tspacceptfile(microfile, 99)
else:
tspacceptfile(outfile1, 99)
tspaction(-16)
os.remove(outfile1)
os.remove(outfile)
continue
elif os.path.isfile(outfilejpg):
filestr = tspimagesetstring(outfilejpg, '')
microfile = os.path.join(microvideo_dir, filestr)
if not os.path.isfile(microfile):
microfile = os.path.join(microvideo_mooc, filestr)
if os.path.isfile(microfile):
tspacceptfile(microfile, 99)
else:
tspacceptfile(outfilejpg, 99)
if bsim == -1: tspaction(-23)
else: tspaction(-(10+bsim))
os.remove(outfilejpg)
continue
else:
urlid = picurl.find('_')
dlflag = 1
if urlid > 0:
if len(picurl[urlid+1:]) < 6: dlflag = 0
picurl = picurl[:urlid] + '.gif'
if dlflag > 0:
img_data = requests.get(picurl).content
with open(outfile, 'wb') as handler:
handler.write(img_data)
tspacceptfile(outfile, 99)
os.remove(outfile)
else:
tspacceptfile(outfile, 99)
#------------------------------------------------
equalid = s.find('=')
widthnumber = 480
if equalid > 0:
xid = s[equalid+1:].find('x')
if xid > 0:
widthnumber = int(s[equalid+1:][:xid])
if widthnumber > 400 or widthnumber < 170 or urlid > 0:
tspaction(-16)
elif ext == 'png':
pngfile = picurl.split('/')[-1]
nutdiskfile = os.path.join(r'd:\Nutdisk\MicroVideo', pngfile)
if os.path.isfile(nutdiskfile):
outfile = nutdiskfile
else:
outfile = os.path.join(outdir, '%s.png'%filename)
img_data = requests.get(picurl).content
with open(outfile, 'wb') as handler:
handler.write(img_data)
# printf(outfile)
# urllib.request.urlretrieve(picurl, outfile)
im1 = Image.open(outfile)
if len(im1.split()) == 4:
r,g,b,a = im1.split()
im1 = Image.merge('RGB', (r,g,b))
newfile = os.path.join(outdir, '%s.JPG'%filename)
im1.save(newfile)
#------------------------------------------------
if os.path.isfile(newfile):
img = cv2.imread(newfile)
H,W = shape(img)[:2]
outzipfile = r'd:\temp\1.zip'
printff(H,W)
if os.path.isfile(outzipfile):
os.remove(outzipfile)
if W/H > 50:
filename = img2datafile(newfile, outzipfile)
if os.path.isfile(outzipfile):
ext = filename.split('.')[-1].upper()
if ext == 'ZIP':
dopfile = tspgeteditfile()
id = dopfile.find('.dop')
if id >= 0: dopdir = dopfile[:id]
else: dopdir = dopfile
bsirname = zip2dir(outzipfile, dopdir)
printf(bsirname)
tspacceptfile(bsirname)
tspcopystring(">>")
os.remove(outzipfile)
os.remove(outfile)
os.remove(newfile)
continue
#------------------------------------------------
if picselflag > 0:
image2clipboard(newfile)
tspcopystring('PICSELOPEN')
time.sleep(1)
else:
tspacceptfile(newfile, 0)
os.remove(outfile)
os.remove(newfile)
#------------------------------------------------
equalid = s.find('=')
widthnumber = 480
if equalid > 0:
xid = s[equalid+1:].find('x')
if xid > 0:
widthnumber = int(s[equalid+1:][:xid])
if widthnumber > 400 and widthnumber < 440:
pngappendshift = '{60,1500,50,50}'
elif widthnumber < 320 and widthnumber > 240:
pngappendshift = '{80,1500,1,0}'
elif widthnumber > 720:
tspaction(-16)
else: pngappendshift = ''
elif s[:2] == '![' and jpgid > 0:
s = s[2:jpgid+4]
bsim = -1
if s.find('-') >= 0:
ss = s.split('-')
s = ss[-1]
smode = '![%s]'%ss[0]
bsim = str2bsim(smode)
jpgfile = os.path.join(outdir, s)
if not os.path.isfile(jpgfile):
jpgfile = os.path.join(microvideo_dir, s)
printf(jpgfile)
if os.path.isfile(jpgfile):
filestr = tspimagesetstring(jpgfile, '')
microfile = os.path.join(microvideo_dir, filestr)
if not os.path.isfile(microfile):
microfile = os.path.join(microvideo_mooc, filestr)
if os.path.isfile(microfile):
tspacceptfile(microfile, 99)
else:
tspacceptfile(jpgfile)
os.remove(jpgfile)
if bsim != -1: tspaction(-(10+bsim))
elif s[:2] == '![' and mpgid > 0:
s = s[2:mpgid+4]
sid = s.find('|')
if sid > 0: s = s[:sid]
mpegfile = os.path.join(microvideo_mooc, s)
if not os.path.isfile(mpegfile):
mpegfile = os.path.join(microvideo_dir, s)
if os.path.isfile(mpegfile):
tspacceptfile(mpegfile, 99)
printf(mpegfile)
elif s[:2] == '![' and aviid > 0:
s = s[2:aviid+4]
sid = s.find('|')
if sid > 0: s = s[:sid]
avifile = os.path.join(microvideo_mooc, s)
if not os.path.isfile(avifile):
avifile = os.path.join(microvideo_dir, s)
if os.path.isfile(avifile):
tspacceptfile(avifile, 99)
printf(avifile)
elif s[:2] == '![' and mp4id > 0:
s = s[2:mp4id+4]
sid = s.find('|')
if sid > 0: s = s[:sid]
mp4file = os.path.join(microvideo_mooc, s)
if not os.path.isfile(mp4file):
mp4file = os.path.join(microvideo_dir, s)
if os.path.isfile(mp4file):
tspacceptfile(mp4file, 99)
printf(mp4file)
else:
microfile = os.path.join(microvideo_dir, s)
if os.path.isfile(microfile):
if s.find('.MPG') >= 0 and s.find('#') < 0:
mpegfile = os.path.join(microvideo_dir, s)
tspacceptfile(mpegfile, 99)
printf(mpegfile)
elif s.find('.MP3') >= 0 and s.find('#') < 0:
audiofile = os.path.join(microvideo_dir, s)
tspacceptfile(audiofile, 99)
printf(audiofile)
elif s.find('.MP4') >= 0 and s.find('#') < 0:
mp4file = os.path.join(microvideo_dir, s)
tspacceptfile(mp4file, 99)
printf(mp4file)
elif s.find('.WAV') >= 0 and s.find('#') < 0:
audiofile = os.path.join(microvideo_dir, s)
tspacceptfile(audiofile, 99)
printf(audiofile)
else:
if len(s) == 0:
if linecount > 0:
tspcopystring('@@@') # Skip help action
continue
#------------------------------------------------
if s.find('<>') >= 0:
tspaction(-10)
s = s.replace('<>', "")
if s.find('><') >= 0:
tspaction(-16)
s = s.replace('><', "")
if s.find('<<<') >= 0:
tspaction(-16)
s = s.replace('<<<', ">>>")
#------------------------------------------------
if s.find('{A}') >= 0:
s = s.replace('{A}', '')
tspaction(-14)
if s.find('{F}') >= 0:
s = s.replace('{F}', '')
tspaction(-14)
if s.find('{R}') >= 0:
s = s.replace('{R}', '')
tspaction(-13)
if s.find('{L}') >= 0:
s = s.replace('{L}', '')
tspaction(-12)
if s.find('{Q}') >= 0:
s = s.replace('{Q}', '')
tspaction(-16)
if s.find('{N}') >= 0:
s = s.replace('{N}', '')
tspaction(-10)
if s.find('{S}') >= 0:
s = s.replace('{S}', '')
tspaction(-17)
if s.find('{P}') >= 0:
s = s.replace('{P}', '')
tspaction(-15)
#------------------------------------------------
# s = s.replace('。【', '。 【').replace(',【', ', 【').replace(':【', ': 【')
# s = s.replace('?【', '? 【').replace('!【', '! 【').replace('、【', '、 【')
# s = s.replace('.【', '. 【').replace(',【', ', 【').replace(':【', ': 【')
# s = s.replace('?【', '? 【').replace('!【', '! 【').replace("**", "").replace(' ', "~ ")
s = s.replace(' ', "~ ^^").replace('【】', "~ ^^").replace(' ', "~ ^^")
sdim = [s]
#------------------------------------------------
for ss in ["。", ":", ",", "?", "!", "、", ";", ",", ":", "?", "!", "~"]:
sss = ss + ' '
tdim = []
if ss == '~': ss = ''
for l in sdim:
l = l.split(sss)
if len(l) > 1:
tdim.extend([ll+ss if id < len(l)-1 else ll for id,ll in enumerate(l)])
else: tdim.extend(l)
sdim = tdim
tdim = []
for ss in sdim:
sss = ss.split(' ')
for ssss in sss:
if len(ssss.strip(' ')) > 0:
tdim.append(ssss.strip(' '))
sdim = tdim
#------------------------------------------------
ssdim = []
for s in sdim:
if len(s) < MAX_LENGTH: ssdim.append(s)
else:
ss = divideSentence(s)
ssdim.extend(ss)
sdim = ssdim
printf(sdim)
#------------------------------------------------
for s in sdim:
s = s.strip(' ')
nextflag = 0
if s[-2:-1] == ' ':
nextflag = 1
s = s[:-2] + s[-1:]
if s[:2] == '^^':
nextflag = 0
tspcopystring('@@@')
s = s[2:]
if s.find('【') >= 0:
startid = s.find('【')
endid = s[startid+1:].find('】')
if startid >= 0 and endid >= 0:
sss = s[startid+1:][:endid]
ssid = sss.find('-')
if ssid > 0: sss = sss[:ssid]
flag = 0
microfile = os.path.join(microvideo_dir, sss)
if not os.path.isfile(microfile):
microfile = os.path.join(microvideo_mooc, sss)
if os.path.isfile(microfile):
if microfile.find('.ZIP') > 0:
dopfile = tspgeteditfile()
id = dopfile.find('.dop')
if id >= 0: dopdir = dopfile[:id]
else: dopdir = dopfile
bsirname = zip2dir(microfile, dopdir)
printf(bsirname)
s = ''.join([a.split('】')[-1] for a in s.split('【')])
if len(s) > 5: tspcopystring(s)
tspacceptfile(bsirname)
tspcopystring(">>")
continue
else:
tspacceptfile(microfile, 99)
printf(microfile)
else:
for ssss in sss:
if ord(ssss) > 0x100:
flag = 1
break
if flag == 0:
tspgetactionfile(sss)
s = ''.join([a.split('】')[-1] for a in s.split('【')])
tspcopystring('@@@')
if len(pngappendshift) > 0 and len(s) > 0:
s = pngappendshift + s
pngappendshift = ''
# printf(s)
s = s.replace(' ', '')
tspcopystring(s)
if nextflag > 0: tspcopystring('@@@')
#------------------------------------------------------------
if picselflag > 0:
tspcopystring('PICSELCLOSE')
printff(bsirnextflag, sharpflag)
if bsirnextflag > 0 and sharpflag == 0:
tspaction(9999)
printf('\a')
tspfocuswindow("TEASOFT")
clipboard.copy('DOPASTE END')
#------------------------------------------------------------
# END OF FILE : DOPASTE.PY
#============================================================
※ 总 结 ※
在这里编写了对 CSDN 视频脚本文件 一键导入 TEASOFT的Python命令。 这可以大大加快完成视频拍摄的过程。