hiiw

itestURL2jane.vbs

Jun 30th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' itestURL2jane.vbs
  2. ' ■itestURL2jane.vbsについて
  3. ' itest.5ch.net のURL を実際の5cn URL に変えてJaneで開く
  4. ' command.dat を使用できる jane で共通して利用可能
  5. ' 次のような2つの形式に対応しています
  6. ' http://itest.5ch.net/corn/test/read.cgi/entrance/990712570/
  7. ' http://itest.5ch.net/test/read.cgi/entrance/1592129652
  8. ' httpsにも対応、itest.2ch.netにも対応しています
  9. ' 変換後のURLはhttpとしています
  10. ' 20200630
  11.  
  12. ' ■使用方法
  13. ' このスクリプトを itestURL2jane.vbs として保存し
  14. ' command.datに
  15. ' itestURL2jane=wscript "itestURL2jane.vbs" {jane_exe} {curl_exe} {itest_url} [2ch.net|5ch.net]
  16. ' のように記述する
  17. ' itestURL2jane.vbs : このスクリプト
  18. '   Jane2ch.exe と同じディレクトリにおいた場合以外はフルパス名で指定
  19. ' jane_exe : janeのexeファイル名
  20. ' curl_exe : curlのexeファイル名
  21. '   curlがない場合は https://curl.haxx.se/ からダウンロードしてください
  22. '   jane_exe curl_exeは原則フルパス名だが、
  23. '   パスが通っているかカレントディレクトリならフルパス不用
  24. ' itest_url : itestのURL
  25. '   "http://itest.5ch.net/test/read.cgi/entrance/1592129652" などだが
  26. '   command.dat では $LINK
  27. ' 2ch.net|5ch.net : 2ch.netまたは5ch.netの指定はどちらのドメインで開くかの指定
  28. '   省略すればもとのiltestでの指定に従うが
  29. '   Janeが2ch.netまたは5ch.netを扱えるように設定されていない場合、対象外のURLとして無視される
  30. '
  31. ' jane での操作は
  32. ' 表示しているスレのitestリンク上で右クリックしメニューからitestURL2janeを選択する
  33.  
  34. ' ■具体的なcommand.dat設定例
  35. ' itestURL2jane=wscript "itestURL2jane.vbs" "Jane2ch.exe" "curl.exe" "$LINK" 5ch.net
  36.  
  37. '■テストに使用したitestリンクのあるスレ
  38. 'http://rosie.5ch.net/test/read.cgi/famicom/1593440400/
  39. 'https://rio2016.5ch.net/test/read.cgi/twwatch/1545191721/
  40. 'https://rio2016.5ch.net/test/read.cgi/twwatch/1589292051/
  41. 'https://rio2016.5ch.net/test/read.cgi/twwatch/1589360808/
  42. 'http://egg.5ch.net/test/read.cgi/smartphone/1507034881/
  43.  
  44. Option Explicit
  45.  
  46. Dim exit_code
  47. exit_code = 0
  48. if 3>WScript.Arguments.Count then
  49.     WScript.echo("パラメータが不足しています")
  50.     WScript.echo("itestURL2jane.vbs {jane_exe} {curl_exe} {itest_url} [2ch.net|5ch.net]")
  51.     WScript.Quit(exit_code)
  52. end if
  53.  
  54. Dim src_url, new_url, domain
  55. Dim curl_exe, jane_exe
  56.  
  57.  
  58. jane_exe = WScript.Arguments(0) 'jane_exe = "C:\jane\JaneXeno2ch.exe" など
  59. curl_exe = WScript.Arguments(1) 'curl_exe = "C:\test\curl.exe" など
  60. src_url = WScript.Arguments(2)  ' "http://itest.5ch.net/test/read.cgi/entrance/1592129652" など
  61. domain = ""
  62. if 3<WScript.Arguments.Count then
  63.     domain = WScript.Arguments(3)
  64. end if
  65.  
  66.  
  67. Dim Matches
  68. Set Matches = RexExecute(src_url, "https?://itest\.([25]ch\.net)/([a-z0-9]+)(/test/read.cgi/[^/]+/[0-9]+/?)", True, True)
  69. 'Set Matches = RexExecute(src_url, "https?://itest\.([25]ch\.net)(/test/read.cgi/[^/]+/[0-9]+/?)", True, True)
  70. If ( 1 = Matches.Count ) Then
  71.     ' サイトに問い合わせなくても変換できるタイプ
  72.     ' https://itest.5ch.net/corn/test/read.cgi/entrance/990712570/
  73.     exit_code = 10
  74. '   MsgBox Matches(0).Value
  75. '   MsgBox Matches(0).SubMatches.Count
  76. '   MsgBox Matches(0).SubMatches(0)
  77. '   MsgBox Matches(0).SubMatches(1)
  78. '   MsgBox Matches(0).SubMatches(2)
  79.     If ( "" = domain ) Then
  80.         new_url = "http://" & Matches(0).SubMatches(1) & "." & Matches(0).SubMatches(0) & Matches(0).SubMatches(2)
  81.     Else
  82.         new_url = "http://" & Matches(0).SubMatches(1) & "." & domain & Matches(0).SubMatches(2)
  83.     End If
  84. Else
  85.     ' サイトに問い合わせないと変換できないタイプ
  86.     'http://itest.5ch.net/test/read.cgi/entrance/1592129652
  87.     exit_code = 20
  88.     new_url = itest_url_by_site(src_url, domain)
  89. End If
  90. 'MsgBox "new_url=" & new_url
  91.  
  92. If ( "" = new_url ) Then
  93.     WScript.echo( "URLを変換できません。" )
  94. Else
  95.     exit_code = exit_code + 1
  96. End If
  97.  
  98. RunExe jane_exe & " " & new_url, 1, False
  99.  
  100. 'Quitメソッドの引数値は、バッチファイルでerrorlevelになる。
  101. Wscript.Quit(exit_code)
  102.  
  103. Function itest_url_by_site(src_url, domain)
  104.     itest_url_by_site = ""
  105.     Dim Matches
  106.     Set Matches = RexExecute(src_url, "https?://itest\.([25]ch\.net)(/test/read.cgi/[^/]+/[0-9]+/?)", True, True)
  107.     If (0 = Matches.Count) Then
  108.         Exit Function
  109.     End If
  110.  
  111.     Dim access_url,temp_file
  112.     If ("2ch.net" = Matches(0).SubMatches(0)) Then
  113.         ' もとのurlがitest.2ch.netの場合アクセスできないので5ch.netに
  114.         access_url = "https://itest.5ch.net" & Matches(0).SubMatches(1)
  115.     Else
  116.         access_url = src_url
  117.     End If
  118.     temp_file = GetEnvironmentString("TEMP") & "\itest2jane_temp.txt"
  119.  
  120.     ' curl を使い itest のURLにアクセスし戻ってきたページソースをtemp_fileとする
  121.     RunExe curl_exe & " -s " & access_url & " -o " & temp_file, 0, True
  122.  
  123.     ' 以下itestのページソースからホスト名を取り出す
  124.     Dim objStreamIn
  125.     Set objStreamIn = CreateObject("ADODB.Stream")
  126.  
  127.     ' 文字コードを設定
  128.     objStreamIn.Charset = "UTF-8"
  129.  
  130.     ' ファイルオープン
  131.     objStreamIn.Open
  132.  
  133.     ' 入力ファイルを読み込む'
  134.     'objStreamIn.LoadFromFile "F:\temp\work\PathScan\ii.txt"
  135.     objStreamIn.LoadFromFile temp_file
  136.     ' データを変数に格納する
  137.  
  138.     'ReadText(-1)でテキスト全文を、ReadText(-2)でテキスト1行を読み込むことができます。
  139.     ' CR+LFしか改行として認識されない
  140.     ' ここで扱うitestのページソースはLFのみなので-2でも実質-1と変わらない
  141.     Dim strData
  142.     Dim webMatches
  143.     Do while (1)
  144.         strData = objStreamIn.ReadText(-2)
  145.         if ("" = strData) Then
  146.             exit Do
  147.         End If
  148.  
  149.         Set webMatches = RexExecute(strData, "pchost = '([a-z0-9]+)\.([25]ch\.net)';", True, True)
  150.         'MsgBox webMatches.Count
  151.         if (1 = webMatches.Count) Then
  152.             exit Do
  153.         End If
  154.     Loop
  155.  
  156.     if (1 = webMatches.Count) Then
  157.         'MsgBox webMatches(0).Value
  158.         'MsgBox webMatches(0).SubMatches.Count
  159.         'MsgBox webMatches(0).SubMatches(0)
  160.         If ( "" = domain ) Then
  161.             itest_url_by_site = "http://" & webMatches(0).SubMatches(0) & "." & Matches(0).SubMatches(0) & Matches(0).SubMatches(1)
  162.         Else
  163.             itest_url_by_site = "http://" & webMatches(0).SubMatches(0) & "." & domain & Matches(0).SubMatches(1)
  164.         End if
  165.     End if
  166. '   MsgBox itest_url_by_site
  167.  
  168.     ' ファイルクローズ
  169.     objStreamIn.Close
  170.  
  171.     Set objStreamIn = Nothing
  172.  
  173.     ' temp_file を削除
  174.     FileDelete temp_file, False
  175.  
  176. End Function
  177.  
  178. Function RexExecute(str, pattern, IgnoreCase, Global)
  179.     Dim regEx
  180.     Set regEx = New RegExp
  181.     regEx.Pattern = pattern            ' パターンを設定します。
  182.     regEx.IgnoreCase = IgnoreCase           ' 大文字と小文字を区別する
  183.     regEx.Global = Global
  184.     regEx.MultiLine = True
  185.  
  186.     Set RexExecute = regEx.Execute(str)
  187. End Function
  188.  
  189. Function FileDelete(file, bDeleteReadOnly)
  190.     Dim objFso
  191.     Set objFso = CreateObject("Scripting.FileSystemObject")
  192.     objFso.DeleteFile file, bDeleteReadOnly
  193.     Set objFso = Nothing
  194. End Function
  195.  
  196. Function GetEnvironmentString(variable_name)
  197.     Dim objWShell
  198.     Set objWShell = CreateObject("WScript.Shell")
  199.     GetEnvironmentString = objWShell.ExpandEnvironmentStrings("%"&variable_name&"%")
  200.     Set objWShell = Nothing
  201. End Function
  202.  
  203. Function RunExe(exe_and_param, ShowStyle, bWaitTerminate)
  204. 'exe_and_param
  205. ' 実行するexeファイルと' 'で区切られたパラメータ
  206. 'ShowStyle
  207. '0 ウィンドウを非表示
  208. '1 通常のウィンドウで、最前面のウィンドウ
  209. '2 最小化で、最前面のウィンドウ
  210. '3 最大化で、最前面のウィンドウ
  211. '4 通常のウィンドウで、最前面ではない
  212. '6 最小化で、最前面にはならない
  213. 'bWaitTerminate
  214. 'True - コマンドの終了を待つ。
  215. 'False - コマンドの終了を待たずスクリプトを実行する。
  216.     Dim objWShell
  217.     Set objWShell = CreateObject("WScript.Shell")
  218.     objWShell.Run exe_and_param, ShowStyle, bWaitTerminate
  219.     Set objWShell = Nothing
  220.     RunExe = 0
  221. End Function
Add Comment
Please, Sign In to add comment