AppleScript m3u8

[mixcloud] mixcloudダウンロード

Screen-20221230-175846
index.m3u8のURLをコピーして実行


#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
(*

m3u8

*)
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions


property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSURL : a reference to refMe's NSURL
property refNSMutableString : a reference to refMe's NSMutableString
property refNSMutableArray : a reference to refMe's NSMutableArray
property refNSDate : a reference to refMe's NSDate
property refNSDateFormatter : a reference to refMe's NSDateFormatter

set objFileManager to refMe's NSFileManager's defaultManager()
property refNSPasteboard : a reference to refMe's NSPasteboard


####################################
###クリップボードを取得
set ocidPasteboard to refNSPasteboard's generalPasteboard()
###クリップボードの中を取得テキストで
set ocidPasteboardTypeString to ocidPasteboard's stringForType:(refMe's NSPasteboardTypeString)
####取得したテキストの格納先を用意(可変テキスト
set ocidCurlURL to refNSMutableString's alloc()'s initWithCapacity:0
####取得したテキストを
ocidCurlURL's setString:ocidPasteboardTypeString

if (ocidCurlURL as text) starts with "curl" then
log "処理開始"
set ocidDoSeparateURLArrayM to doSeparateURL(ocidCurlURL)
set strM3uURL to ocidCurlURL as text
else
return "CURLとしてコピーしてください"
end if
#############################################
###Session-Idが含まれている方が必要なためチェックと
if strM3uURL contains "X-Playback-Session-Id" then
###################
###セッションIDがあればURLとヘッダーに分離して格納
set strUrlText to (item 2 of ocidDoSeparateURLArrayM)'s absoluteString() as text
set ocidM3UURL to refNSURL's URLWithString:strUrlText
set strHeaderText to item 4 of ocidDoSeparateURLArrayM as text
else
error "Session-Idが含まれていません" number -2753 from "Session-Idが含まれていません"
return
end if




####################################
#####保存先
###日付
set ocidDate to refNSDate's |date|()
log ocidDate as date
log className() of ocidDate as text
set ocidNSDateFormatter to refNSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setDateFormat:"yyyyMMddhhmmss"
set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
log ocidDateAndTime as text
log className() of ocidDateAndTime as text
set strDateAndTime to ocidDateAndTime as text

#####保存先のファイルパス
set strTempPath to ("/private/tmp/" & (ocidDateAndTime as text) & "") as text
#####NSStringファイルパス
set ocidDirPath to (refNSString's stringWithString:strTempPath)
#####FileManager
set listBoolMakeDir to objFileManager's createDirectoryAtPath:(ocidDirPath) withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
log class of (item 1 of listBoolMakeDir) as text
set boolMakeDir to (item 1 of listBoolMakeDir) as boolean
log boolMakeDir
if boolMakeDir is false then
error "フォルダ作成に失敗しました" number -2753 from "フォルダ作成に失敗しました"
return
end if



#################################################
###URLからデータを取得
###########################-->index.m3u8
set listDownLoadData to refNSString's stringWithContentsOfURL:ocidM3UURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
-->戻り値がList
####データ部
set ocidGetDataString to (item 1 of listDownLoadData)
####エラー情報(不要な場合は削除可)
set ocidNSErrorData to item 2 of listDownLoadData
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
return "エラー"
end if

#########Arrayの初期化
set ocidM3uArray to (refNSMutableArray's alloc()'s initWithCapacity:0)

###区切り文字を定義
set ocidNSCFCharacterSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:"\n")

###ocidNSCFCharacterSetArray=リスト化
set ocidM3uArray to (ocidGetDataString's componentsSeparatedByCharactersInSet:ocidNSCFCharacterSet)

#####URL判定用の値の初期化
set strM3U8url to "" as text
set numCntLineNo to 0 as number
####読み込んだファイルデータの行分だけ繰り返し
repeat with objM3uArray in ocidM3uArray
set strM3uArray to objM3uArray as text
set numCntURL to numCntLineNo + 1 as number
###それぞれのURLを格納する
if strM3uArray contains "#EXT-X-STREAM-INF" then
#####EXT-X-STREAM-INFの次の行のデータ=URLを取得する
set strM3U8url to (ocidM3uArray's objectAtIndex:numCntURL) as text
end if
set numCntLineNo to numCntLineNo + 1 as number
end repeat

set ocidM3UURL to refNSURL's URLWithString:strM3U8url
set ocidBaseURL to ocidM3UURL's URLByDeletingLastPathComponent
set strBaseUrl to ocidBaseURL's absoluteString() as text


#################################################
###URLからデータを取得 -->streamindex

set listDownLoadData to refNSString's stringWithContentsOfURL:ocidM3UURL encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
-->戻り値がList
####データ部
set ocidGetDataString to (item 1 of listDownLoadData)
####エラー情報(不要な場合は削除可)
set ocidNSErrorData to item 2 of listDownLoadData
if ocidNSErrorData is not (missing value) then
doGetErrorData(ocidNSErrorData)
return "エラー"
end if

######Arrayの初期化
set ocidM3u8Array to (refNSMutableArray's alloc()'s initWithCapacity:0)
log ocidM3u8Array as list
log className() of ocidM3u8Array as text
###区切り文字を定義
set ocidNSCFCharacterSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:"\n")
log className() of ocidNSCFCharacterSet as text
###ocidNSCFCharacterSetArray=リスト化
set ocidM3u8Array to (ocidGetDataString's componentsSeparatedByCharactersInSet:ocidNSCFCharacterSet)
log ocidM3u8Array as list
log className() of ocidM3u8Array as text

###Arrayの数を数える
set numReadFileText to count of ocidM3u8Array
###ファイル名
set numFileName to 10000 as number
###ファイル番号のカウントアップ
set numFileCnt to 0 as number

####読み込んだデータの行数だけ々
repeat with objM3u8Array in ocidM3u8Array
###テキストに
set strM3u8Array to objM3u8Array as text
#####
#####set cntHas to the offset of "#" in strM3u8Array
##### #で始まるのはコメント行
if strM3u8Array starts with "#EXT-X-ENDLIST" then
log "m3u最終行"
exit repeat
else if strM3u8Array starts with "#" then
log "コメント行"
else
###URLが格納されているので整形
set numFileName to numFileName + 1 as number
set strTsURL to strM3u8Array as text
set strTsFileName to (numFileName & ".ts")
set strTsDownPath to (strTempPath & "/" & strTsFileName) as text
####ダウンロード
set strCommandText to "/usr/bin/curl '" & strTsURL & "' -o '" & strTsDownPath & "' " & strHeaderText & "" as text
log strCommandText
do shell script strCommandText
end if
end repeat

####終わったら ダウンロードしたデータをデスクトップに結合して終了
do shell script "cat " & strTempPath & "/* >> ~/Desktop/" & (ocidDateAndTime as text) & ".ts"





to doSeparateURL(ocidCurlURL)
###戻り値用のリスト
set ocidDoSeparateURLArrayM to refNSMutableArray's alloc()'s initWithCapacity:0

###改行を取る
set ocidCurlURLLength to ocidCurlURL's |length|()
set ocidCurlURLRange to {location:0, |length|:ocidCurlURLLength}
ocidCurlURL's replaceOccurrencesOfString:("\n") withString:("") options:(refMe's NSRegularExpressionSearch) range:ocidCurlURLRange
###'』でリスト化して2番目がURL
set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"'"
set ocidCurlURLArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters

###URL確定(クエリー入り)
set ocidURLString to ocidCurlURLArray's objectAtIndex:1
set ocidURL to refNSURL's URLWithString:ocidURLString
log ocidURL's absoluteString() as text
ocidDoSeparateURLArrayM's addObject:ocidURL

###URL部分(クエリー無し)
set ocidURLComponents to (refMe's NSURLComponents)'s componentsWithURL:ocidURL resolvingAgainstBaseURL:true
ocidURLComponents's setQueryItems:(missing value)
set ocidBaseURL to ocidURLComponents's |URL|
log ocidBaseURL's absoluteString() as text
ocidDoSeparateURLArrayM's addObject:ocidBaseURL

###クエリー部分
set ocidQueryName to ocidURL's query()
log ocidQueryName as text
if ocidQueryName is missing value then
ocidDoSeparateURLArrayM's addObject:""
else
ocidDoSeparateURLArrayM's addObject:ocidQueryName
end if
###ヘッダー部処理
set ocidDelimiters to (refMe's NSCharacterSet)'s characterSetWithCharactersInString:"\\"
set ocidHeaderArray to ocidCurlURL's componentsSeparatedByCharactersInSet:ocidDelimiters
set ocidHeaderArrayLength to ocidHeaderArray's |count|()
set ocidHeaderRange to refMe's NSMakeRange(1, (ocidHeaderArrayLength - 1))
set ocidSubbArray to ocidHeaderArray's subarrayWithRange:ocidHeaderRange
set ocidHeaderText to (ocidSubbArray's componentsJoinedByString:"")

log ocidHeaderText as text
ocidDoSeparateURLArrayM's addObject:ocidHeaderText
###値を戻す
return ocidDoSeparateURLArrayM

end doSeparateURL

|

[VK]VKビデオダウンロード

VK(ロシアのSNS)に上がっているビデオをダウンロードします
m3u8ファイルが3種類あって
今回はvideo.m3u8からダウンロードします
video.m3u8の解像度別のURLから最高解像度のm3u8をダウンロードして
TSファイルをダウンロードします



対象のビデオを再生中にm3u8のURLを取得します。
↓この値をクリップボードに入れた状態でスタートしてください
20220924110113_1862x988x1440



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

######ログ表示
doLogView()

property refMe : a reference to current application
property refNSString : a reference to refMe's NSString
property refNSMutableString : a reference to refMe's NSMutableString
property refNSUTF8StringEncoding : a reference to refMe's NSUTF8StringEncoding
property refNSMutableArray : a reference to refMe's NSMutableArray
property refNSArray : a reference to refMe's NSArray
property refNSDate : a reference to refMe's NSDate
property refNSDateFormatter : a reference to refMe's NSDateFormatter

property refNSCharacterSet : a reference to refMe's NSCharacterSet
property refNSRegularExpressionSearch : a reference to refMe's NSRegularExpressionSearch

property refNSNotFound : a reference to 9.22337203685477E+18 + 5807

set objFileManager to refMe's NSFileManager's defaultManager()

###クリックボードの中のURLを取得
tell application "Finder"
set strM3uURL to the clipboard as text
end tell

###Session-Idが含まれている方が必要なため
if strM3uURL contains "X-Playback-Session-Id" then
###日付
set ocidDate to refNSDate's |date|()
log ocidDate as date
log className() of ocidDate as text
set ocidNSDateFormatter to refNSDateFormatter's alloc()'s init()
ocidNSDateFormatter's setDateFormat:"yyyyMMddhhmmss"
set ocidDateAndTime to ocidNSDateFormatter's stringFromDate:ocidDate
log ocidDateAndTime as text
log className() of ocidDateAndTime as text
else
error "Session-Idが含まれていません" number -2753 from "Session-Idが含まれていません"
return
end if



#####保存先のファイルパス
set strTempPath to ("/private/tmp/" & (ocidDateAndTime as text) & "") as text
#####NSStringファイルパス
set ocidDirPath to (refNSString's stringWithString:strTempPath)
#####FileManager
set listBoolMakeDir to objFileManager's createDirectoryAtPath:(ocidDirPath) withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
log class of (item 1 of listBoolMakeDir) as text
set boolMakeDir to (item 1 of listBoolMakeDir) as boolean
log boolMakeDir
if boolMakeDir is false then
error "フォルダ作成に失敗しました" number -2753 from "フォルダ作成に失敗しました"
return
end if


set strM3uDir to ("/private/tmp/" & (ocidDateAndTime as text) & "m3u") as text
#####保存先のファイルパス
set ocidM3uDirPath to (refNSString's stringWithString:strM3uDir)
set listBoolMakeDir to objFileManager's createDirectoryAtPath:(ocidM3uDirPath) withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
log class of (item 1 of listBoolMakeDir) as text
set boolMakeDir to (item 1 of listBoolMakeDir) as boolean
log boolMakeDir

if boolMakeDir is false then
error "フォルダ作成に失敗しました" number -2753 from "フォルダ作成に失敗しました"
return
end if


set strM3u8Dir to ("/private/tmp/" & (ocidDateAndTime as text) & "m3u8") as text
#####保存先のファイルパス
set ocidM3u8DirPath to (refNSString's stringWithString:strM3u8Dir)
set listBoolMakeDir to objFileManager's createDirectoryAtPath:(ocidM3u8DirPath) withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
log class of (item 1 of listBoolMakeDir) as text
set boolMakeDir to (item 1 of listBoolMakeDir) as boolean
log boolMakeDir

if boolMakeDir is false then
error "フォルダ作成に失敗しました" number -2753 from "フォルダ作成に失敗しました"
return
end if


###初期化 空の可変テキストを作成
set ocidNSMutableString to refNSMutableString's alloc()'s initWithCapacity:0
log className() of ocidNSMutableString as text
###可変テキスト
set ocidNSMutableString to refNSMutableString's stringWithString:strM3uURL
log ocidNSMutableString as text
log className() of ocidNSMutableString as text
#########################
###↑文字数を数える
set numCntNSMutableString to ocidNSMutableString's |length|()
###↑文字数全部でレンジにする
set ocidNsRange to {location:0, |length|:numCntNSMutableString}
log class of ocidNsRange
log ocidNsRange as record
###改行除去
ocidNSMutableString's replaceOccurrencesOfString:("\n") withString:("") options:(refNSRegularExpressionSearch) range:ocidNsRange
log ocidNSMutableString as text
log className() of ocidNSMutableString as text
#########################
###↑文字数を数える
set numCntNSMutableString to ocidNSMutableString's |length|()
###↑文字数全部でレンジにする
set ocidNsRange to {location:0, |length|:numCntNSMutableString}
log class of ocidNsRange
log ocidNsRange as record
###改行除去
ocidNSMutableString's replaceOccurrencesOfString:("\r") withString:("") options:(refNSRegularExpressionSearch) range:ocidNsRange
log ocidNSMutableString as text
log className() of ocidNSMutableString as text

#########################
###↑文字数を数える
set numCntNSMutableString to ocidNSMutableString's |length|()
###↑文字数全部でレンジにする
set ocidNsRange to {location:0, |length|:numCntNSMutableString}
log class of ocidNsRange
log ocidNsRange as record
###バックスラッシュ除去
ocidNSMutableString's replaceOccurrencesOfString:("curl ") withString:("") options:0 range:ocidNsRange
log ocidNSMutableString as text
log className() of ocidNSMutableString as text
#########################
###区切り文字を定義
set ocidNSCFCharacterSet to refNSCharacterSet's characterSetWithCharactersInString:" "
log className() of ocidNSCFCharacterSet as text
###置き換え文字でArray=リスト化
set ocidNsArrayList to ocidNSMutableString's componentsSeparatedByCharactersInSet:ocidNSCFCharacterSet
log ocidNsArrayList as list
log className() of ocidNsArrayList as text
####URL部分を取り出す
###初期化 空の可変テキストを作成
set ocidUrlText to refNSMutableString's alloc()'s initWithCapacity:0
set ocidUrlText to refNSMutableString's stringWithString:(ocidNsArrayList's objectAtIndex:0)
log ocidUrlText as text


#########################
###区切り文字を定義
set ocidNSCFCharacterSet to refNSCharacterSet's characterSetWithCharactersInString:"\\"
log className() of ocidNSCFCharacterSet as text
###置き換え文字でArray=リスト化
set ocidNsArrayHeader to ocidNSMutableString's componentsSeparatedByCharactersInSet:ocidNSCFCharacterSet
log ocidNsArrayHeader as list
log className() of ocidNsArrayHeader as text


log className() of ocidNSMutableString as text
set ocidHeaderText to (ocidNsArrayHeader's componentsJoinedByString:"")
log className() of ocidUrlText as text
log className() of ocidHeaderText as text

########################
###
set strUrlText to ocidUrlText as text
###ヘッダーテキスト
set strHeaderText to ocidHeaderText as text
####日時テキスト
set strDateAndTime to ocidDateAndTime as text
###M3uFileName
set strM3uFileName to ("" & strDateAndTime & ".m3u8")
####
set strM3uSavePath to ("" & strM3uDir & "/" & strM3uFileName & "") as text

log strM3uFileName
log strM3uSavePath

set strCommandText to "/usr/bin/curl " & strUrlText & " -o '" & strM3uSavePath & "' " & strHeaderText & "" as text
log strCommandText

do shell script strCommandText
################################################
################################################

####ファイルを読み取る
set ocidReadData to (refNSString's stringWithContentsOfFile:strM3uSavePath encoding:(refNSUTF8StringEncoding) |error|:(missing value))
log ocidReadData as text
log className() of ocidReadData as text

######Arrayの初期化
set ocidM3uArray to (refNSMutableArray's alloc()'s initWithCapacity:0)
log ocidM3uArray as list
log className() of ocidM3uArray as text
###区切り文字を定義
set ocidNSCFCharacterSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:"\n")
log className() of ocidNSCFCharacterSet as text
###ocidNSCFCharacterSetArray=リスト化
set ocidM3uArray to (ocidReadData's componentsSeparatedByCharactersInSet:ocidNSCFCharacterSet)
log ocidM3uArray as list
log className() of ocidM3uArray as text


#####URL判定用の値の初期化
set strM3U8urlmobile to "" as text
set strM3U8urllowest to "" as text
set strM3U8urllow to "" as text
set strM3U8urlsd to "" as text
set strM3U8urlhd to "" as text
set strM3U8urlfull to "" as text
set numCntLineNo to 0 as number
####読み込んだファイルデータの行分だけ繰り返し
repeat with objM3uArray in ocidM3uArray

set strM3uArray to objM3uArray as text
set numCntURL to numCntLineNo + 1 as number
###それぞれのURLを格納する
if strM3uArray contains "QUALITY=mobile" then
set strM3U8urlmobile to (ocidM3uArray's objectAtIndex:numCntURL) as text
end if
if strM3uArray contains "QUALITY=lowest" then
set strM3U8urllowest to (ocidM3uArray's objectAtIndex:numCntURL) as text
end if
if strM3uArray contains "QUALITY=low" then
set strM3U8urllow to (ocidM3uArray's objectAtIndex:numCntURL) as text
end if
if strM3uArray contains "QUALITY=sd" then
set strM3U8urlsd to (ocidM3uArray's objectAtIndex:numCntURL) as text
end if
if strM3uArray contains "QUALITY=hd" then
set strM3U8urlhd to (ocidM3uArray's objectAtIndex:numCntURL) as text
end if
if strM3uArray contains "QUALITY=full" then
set strM3U8urlfull to (ocidM3uArray's objectAtIndex:numCntURL) as text
end if

set numCntLineNo to numCntLineNo + 1 as number
end repeat

######高解像度のURLを優先する
if strM3U8urlmobile is not "" then
set strM3U8url to strM3U8urlmobile as text
end if
if strM3U8urllowest is not "" then
set strM3U8url to strM3U8urllowest as text
end if
if strM3U8urllow is not "" then
set strM3U8url to strM3U8urllow as text
end if
if strM3U8urlsd is not "" then
set strM3U8url to strM3U8urlsd as text
end if
if strM3U8urlhd is not "" then
set strM3U8url to strM3U8urlhd as text
end if
if strM3U8urlfull is not "" then
set strM3U8url to strM3U8urlfull as text
end if
###取得したURL
log strM3U8url
###ダウンロードパス
set strM3u8Dir to ("/private/tmp/" & (ocidDateAndTime as text) & "m3u8") as text
####ファイル名
set strM3u8SavePath to ("" & strM3u8Dir & "/" & strM3uFileName & "") as text
####ダウンロード
set strCommandText to "/usr/bin/curl '" & strM3U8url & "' -o '" & strM3u8SavePath & "' " & strHeaderText & "" as text
log strCommandText
do shell script strCommandText

##################################################
##################################################
####ダウンロードしたURLはデイレクトリなのでそのまま利用
set strBaseUrl to strM3U8url as text

####ファイルを読み取る
set ocidM3u8ReadData to (refNSString's stringWithContentsOfFile:strM3u8SavePath encoding:(refNSUTF8StringEncoding) |error|:(missing value))
log ocidM3u8ReadData as text
log className() of ocidM3u8ReadData as text

######Arrayの初期化
set ocidM3u8Array to (refNSMutableArray's alloc()'s initWithCapacity:0)
log ocidM3u8Array as list
log className() of ocidM3u8Array as text
###区切り文字を定義
set ocidNSCFCharacterSet to (refMe's NSCharacterSet's characterSetWithCharactersInString:"\n")
log className() of ocidNSCFCharacterSet as text
###ocidNSCFCharacterSetArray=リスト化
set ocidM3u8Array to (ocidM3u8ReadData's componentsSeparatedByCharactersInSet:ocidNSCFCharacterSet)
log ocidM3u8Array as list
log className() of ocidM3u8Array as text
###Arrayの数を数える
set numReadFileText to count of ocidM3u8Array
###ファイル名
set numFileName to 10000 as number
###ファイル番号のカウントアップ
set numFileCnt to 0 as number

####読み込んだデータの行数だけ々
repeat with objM3u8Array in ocidM3u8Array
###テキストに
set strM3u8Array to objM3u8Array as text
#####
#####set cntHas to the offset of "#" in strM3u8Array
##### #で始まるのはコメント行
if strM3u8Array starts with "#" then
log "コメント行"
else
###URLが格納されているので整形
set numFileName to numFileName + 1 as number
set strTsURL to strBaseUrl & strM3u8Array as text
set strTsFileName to (numFileName & ".ts")
set strTsDownPath to (strTempPath & "/" & strTsFileName) as text
####ダウンロード
set strCommandText to "/usr/bin/curl '" & strTsURL & "' -o '" & strTsDownPath & "' " & strHeaderText & "" as text
log strCommandText
do shell script strCommandText
end if

end repeat

####終わったら ダウンロードしたデータをデスクトップに結合して終了
do shell script "cat " & strTempPath & "/* >> ~/Desktop/" & (ocidDateAndTime as text) & ".ts"

#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
tell application "Script Editor"
tell application "System Events"
tell process "Script Editor"
tell window 1
tell splitter group 1
tell splitter group 1
tell group 1
tell checkbox "返された値"
set boolValue to value as boolean
end tell
if boolValue is false then
click checkbox "返された値"
end if
end tell
end tell
end tell
end tell
end tell
end tell
end tell
end if
end repeat

end doLogView
#########################

|

[m3u]Twitterビデオダウンロード(ヘルパー)m4s用



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
(*

m3u8?variant_version=1
m3u8?container=fmp4

*)
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
######ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL

set objFileManager to objMe's NSFileManager's defaultManager()

####BaseURL
set strBaseURL to "https://video.twimg.com"

####クリップボードの内容をデフォルトに利用
tell application "Finder"
set strClipboardText to the clipboard as text
end tell
####コピーしているの前提で
set strDefaultAnswer to (strBaseURL & strClipboardText)
###ICONのパス
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ConnectToIcon.icns" as alias
###ダイアログ
set objResponse to (display dialog "URLを入力してください" with title "m3uファイル" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 10 without hidden answer)
if true is equal to (gave up of objResponse) then
return "時間切れですやりなおしてください"
error number -128
end if
if "OK" is equal to (button returned of objResponse) then
set strResponse to (text returned of objResponse) as text
else
log "エラーしました"
return "エラーしました"
error number -128
end if


###日付取得
###フォーマットは<http://nsdateformatter.com>参照
set strDateFormat to "yyyyMMddhhmmss" as text
set ocidForMatter to objMe's NSDateFormatter's alloc()'s init()
ocidForMatter's setLocale:(objMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidForMatter's setDateFormat:(strDateFormat)
set strDateText to (ocidForMatter's stringFromDate:(current date)) as text
####作成するダウンロードフォルダのパス
set strDirPath to ("/tmp/" & strDateText & "")
set objDirPath to objNSString's stringWithString:strDirPath
###フォルダを作る
set boolMakeNewFolder to (objFileManager's createDirectoryAtPath:objDirPath withIntermediateDirectories:true attributes:(missing value) |error|:(missing value))
###エラー判定
if boolMakeNewFolder is false then
return "ダウンロードフォルダの作成に失敗しました"
end if
(*
テンポラリーファイルを/tmpに作る事で
次回の再起動時に自動的に削除されるメリットがあります
*)
####
##まずは m3u8ファイルをダウンロードします
###コマンド整形
set strCommandText to "/usr/bin/curl -k '" & strResponse & "' -o '" & strDirPath & "/" & strDateText & ".m3u8'" as text
###コマンド実行
do shell script strCommandText

set strFilePath to "" & strDirPath & "/" & strDateText & ".m3u8"

####ドキュメントのパスをNSString
set ocidFilePath to objNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidNSUrlPath to objNSURL's fileURLWithPath:ocidFilePath
####ファイル名
set ocidFileName to ocidNSUrlPath's lastPathComponent
####拡張子を除いたファイル名
set strPrefixName to (ocidFileName's stringByDeletingPathExtension) as text

##読み込み (テキスト)
set ocidReadM3Udata to objNSString's stringWithContentsOfFile:ocidFilePath

###改行でリスト化
set strReturn to "\n"
set ocidReadM3UdataArray to ocidReadM3Udata's componentsSeparatedByString:(strReturn)
###ダウンロードするテンポラリーファイルのナンバリング
set strFileNo to 1000 as number
###改行だけ繰返し
repeat with objReadM3UdataArray in ocidReadM3UdataArray
###テキストにして
set strReadM3UdataArray to objReadM3UdataArray as text
####EXT-X-MAP 部を最初に処理 初期化セクション
if strReadM3UdataArray starts with "#EXT-X-MAP" then
###ファイル番号カウントアップ
set strFileNo to strFileNo + 1 as number
set strMapPath to doReplace(strReadM3UdataArray, "#EXT-X-MAP:URI=", "")
set strMapPath to doReplace(strMapPath, "\"", "")
###URL整形
set strURL to (strBaseURL & strMapPath) as text
set strCommandText to "/usr/bin/curl -k '" & strURL & "' -o '" & strDirPath & "/" & strFileNo & ".m4s'" as text
do shell script strCommandText
else if strReadM3UdataArray starts with "#" then
log "コメント行"
else if strReadM3UdataArray is "" then
log "空行"
else
#####コンテンツ部のダウンロード
###ファイル番号カウントアップ
set strFileNo to strFileNo + 1 as number
####ドキュメントのパスをNSURL
set ocidNSUrlReadM3UdataArray to (objNSURL's fileURLWithPath:objReadM3UdataArray)
####ファイル名
set ocidM3UFileName to ocidNSUrlReadM3UdataArray's lastPathComponent
####拡張子
set strM3UPrefixName to (ocidM3UFileName's pathExtension) as text
log strM3UPrefixName
### URL整形
set strURL to (strBaseURL & strReadM3UdataArray) as text
if strM3UPrefixName is "m4s" then
###コマンド整形
set strCommandText to "/usr/bin/curl -k '" & strURL & "' -o '" & strDirPath & "/" & strFileNo & ".m4s'" as text
else if strM3UPrefixName is "ts" then
###コマンド整形
set strCommandText to "/usr/bin/curl -k '" & strURL & "' -o '" & strDirPath & "/" & strFileNo & ".ts'" as text
end if
###コマンド実行
do shell script strCommandText
end if

end repeat
#####
(*
要は元のMP4だったりTSファイルをsplitしてある『だけ』なので
CATで結合できます。

*)
if strM3UPrefixName is "m4s" then
###ダウンロード終わったものをまとめる
do shell script "/bin/cat " & strDirPath & "/*m4s >> ~/Desktop/" & strPrefixName & ".mp4"
else if strM3UPrefixName is "ts" then
###ダウンロード終わったものをまとめる
do shell script "/bin/cat " & strDirPath & "/*ts >> ~/Desktop/" & strPrefixName & ".ts"
end if





to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace


#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################



#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
(*

m3u8?variant_version=1
m3u8?container=fmp4

*)
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7

use AppleScript version "2.8"
use framework "Foundation"
use scripting additions
######ログ表示
doLogView()

property objMe : a reference to current application
property objNSString : a reference to objMe's NSString
property objNSURL : a reference to objMe's NSURL

set objFileManager to objMe's NSFileManager's defaultManager()

####BaseURL
set strBaseURL to "https://video.twimg.com"

####クリップボードの内容をデフォルトに利用
tell application "Finder"
set strClipboardText to the clipboard as text
end tell
####コピーしているの前提で
set strDefaultAnswer to (strBaseURL & strClipboardText)
###ICONのパス
set aliasIconPath to POSIX file "/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/ConnectToIcon.icns" as alias
###ダイアログ
set objResponse to (display dialog "URLを入力してください" with title "m3uファイル" default answer strDefaultAnswer buttons {"OK", "キャンセル"} default button "OK" cancel button "キャンセル" with icon aliasIconPath giving up after 10 without hidden answer)
if true is equal to (gave up of objResponse) then
return "時間切れですやりなおしてください"
error number -128
end if
if "OK" is equal to (button returned of objResponse) then
set strResponse to (text returned of objResponse) as text
else
log "エラーしました"
return "エラーしました"
error number -128
end if


###日付取得
###フォーマットは<http://nsdateformatter.com>参照
set strDateFormat to "yyyyMMddhhmmss" as text
set ocidForMatter to objMe's NSDateFormatter's alloc()'s init()
ocidForMatter's setLocale:(objMe's NSLocale's localeWithLocaleIdentifier:"ja_JP_POSIX")
ocidForMatter's setDateFormat:(strDateFormat)
set strDateText to (ocidForMatter's stringFromDate:(current date)) as text
####作成するダウンロードフォルダのパス
set strDirPath to ("/tmp/" & strDateText & "")
set objDirPath to objNSString's stringWithString:strDirPath
###フォルダを作る
set boolMakeNewFolder to (objFileManager's createDirectoryAtPath:objDirPath withIntermediateDirectories:true attributes:(missing value) |error|:(missing value))
###エラー判定
if boolMakeNewFolder is false then
return "ダウンロードフォルダの作成に失敗しました"
end if
(*
テンポラリーファイルを/tmpに作る事で
次回の再起動時に自動的に削除されるメリットがあります
*)
####
##まずは m3u8ファイルをダウンロードします
###コマンド整形
set strCommandText to "/usr/bin/curl -k '" & strResponse & "' -o '" & strDirPath & "/" & strDateText & ".m3u'" as text
###コマンド実行
do shell script strCommandText

set strFilePath to "" & strDirPath & "/" & strDateText & ".m3u8"

####ドキュメントのパスをNSString
set ocidFilePath to objNSString's stringWithString:strFilePath
####ドキュメントのパスをNSURL
set ocidNSUrlPath to objNSURL's fileURLWithPath:ocidFilePath
####ファイル名
set ocidFileName to ocidNSUrlPath's lastPathComponent
####拡張子を除いたファイル名
set strPrefixName to (ocidFileName's stringByDeletingPathExtension) as text

##読み込み (テキスト)
set ocidReadM3Udata to objNSString's stringWithContentsOfFile:ocidFilePath
###改行でリスト化
set ocidReadM3UdataArray to ocidReadM3Udata's componentsSeparatedByString:"\n"
###ダウンロードするテンポラリーファイルのナンバリング
set strFileNo to 1000 as number
###改行だけ繰返し
repeat with objReadM3UdataArray in ocidReadM3UdataArray
###テキストにして
set strReadM3UdataArray to objReadM3UdataArray as text
####EXT-X-MAP 部を最初に処理 初期化セクション
if strReadM3UdataArray starts with "#EXT-X-MAP" then
###ファイル番号カウントアップ
set strFileNo to strFileNo + 1 as number
set strMapPath to doReplace(strReadM3UdataArray, "#EXT-X-MAP:URI=", "")
set strMapPath to doReplace(strMapPath, "\"", "")
###URL整形
set strURL to (strBaseURL & strMapPath) as text
set strCommandText to "/usr/bin/curl -k '" & strURL & "' -o '" & strDirPath & "/" & strFileNo & ".m4s'" as text
do shell script strCommandText
else if strReadM3UdataArray starts with "#" then
log "コメント行"
else if strReadM3UdataArray is "" then
log "空行"
else
#####コンテンツ部のダウンロード
###ファイル番号カウントアップ
set strFileNo to strFileNo + 1 as number
####ドキュメントのパスをNSURL
set ocidNSUrlReadM3UdataArray to (objNSURL's fileURLWithPath:objReadM3UdataArray)
####ファイル名
set ocidM3UFileName to ocidNSUrlReadM3UdataArray's lastPathComponent
####拡張子
set strM3UPrefixName to (ocidM3UFileName's pathExtension) as text
log strM3UPrefixName
### URL整形
set strURL to (strBaseURL & strReadM3UdataArray) as text
if strM3UPrefixName is "m4s" then
###コマンド整形
set strCommandText to "/usr/bin/curl -k '" & strURL & "' -o '" & strDirPath & "/" & strFileNo & ".m4s'" as text
else if strM3UPrefixName is "ts" then
###コマンド整形
set strCommandText to "/usr/bin/curl -k '" & strURL & "' -o '" & strDirPath & "/" & strFileNo & ".ts'" as text
end if
###コマンド実行
do shell script strCommandText
end if

end repeat
#####
(*
要は元のMP4だったりTSファイルをsplitしてある『だけ』なので
CATで結合できます。

*)
if strM3UPrefixName is "m4s" then
###ダウンロード終わったものをまとめる
do shell script "/bin/cat " & strDirPath & "/*m4s >> ~/Desktop/" & strPrefixName & ".mp4"
else if strM3UPrefixName is "ts" then
###ダウンロード終わったものをまとめる
do shell script "/bin/cat " & strDirPath & "/*ts >> ~/Desktop/" & strPrefixName & ".ts"
end if





to doReplace(theText, orgStr, newStr)
set oldDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to orgStr
set tmpList to every text item of theText
set AppleScript's text item delimiters to newStr
set tmpStr to tmpList as text
set AppleScript's text item delimiters to oldDelim
return tmpStr
end doReplace


#########################ログ表示
to doLogView()

tell application "System Events"
set listAppList to title of (every process where background only is false)
end tell
repeat with objAppList in listAppList
set strAppList to objAppList as text
if strAppList is "スクリプトエディタ" then
tell application "Script Editor"
if frontmost is true then
try
tell application "System Events" to click menu item "ログを表示" of menu "表示" of menu bar item "表示" of menu bar 1 of application process "Script Editor"
end try
end if
end tell
end if
end repeat

end doLogView
#########################

|

その他のカテゴリー

Acrobat Acrobat Annotation Acrobat Dialog Acrobat Form Acrobat JS Acrobat Open Acrobat Plugin Acrobat python Acrobat Reader Acrobat Sequ Acrobat Stamps Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin System Information Admin Tools Admin Users Admin Volumes Adobe Apple AppleScript AppleScript Guide AppleScript Accessibility AppleScript AppKit AppleScript Applications AppleScript AppStore AppleScript Archive AppleScript Attributes AppleScript Automator AppleScript AVAsset AppleScript AVconvert AppleScript AVFoundation AppleScript AVURLAsset AppleScript BackUp AppleScript Barcode AppleScript Bash AppleScript Basic AppleScript Basic Path AppleScript Bluetooth AppleScript BOX AppleScript Browser AppleScript Calendar AppleScript CD/DVD AppleScript Choose AppleScript Chrome AppleScript CIImage AppleScript CloudStorage AppleScript Color AppleScript com.apple.LaunchServices.OpenWith AppleScript Console AppleScript Contacts AppleScript CotEditor AppleScript CURL AppleScript current application AppleScript Date&Time AppleScript delimiters AppleScript Desktop AppleScript Device AppleScript Disk AppleScript do shell script AppleScript Dock AppleScript DropBox AppleScript eMail AppleScript Encode Decode AppleScript Error AppleScript EXIFData AppleScript ffmpeg AppleScript File AppleScript Finder AppleScript Firefox AppleScript Folder AppleScript Fonts AppleScript GIF AppleScript HTML AppleScript Icon AppleScript Illustrator AppleScript Image Events AppleScript Image2PDF AppleScript ImageOptim Applescript iWork AppleScript Javascript AppleScript Jedit AppleScript Json AppleScript Label AppleScript List AppleScript locationd AppleScript LRC AppleScript LSSharedFileList AppleScript m3u8 AppleScript MakePDF AppleScript Map AppleScript Math AppleScript Microsoft AppleScript Microsoft Edge AppleScript Microsoft Excel AppleScript Mobileconfig AppleScript Mouse AppleScript Movie AppleScript Music AppleScript NetWork AppleScript Notes AppleScript NSArray AppleScript NSBitmapImageRep AppleScript NSBundle AppleScript NSCFBoolean AppleScript NSCharacterSet AppleScript NSColor AppleScript NSColorList AppleScript NSData AppleScript NSDictionary AppleScript NSError AppleScript NSEvent AppleScript NSFileAttributes AppleScript NSFileManager AppleScript NSFont AppleScript NSFontManager AppleScript NSImage AppleScript NSIndex AppleScript NSKeyedArchiver AppleScript NSKeyedUnarchiver AppleScript NSLocale AppleScript NSMutableArray AppleScript NSMutableDictionary AppleScript NSMutableString AppleScript NSNotFound AppleScript NSNumber AppleScript NSOpenPanel AppleScript NSPasteboard AppleScript NSPDFImageRep AppleScript NSpoint AppleScript NSPredicate AppleScript NSRange AppleScript NSRegularExpression AppleScript NSRunningApplication AppleScript NSScreen AppleScript NSSize AppleScript NSString AppleScript NSStringCompareOptions AppleScript NSTask AppleScript NSTimeZone AppleScript NSURL AppleScript NSURL File AppleScript NSURLBookmark AppleScript NSURLComponents AppleScript NSURLResourceKey AppleScript NSUserDefaults AppleScript NSUUID AppleScript NSView AppleScript NSWorkspace AppleScript Numbers AppleScript OAuth AppleScript ObjC AppleScript OneDrive AppleScript Osax AppleScript PDF AppleScript PDFAnnotationWidget AppleScript PDFContext AppleScript PDFDisplayBox AppleScript PDFDocumentPermissions AppleScript PDFKit AppleScript PDFnUP AppleScript PDFOutline AppleScript Photos AppleScript Photoshop AppleScript Plist System Events AppleScript PostScript AppleScript prefPane AppleScript Preview AppleScript Python AppleScript QR AppleScript QR Decode AppleScript QuickLook AppleScript QuickTime AppleScript record AppleScript Regular Expression AppleScript Reminders AppleScript ReName AppleScript Repeat AppleScript RTF AppleScript Safari AppleScript SaveFile AppleScript ScreenCapture AppleScript ScreenSaver AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript Sort AppleScript Sound AppleScript Spotlight AppleScript SRT AppleScript StandardAdditions AppleScript stringByApplyingTransform AppleScript System Events AppleScript System Settings AppleScript TemporaryItems AppleScript Terminal AppleScript Text AppleScript Text CSV AppleScript Text TSV AppleScript TextEdit AppleScript Translate AppleScript Trash AppleScript Twitter AppleScript UI AppleScript Unit Conversion AppleScript UTType AppleScript valueForKeyPath AppleScript Video AppleScript VisionKit AppleScript Visual Studio Code AppleScript webarchive AppleScript webp AppleScript Wifi AppleScript XML AppleScript XML EPUB AppleScript XML OPML AppleScript XML Plist AppleScript XML RSS AppleScript XML SVG AppleScript XML TTML AppleScript XML webloc AppleScript YouTube AppleScript Zero Suppress Applications Barcode OnLine Tools CityCode Design iPhone List Logs lsappinfo Memo Music perl PlistBuddy pluginkit postalcode ReadMe SF Symbols character id SF Symbols Entity sips Skype Slack sqlite TCC Tools Typography Video Wacom zoom