Skip to content

Instantly share code, notes, and snippets.

@Hayao0819
Last active August 3, 2022 07:34
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 Hayao0819/897be45dfedb0aa4fcc2735a0831966f to your computer and use it in GitHub Desktop.
Save Hayao0819/897be45dfedb0aa4fcc2735a0831966f to your computer and use it in GitHub Desktop.
Arch Linuxの最新のニュースをGUIで表示します
#!/usr/bin/env bash
# shellcheck disable=SC2034
set -Eeu
#ArchNewsRSSURL="https://www.archlinux.jp/feeds/news.xml"
ArchNewsRSSURL="https://archlinux.org/feeds/news/"
CachePath="${XDG_STATE_HOME-"$HOME/.local/state"}/archnews.latest"
AutoStartPath="${XDG_CONFIG_HOME-"${HOME}/.config"}/autostart/archnews.desktop"
RSSXML=()
IgnoreCache=false
ShowMode="ZENITY"
FSBLIB_PATHLIST=(
"/usr/lib/archnews.sh"
#"$HOME/Git/FasBashLib/fasbashlib.sh"
)
LoadLib(){
for File in "${FSBLIB_PATHLIST[@]}"; do
# shellcheck source=/dev/null
[[ -e "$File" ]] && source "$File"
done
if [[ -z "${FSBLIB_VERSION-""}" ]]; then
Msg.Err "Failed to load FasBashLib"
exit 1
fi
if ! Array.Includes FSBLIB_LIBLIST "Parsrs"; then
Msg.Err "FasBashLibをParsrsを含めてビルドしてください"
exit 1
fi
}
GetLatestNewsURL(){ PrintArray "${RSSXML[@]}" | grep "^/rss/channel/item/link.*" | URL.Path | head -n 1; }
GetNews(){
local Line CurrentTitle CurrentLink InTarget=false
while read -r Line; do
case "$Line" in
"/rss/channel/item/title "*)
CurrentTitle="$(cut -d " " -f 2- <<< "$Line")"
continue
;;
"/rss/channel/item/link "*)
CurrentLink="$(cut -d " " -f 2- <<< "$Line" | URL.Path)"
if [[ "$CurrentLink" = "$1" ]]; then
InTarget=true
echo "$CurrentTitle"
else
InTarget=false
fi
continue
;;
"/rss/channel/item")
InTarget=false
continue
;;
*)
if [[ "$InTarget" = true ]]; then
cut -d " " -f 2- <<< "$Line"
fi
;;
esac
done < <(PrintArray "${RSSXML[@]}" | grep "^/rss/channel/item.*")
}
FormatXML(){
sed -e 's|\\n| |g' -e 's|\&lt;|<|g' -e 's|\&gt;|>|g' -e 's|\&amp;|&|g' -e 's|\&quot;|"|g' -e 's|\&apos;||g' | sed -e "s|<[^>]*>||g" | sed -e "s|lt;p>||g"
}
Main(){
# XMLを解析して配列に格納
ArrayAppend RSSXML < <(curl -sL "$ArchNewsRSSURL" | Parsrs.Xml)
{ (( "${#RSSXML[@]}" < 1 )) || [[ -z "${RSSXML[*]}" ]]; } && {
Msg.Err "RSSの取得に失敗しました"
exit 1
}
local LatestNewsURL
LatestNewsURL="$(GetLatestNewsURL)"
if [[ -z "$LatestNewsURL" ]]; then
Msg.Err "最新のニュースの取得ができませんでした"
exit 1
fi
if [[ -e $CachePath ]] && ! Bool "IgnoreCache" && [[ "$(cat "$CachePath")" = "$LatestNewsURL" ]]; then
Msg.Info "最新のニュースは既に確認済みです"
exit 0
fi
# キャッシュを作成
echo "$LatestNewsURL" > "$CachePath"
# 最新ニュースを取得
local LatestNews=()
ArrayAppend LatestNews < <(GetNews "$(GetLatestNewsURL)")
# ニュースを出力
case "${ShowMode^^}" in
"CLI")
echo "<${LatestNews[0]}>"
echo "${LatestNews[1]}" | FormatXML
echo "(Last update: ${LatestNews[4]} By ${LatestNews[3]}"
;;
"ZENITY")
zenity --info \
--title="ArchNews - ${LatestNews[0]}" \
--text="$(FormatXML <<< "${LatestNews[1]}")"
;;
esac
return 0
}
LoadLib
ParseArg LONG="help,force,cli,gui,english,japanese,clean,startup" SHORT="hfcgej" -- "${@}"
eval set -- "${OPTRET[*]}"
while true; do
case "${1-""}" in
"-c" | "--cli")
ShowMode="CLI"
shift
;;
"-g" | "--gui")
ShowMode="ZENITY"
shift 1
;;
"-e" | "--english")
ArchNewsRSSURL="https://archlinux.org/feeds/news/"
shift 1
;;
"-j" | "--japanese")
ArchNewsRSSURL="https://www.archlinux.jp/feeds/news.xml"
shift 1
;;
"-h" | "--help")
echo "Usage: $0 "
echo " -g, --gui : GUIモード"
echo " -c, --cli : CLIモード"
echo " -f , --force : 強制的に表示"
echo " -h, --help : ヘルプを表示"
echo " --clean : 一時データを削除して終了"
echo " --startup : スタートアップのオンオフ "
exit 0
;;
"-f" | "--force")
IgnoreCache=true
shift 1
;;
"--clean")
[[ -e "$CachePath" ]] && rm -f "$CachePath"
exit 0
;;
"--startup")
if [[ -e "$AutoStartPath" ]]; then
rm -f "$AutoStartPath"
Msg.Info "Disable autostart"
exit 0
elif [[ -e "/usr/share/applications/show-archnews.desktop" ]]; then
cp "/usr/share/applications/show-archnews.desktop" "$AutoStartPath"
Msg.Info "Enable autostart"
exit 0
else
Msg.Err "You must install ArchNews with pacman to enable autostart."
exit 1
fi
shift 1
;;
"--")
shift 1 || true
break
;;
*)
break
;;
esac
done
Main
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
# Maintainer: Yamada Hayao <hayao@fascode.net>
pkgname=archnews
pkgver='20220720'
pkgrel='1'
pkgdesc='Show the latest news from Arch Linux official page'
arch=('any')
url='https://gist.github.com/Hayao0819/897be45dfedb0aa4fcc2735a0831966f'
license=('WTFPL')
depends=('zenity')
source=(
"archnews.sh::https://gist.githubusercontent.com/Hayao0819/897be45dfedb0aa4fcc2735a0831966f/raw/ArchNews.sh"
"show-archnews.desktop::https://gist.githubusercontent.com/Hayao0819/897be45dfedb0aa4fcc2735a0831966f/raw/show-archnews.desktop"
"FasBashLib::git+https://github.com/Hayao0819/FasBashLib.git#commit=3198a759281504ccb2af4df76fca192be9a81e17"
)
sha512sums=('SKIP' 'SKIP' 'SKIP')
build(){
cd "$srcdir/FasBashLib"
git submodule update --init --recursive
./tools.sh build single -out "$srcdir/fasbashlib.sh" URL BetterShell Parsrs Array ParseArg
}
package() {
# Install files
install -Dm 755 "$srcdir/archnews.sh" "$pkgdir/usr/bin/archnews"
install -Dm 644 "$srcdir/show-archnews.desktop" "$pkgdir/usr/share/applications/show-archnews.desktop"
install -Dm 755 "${srcdir}/fasbashlib.sh" "$pkgdir/usr/lib/archnews.sh"
}
[Desktop Entry]
Type=Application
Version=1.0
Name=ArchNews
Comment=Show the latest news from Arch Linux official page
Path=/usr/bin
Exec=/usr/bin/archnews
Icon=archlinux
Terminal=false
Categories=Internet;Utility;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment