(8)URL変更スクリプト
一行に複数の自記事URLに対応後、さらにページ内リンク対応した
スクリプトを修正
cnvlink4.py
#!/usr/bin/python3
import urllib.request
import re
infile="20250428-export_blog_1.txt"
infiles=infile.split('.')
outfile=infiles[0]+"-conv."+infiles[1]
urllink1="https://blog.goo.ne.jp/gooアカウント名"
def getdate(gurl):
pat1=r'<span class="entry-top-info-time">(\d+)-(\d+)-(\d+)\s+(\d+):(\d+):(\d+)</span>'
req=urllib.request.Request(gurl)
try:
with urllib.request.urlopen(req) as res:
body=res.read().decode("utf-8")
res1=re.search(pat1,body,re.S) #
if res1:
hurl="/entry/"+res1.group(1)+"/"+res1.group(2)+"/"+res1.group(3)+"/"+res1.group(4)+res1.group(5)+res1.group(6)
return hurl
else:
hurl="may be document style error"
return 1
except urllib.error.HTTPError as e:
if e.code >= 400:
print("HTTP("+str(e.code)+") "+e.reason)
else:
raise e
return 1
cnt=0
cnturl=0
outlines=""
p=re.compile(r'<a href="([\w\.:/]*?)/gooアカウント名(/\w/\w+)(#?\w*)">')
with open(infile, encoding="utf-8") as f:
for line in f:
cnt=cnt+1 #lines
while True:
m=p.search(line)
if m:
print("GOO link: "+m.group(0))
rs=getdate(urllink1+m.group(2))
if isinstance(rs,str):
print("HATENA link: "+rs.+m.group(3))
cnturl=cnturl+1 #conerted urls
print("Converted: "+str(cnturl)+" urls@lineNumber "+str(cont)+"\n")
line=line[:m.start()]+"<!--/gooアカウント名"+m.group(2)+"--><a href=\"https://はてなblog名.hatenadiary.jp"+rs+"\">"+line[m.end():]
else:
line=line[:m.start()]+"<!--/gooアカウント名"+m.group(2)+"--><a href=\"https://はてなblog名.hatenadiary.jp/\">"+line[m.end():]
else:
break #escap from while
outlines=outlines+line
with open(outfile, mode='w') as fo:
fo.write(outlines)
877個のurl変換に加え11個のページ内url変換が加わり888個のurl変換となった。
(9)コメント内のURLリンク
・gooでコメント内に記事へのurlリンクが記載されていた(1件)。MT形式データをcoteditorでhatenaの記事リンクへ修正
・記事からコメントへのurlリンクが記載されていた。「はてな」ではコメント内にurlアンカー記載が出来ない。MT形式データに「<a name="someAnchor">」としたが、インポート後「<a>」で削除された。「id」も削除され対応出来なかった。