Skip to content

Instantly share code, notes, and snippets.

@Aquei
Last active March 4, 2018 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aquei/ebd27664001e63267db9bbd5f5a63460 to your computer and use it in GitHub Desktop.
Save Aquei/ebd27664001e63267db9bbd5f5a63460 to your computer and use it in GitHub Desktop.
redditに投稿されたメディアをmpvで再生する
#!/bin/sh
URLS=""
UA="plz_give_me_json"
REDDIT_DEFAULT_LIMIT=100
#json urlを追加する場合はadd_jsonを使うこと
add_json()
{
for url in "$@"; do
if ! echo "$URLS" | grep -F "$url"; then
URLS="${URLS} ${url}"
fi
done
}
#対象とするサブミッションの最小タイム
if [ $# -ge 1 ]; then
for date_string in "${1}" "${1} ago" "-${1}" "0"; do
if echo "$date_string" | grep -E -e '^[0-9]+$'; then
#タイムタンプが直接指定
min_timestamp="$1"
else
#日付文字列
min_timestamp=$(date "+%s" -d "$date_string")
if [ $? -eq 1 ];then
#日付パースに失敗
continue
fi
fi
if [ "$(date '+%s')" -lt "$min_timestamp" ]; then
#未来の日付が渡された!!!
echo "${date_string}は未来の日付です" >&2
else
break
fi
done
else
min_timestamp=0
fi
#二番目以降の引数はjsonのurlかsub redditのスラッグ
if [ $# -ge 2 ]; then
arg_index=0
for arg in "$@"; do
arg_index=$(expr "${arg_index}" \+ 1)
if [ "${arg_index}" -le 1 ]; then
continue
fi
if echo "$arg" | grep -E -i -e '^https?://www\.reddit\.com/r/[a-z0-9\-_]+/?\.json'; then
add_json "$arg"
else
add_json "https://www.reddit.com/r/${arg}.json?limit=${REDDIT_DEFAULT_LIMIT}"
fi
done
fi
#urlにあるオブジェクトを値に持つ配列(json文字列)を返す
get_jsons()
{
result=""
for url in "$@"; do
json="$(wget -O - -U "${UA}" "${url}")"
if [ $? != 0 ]; then
#何かwgetでエラー
continue
fi
result="${result}${json},"
done
result="[$(printf "%s" "${result}" | sed -r -e 's/,$//')]"
printf "%s" "${result}"
}
get_jsons ${URLS} | jq -r "[.[].data.children[]] | map(select(.data.created_utc >= ${min_timestamp})) | unique_by(.data.url) | sort_by(.data.created_utc) | reverse | .[].data.url" | sed -r -e '/^https?:\/\/www\.reddit\.com\//d'| xargs -d "\n" mpv
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment