電子竹林:Blog2

Tech関係、はてなダイアリーから移転済み...

「はてなダイアリー」から「はてなブログ」移行へのメモ

  1. 標準的なインポートをやってみる -> (問題点) (id:zom-1:20200819#p1といった#を使うリンクが正しくされない -> MT形式でのインポートに変更
  2. はてなダイアリーでMT形式でエクスポート、はてなブログでインポートをやってみる -> (問題点) エントリー数が大きすぎてインポートできない、サポートに連絡するが返事がなかなか来ないので自力でやることにする -> Pythonで簡単なプログラムを書いて、1000ずつに分割する
  3. はてなダイアリーでMT形式でエクスポート、1000ずつに分割してはてなブログでインポートをやってみる -> (問題点) 同じ日にエントリーがある場合、すべての時間が 00:00:00になっているために上書きされ、1日1エントリーしか書き込まれない -> Pythonで簡単なプログラム作って、複数のエントリーがある場合は最初00:00:001、二つ目は00:00:002 のようにする。同時にリンクを http://d.hatena.ne.jp/zom-1/20200819#p1 など#のリンクがある場合に https://zom1.hatenablog.com/entry/2020/08/19/0001" に変更する。http://d.hatena.ne.jp/zom-1/20200819/ の場合には https://zom-1.hatenablog.com/archive/2020/08/19 にしておく
  4. 以上でとりあえずOKみたい

変換したプログラムは適当だけどこんな感じ

import re

# replace link :  http://d.hatena.ne.jp/zom-1/20190101#p2 -> https://zom-1.hatenablog.com/entry/2019/01/01/0002
pat1 =  r'http://d.hatena.ne.jp/zom-1/([1-2][0-9][[0-9][0-9])([0-1][0-9])([0-3][0-9])#p([0-9]+)'
repl1 = r'https://zom-1.hatenablog.com/entry/\1/\2/\3/'+('000000'+r'\4')[-7:]  ## @@ 
# replace link 2:   http://d.hatena.ne.jp/zom-1/20190101/ -> https://zom-1.hatenablog.com/archive/2019/01/01
pat2 = r'http://d.hatena.ne.jp/zom-1/([1-2][0-9][[0-9][0-9])([0-1][0-9])([0-3][0-9])/'
repl2 = r'https://zom-1.hatenablog.com/archive/\1/\2/\3'

outFileCount = 0 # output file number 
entryCount = 0 # entry number of one file
outName = 'hatena_' + str(outFileCount) + '.txt' # hatena_1.txt , hatena_2.txt ...
outFile = open(outName, "w", encoding='utf-8')  
print(outName)

with open("hatena.txt", "r", encoding='utf-8') as mtFile:
  theDay = '' # dummy
  entryCountOfTheDay = 1 # dummy
  for line in mtFile:
    if line == "--------\n": # top of entry @@
      entryCount += 1

      # entry size check, entry size problem (max 1000)
      if entryCount >= 1000: # next out file
        entryCount = 0
        outFile.close()
        # new file
        outFileCount += 1
        outName = 'hatena_' + str(outFileCount) + '.txt'
        print(outName)
        outFile = open(outName, 'w', encoding='utf-8')

        # DATE check, same day problem
    if line[:5] == 'DATE:':
      if theDay != line: # entry of another day 
          entryCountOfTheDay = 1
          theDay = line
      else: # entry of same day
          # DATE: MM/DD/YYYY 00:00:nn AM , nn=entryCountOfTheDay
          entryCountOfTheDay += 1
      # 1st entry 0001, 2nd entry 0002 ....
      line = re.sub(r'00:00:00', '00:00:'+('0'+str(entryCountOfTheDay))[-2:], line)

    # link check
    ret = re.subn(pat1, repl1, line)
    if ret[1] != 0: # ret is not None:
      line = ret[0]
    ret = re.subn(pat2, repl2, line)
    if ret[1] != 0: # ret is not None:
      line = ret[0]

    # output
    outFile.write(line) 
  outFile.close()