とりあえず Amazon のリンクを全部書き直すスクリプト書いてみたものの、ブログの設定を直すのを忘れていたのではてなダイアリーではてな記法で書いていたものがすべて markdown になってしまった。うーむ。はてなの markdown は markdown 風はてな記法でどっちかというと使いづらいのではてな記法に戻したほうがよかったな。まぁしかたない。しかも途中で気付いて、スクリプトを止めて設定変更したつもりになってたものの、再度確認してみるとやっぱり markdown になっていた。うーむむむ。つらい。
ついでなので、こんなもん書いたということでのせときます。XMLの書式が微妙に違っててもXMLの仕様に沿ってればちゃんと解釈するので、(株)はてなは文字化けの対応程度のこともしてくれないくせにちゃんとやってるなと感心した。あと、全部書き換えるの自体はあっという間に終わった。すごく楽だがうっかりミスのせいでこれからはてな書式をmarkdownに書き換えていかないといけない。面倒くさい。
とりあえずアマゾンのリンクの整理にしか使ってませんが、たとえばカテゴリーの整理(表記の統一とか)みたいなことでもちょっといじったらできるようにしてあります。ただ自作スクリプトからコピペして改変したのでこのままで動くかどうか...
import requests from lxml import etree as eT sHid = "はてなID" sBid = "ブログID" sKey = "APIキー" #コレクションURI sCol = "https://blog.hatena.ne.jp/{}/{}/atom/entry".format(sHid,sBid) sNextPage = sCol while sNextPage != "": oRes = requests.get(url=sNextPage,auth=(sHid,sKey)) sNextPage = "" oR = eT.fromstring(oRes.content) dN = {'atom':'http://www.w3.org/2005/Atom'} oE = oR.xpath('/atom:feed/atom:entry',namespaces=dN) if len(oE): for eE in oE: sEid = "" sThisEntry = "" sAuthor = "" sTitle="" sContent = "" sContentType = "" aCategory = [] sUpdated = "" sPublished = "" sDraft = "" for ee in eE: sTag = ee.tag if sTag == "{http://www.w3.org/2005/Atom}link" : if ee.get("rel") == "edit": sThisEntry = ee.get("href") sEid = sThisEntry.split("/")[-1] elif sTag == "{http://www.w3.org/2005/Atom}author" : sAuthor = ee[0].text elif sTag == "{http://www.w3.org/2005/Atom}title" : sTitle = ee.text elif sTag == "{http://www.w3.org/2005/Atom}updated" : sUpdated = ee.text elif sTag == "{http://www.w3.org/2005/Atom}published" : sPublished = ee.text elif sTag == "{http://www.w3.org/2005/Atom}content": sContent = ee.text sContentType = ee.get("type").split("/")[-1] elif sTag == "{http://www.w3.org/2005/Atom}category": aCategory.append(ee.get("term")) elif sTag == "{http://www.w3.org/2007/app}control": sDraft = ee[0].text if sContentType == "x-hatena-syntax" and "amazon-adsystem" in sContent: print(sEid,sContent[:20],sPublished) print(" - start") ##操作の例 sContent = sContent.replace("http://rcm-fe.amazon-adsystem","//rcm-fe.amazon-adsystem") ##ここではアマゾンのリンクのhttpを消している oEntry = eT.Element("entry",nsmap={ None:"http://www.w3.org/2005/Atom", "app":"http://www.w3.org/2007/app" }) oTitle = eT.SubElement(oEntry,"title") oTitle.text = sTitle oAuthor = eT.SubElement(oEntry,"author") oName = eT.SubElement(oAuthor,"name") oName.text = sAuthor oContent = eT.SubElement(oEntry,"content") oContent.attrib['type'] = "text/plain" oContent.text = sContent oUpdated = eT.SubElement(oEntry,"updated") oUpdated.text = sUpdated for sCat in aCategory: oCat = eT.SubElement(oEntry,"category") oCat.attrib["term"] = sCat oControl = eT.SubElement(oEntry,"{app}control") oDraft = eT.SubElement(oControl,"{app}draft") oDraft.text=sDraft sData = eT.tostring(oEntry,xml_declaration=True,encoding='utf-8') oRes = requests.put(url=sThisEntry,auth=(sHid,sKey),data=sData) print(" - ",oRes.ok)
まちがえて違う形式で書きこんでしまった記事はあらためて編集形式をかえてそのまま上書きしていったらかわった。二度手間だったか。
- 作者: Bill Lubanovic,斎藤康毅,長尾高弘
- 出版社/メーカー: オライリージャパン
- 発売日: 2015/12/01
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (3件) を見る
退屈なことはPythonにやらせよう ―ノンプログラマーにもできる自動化処理プログラミング
- 作者: Al Sweigart,相川愛三
- 出版社/メーカー: オライリージャパン
- 発売日: 2017/06/03
- メディア: 単行本(ソフトカバー)
- この商品を含むブログ (6件) を見る