当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 3 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : [问题]寻找在Linux下自动更换国家地理壁纸的脚本,并[转贴]獻上一個下載NGPOD的腳本
帖子发表于 : 2008-09-29 17:55 
头像

注册: 2008-09-18 18:21
帖子: 118
送出感谢: 0 次
接收感谢: 0 次
以前见过一个在Linux下自动更换壁纸的脚本,来源是国家地理,现在却找不到了,请大家看看下面这个能用吗?
:em06

来自[蓝森林]

獻上一個下載national geographic Photo Of the Day的腳本,並請指正


代碼:
-->#!/bin/bash

#NGPOD:national geographic Photo Of the Day

#the earliest ngpod is the March 13, 2001
declare BYEAR=01 BMONTH=03 BDAY=13

#current year,month,day
declare CYEAR CMONTH CDAY

#download year,month,day
declare DYEAR DMONTH DDAY

TRUE=1
FALSE=0

LOGFILE=.ngpod.log

declare -i GOON=0
declare NEXTLINK
###############################################
#function difinition area

#get the begin day
getbeginday()
{
local JUDGE IFS lastYEAR lastMONTH lastDAY lastSTATUS

if [ -f $LOGFILE ] ;then
tail -1 $LOGFILE >.getbeginday.tmp #get the last log
else
echo "Seem that you the first time to download ngpod "
echo "Will start with the day 2001:03:13"
echo "It may take a long time to download"
echo "You may want to change begin day "
read -p "begin with 2001:03:13?(y/n):" JUDGE

if [ $JUDGE = "y" ]; then
DYEAR=$BYEAR
DMONTH=$BMONTH
DDAY=$BDAY
return $TRUE # use begin day 20010313
else
echo "you can change begin day "
echo "edit ngpod.sh:line 6"
echo "ngpod will exit now"
exit
fi
fi

IFS=" "
read lastYEAR lastMONTH lastDAY lastSTATUS <.getbeginday.tmp
rm -f .getbeginday.tmp
DYEAR=$lastYEAR
DMONTH=$lastMONTH
DDAY=$lastDAY

if [ $lastSTATUS = "OK" ];then
GOON=1;
fi
#echo "day is $DDAY"


return $TRUE
}





#get current year,month,day
gettoday()
{
local currentWEEK currentMONTH currentDAY currentTIME currentUTC currentYEAR
local IFS

IFS=" " #set spacekey as the split char
#echo "get today"

date -u >.gettoday.tmp
read currentWEEK currentMONTH currentDAY currentTIME currentUTC currentYEAR <.gettoday.tmp
IFS=$localIFS
rm -f .gettoday.tmp


CYEAR=`expr ${currentYEAR} - 2000 `
CYEAR="0${CYEAR}"
CDAY=$currentDAY
if [ `expr $CDAY` -le 9 ] ;then
CDAY=0$CDAY
fi

case $currentMONTH in
Jan) CMONTH=01 ;;
Feb) CMONTH=02 ;;
Mar) CMONTH=03 ;;
Apr) CMONTH=04 ;;
May) CMONTH=05 ;;
Jun) CMONTH=06 ;;
Jul) CMONTH=07 ;;
Aug) CMONTH=08 ;;
Sep) CMONTH=09 ;;
Oct) CMONTH=10 ;;
Nov) CMONTH=11 ;;
Dec) CMONTH=12 ;;

?) return $FALSE;;
esac

echo "current is $CYEAR $CMONTH $CDAY "

return $TRUE
}


getnextday()
{
#echo "next is $NEXTLINK"
DYEAR=` echo $NEXTLINK |sed -n -e 's/.*year=\(..\).*/\1/p' `
DMONTH=` echo $NEXTLINK |sed -n -e 's/.*month=\(..\).*/\1/p' `
DDAY=` echo $NEXTLINK |sed -n -e 's/.*day=\(..\).*/\1/p' `

#echo "next is $DYEAR $DMONTH $DDAY"
return $TRUE;
}


#if is today
ifistoday()
{

if [ $CYEAR = $DYEAR ];then
if [ $CMONTH = $DMONTH ];then
if [ $CDAY = $DDAY ] ;then
exit
fi
fi
fi
return $TRUE

}

sedget_title()
{
sed -n -e 's/.*<title>\(.*\)<\/title>.*/\1/p' $1
}
sedget_titledate()
{
sed -n -e '/pod_title\.gif/{N;N;N;N;s/.*<span class="titlesmain">\(.*\)<\/span>.*/\1/p}' $1 \
| sed -n -e 's/[[:blank:]]*\([^ ].*\)/\1/p'
}
sedget_where()
{
sed -n -e '/text_where\.gif/{N;N;N;N;N;N;s/.*<span class="titlesbody">\(.*\)<\/span>.*/\1/p}' $1 \
| sed -n -e 's/[[:blank:]]*\([^ ].*\)/\1/p'
}
sedget_when()
{
sed -n -e '/text_when\.gif/{N;N;N;N;N;N;s/.*<span class="titlesbody">\(.*\)<\/span>.*/\1/p}' $1 \
| sed -n -e 's/[[:blank:]]*\([^ ].*\)/\1/p'
}
sedget_who()
{
sed -n -e '/text_photographer\.gif/{N;N;N;N;N;N;s/.*<span class="titlesbody">\(.*\)<\/span>.*/\1/p}' $1 \
| sed -n -e 's/[[:blank:]]*\([^ ].*\)/\1/p'
}
sedget_intro()
{
sed -n -e '/<td width="5" valign=/{N;N;N;N;N;N;N;N;N;p}' $1 \
| sed -e '/<td width="5" valign=/,/<span class="titlesbody">/s/<[^>]*>//gp' \
| sed -e '/<\/span>/,/.$/s/<[^>]*>//gp' \
| sed -n -e 's/[[:blank:]]*\([^ ].*\)/\1/p'
}
sedget_related()
{
sed -n -e '/text_related\.gif/{N;N;N;N;s/.*<span class="titlesbody">\(.*\)<\/span>.*/\1/p}' $1 \
| sed -n -e 's/[[:blank:]]*\([^ ].*\)/\1/p'
}

sedget_nextlink()
{
sed -n -e 's/.*<a href="\(.*\)">.*newnext\.gif.*/\1/p' $1
}
sedget_jpg400()
{
sed -n -e 's/.*\/\(pod\/pictures\/normal\/.*jpg\).*/\1/p' $1
}
sedget_jpg1024()
{
sed -n -e 's/.*\/\(pod\/pictures\/lg_wallpaper\/.*jpg\).*/\1/p' $1
}

create_introhead()
{
local INTROFILE YEAR MONTH
local -i DAY COUNT TMP
if [ $# -ne 3 ];then
return $FALSE
fi

INTROFILE=$1
if [ ! -f $INTROFILE ];then
touch $INTROFILE;
else
return $FALSE
fi
echo "<meta http-equiv=Content-Type content=\"text/html; charset=utf8\">" > $INTROFILE;
echo "<!-- htm file create and translate by eagleisyou@sohu.com -->" >>$INTROFILE
YEAR=20$2
case $3 in
01) MONTH=January; DAY=31 ;;
02) MONTH=February;
if [ $YEAR == "04" ];then
DAY=29
else
DAY=28
fi
;;
03) MONTH=March ;DAY=31 ;;
04) MONTH=April ;DAY=30 ;;
05) MONTH=Jay ;DAY=31 ;;
06) MONTH=June ;DAY=30 ;;
07) MONTH=July ;DAY=31 ;;
08) MONTH=August ;DAY=31 ;;
09) MONTH=September ;DAY=30 ;;
10) MONTH=October ;DAY=31 ;;
11) MONTH=November ;DAY=30 ;;
12) MONTH=December ;DAY=31 ;;

?) return $FALSE ;;
esac
echo "<body bgcolor=\"grey\">" >>$INTROFILE
echo "<table><center><h1>" >>$INTROFILE
echo "<a target=\"_blank\" href=\"http://lava.nationalgeographic.com/pod/index.html\">" >>$INTROFILE
echo "National Geographic Photo of the Day</a></h1><br>" >>$INTROFILE
echo "<h2>${YEAR} , ${MONTH}</h2></center></table>" >>$INTROFILE

echo "<table width=\"90%\" border=1>" >>$INTROFILE
COUNT=1
TMP=0
while [ $COUNT -le $DAY ]
do
TMP=`expr $COUNT % 5`;
if [ $TMP -eq 1 ];then
echo "<tr>" >>$INTROFILE
fi

if [ $COUNT -le 9 ];then
echo "<td width=\"20%\"><a href=\"#day0$COUNT\">day 0$COUNT</a></td>" >>$INTROFILE
else
echo "<td width=\"20%\"><a href=\"#day$COUNT\">day $COUNT</a></td>" >>$INTROFILE
fi
TMP=`expr $COUNT % 5`
if [ $TMP -eq 0 ];then
echo "</tr>" >>$INTROFILE
fi
COUNT=`expr $COUNT + 1`
done
echo "</table><br><br>" >>$INTROFILE

return $TRUE
}

update_intro()
{
local INTROFILE DAY JPG1024 JPG400
local TITLE TITLEDATE WHERE WHEN WHO INTRO RELATED

if [ $# -ne 5 ];then
return false
fi

INTROFILE=$1;
DAY=$3
JPG1024=$4
JPG400=$5

TITLE=`sedget_title $2`
#for some page,title may be empty
TITLE=${TITLE:-"National Geographic Photo of the Day:"}

TITLEDATE=`sedget_titledate $2`
WHERE=`sedget_where $2`
WHEN=`sedget_when $2`
WHO=`sedget_who $2`
INTRO=`sedget_intro $2`
RELATED=`sedget_related $2`

echo "<br><a name=\"day${DAY}\"></a><br>" >> $INTROFILE
echo "<table border=\"0\" width=\"90%\">" >>$INTROFILE
echo " <table border=\"0\" width=\"100%\" bgcolor=\"\#666666\"><tr>" >>$INTROFILE
echo " <td width=\"45%\" align=\"right\" >${TITLE}</td>" >>$INTROFILE
echo " <td align=\"center\">${TITLEDATE}</td>" >>$INTROFILE
echo " </tr></table>" >>$INTROFILE
echo " <table border=\"0\" width=\"100%\">" >>$INTROFILE
echo " <tr><td align=\"right\" width=\"40%\">" >>$INTROFILE
echo " <table border=\"0\" width=\"100%\">" >>$INTROFILE
echo " <tr><td width=\"5\" >Where:</td><td>${WHERE}</td></tr>" >>$INTROFILE
echo " <tr><td width=\"5\" >When:</td><td>${WHEN}</td></tr>" >>$INTROFILE
echo " <tr><td width=\"5\" >Photographer:</td><td>${WHO}</td></tr>" >>$INTROFILE
echo " <tr><td width=\"5\" >Related link:</td><td >${RELATED}</td></tr>" >>$INTROFILE
echo " <tr><td valign=\"middle\"><br><br><a target=\"_blank\" href=\"${JPG1024}\">View 1024x768 Photo</a></td></tr>" >>$INTROFILE
echo " </table></td>" >>$INTROFILE
echo " <td align=\"center\" width=\"400\"><img src=\"${JPG400}\" border=\"0\"></td>" >>$INTROFILE
echo " </tr>" >>$INTROFILE
echo " </table>" >>$INTROFILE
echo " <tr><td><br><br>${INTRO}<br><br><br></td></tr>" >>$INTROFILE
echo "</table>" >>$INTROFILE

return $TRUE;
}

#download ngpod
downloadngpod(){
local YEAR MONTH DAY
local DIR JPG1024 JPG400 INTROFILE
local HTMLINK1 HTMLINK2 JPGLINK1024 JPGLINK400 EXLINK
local TMPFILE1 TMPFILE2

EXLINK="http://lava.nationalgeographic.com/"
TMPFILE1=.ngpod.tmp
TMPFILE2=.ngpod.tmp.utf8
if [ $# -ne 3 ];then
return false
fi

YEAR=$DYEAR
MONTH=$DMONTH
DAY=$DDAY

DIR="ngpod/20${YEAR}/${MONTH}"
mkdir -p "${DIR}_1024x768"
mkdir -p "${DIR}_400x300"
JPG1024="${MONTH}_1024x768/ngpod20${YEAR}${MONTH}${DAY}_1024x768.jpg"
JPG400="${MONTH}_400x300/ngpod20${YEAR}${MONTH}${DAY}_400x300.jpg"
INTROFILE="ngpod/20${YEAR}/ngpod20${YEAR}${MONTH}intro.htm"
if [ ! -f ${INTROFILE} ];then
create_introhead ${INTROFILE} ${YEAR} ${MONTH};
fi

touch ${LOGFILE}
echo "++++++++++++++++++++++++++++++++++++now getting $YEAR $MONTH $DAY ngpod"

HTMLINK1="${EXLINK}cgi-bin/pod/PhotoOfTheDay.cgi?month=${MONTH}&day=${DAY}&year=${YEAR}"
HTMLINK2="${EXLINK}cgi-bin/pod/wallpaper.cgi?month=${MONTH}&day=${DAY}&year=${YEAR}"

` curl -o ${TMPFILE1} "$HTMLINK1" 2>/dev/null `
if [ ! -f ${TMPFILE1} ];then
return $FALSE;
fi
#update introduce file
dos2unix ${TMPFILE1} 2>/dev/null
iconv -f "iso8859-1" -t "utf8" -o ${TMPFILE2} ${TMPFILE1} 2>/dev/null

NEXTLINK=`sedget_nextlink $TMPFILE2`
if [ $GOON -eq 1 ];then
GOON=0
return $TURE
else
echo -n " $YEAR $MONTH $DAY " >>${LOGFILE}
update_intro ${INTROFILE} ${TMPFILE2} ${DAY} ${JPG1024} ${JPG400}
fi

JPG1024="ngpod/20${YEAR}/${JPG1024}"
JPG400="ngpod/20${YEAR}/${JPG400}"

JPGLINK400=`sedget_jpg400 ${TMPFILE2}`
JPGLINK400="${EXLINK}${JPGLINK400}"
if [ ! -f ${JPG400} ];then
` curl -o ${JPG400} ${JPGLINK400} 2>/dev/null`
fi
rm -f ${TMPFILE1} ${TMPFILE2}

#get 1024x768 jpeg file link
`curl -o $TMPFILE1 ${HTMLINK2} 2>/dev/null`
dos2unix $TMPFILE1 2>/dev/null
iconv -f "iso8859-1" -t "utf8" -o ${TMPFILE2} ${TMPFILE1} 2>/dev/null
JPGLINK1024=`sedget_jpg1024 $TMPFILE2`
JPGLINK1024="${EXLINK}${JPGLINK1024}"

if [ ! -f ${JPG1024} ];then
`curl -o ${JPG1024} ${JPGLINK1024} 2>/dev/null`
fi
if [ -f ${JPG1024} ];then
echo " OK" >> ${LOGFILE}
echo "------------------------------------ get $YEAR $MONTH $DAY ngpod OK"
else
echo "???????????????????????????????????? get $YEAR $MONTH $DAY ngpod fail"
fi


#remove tmp file
rm -f ${TMPFILE1} ${TMPFILE2}


return $TRUE
}

##########################################
# function run from here
#get the first download day
getbeginday
#get today,change
gettoday

while [ $TRUE ]
do
downloadngpod $DYEAR $DMONTH $DDAY;
ifistoday
getnextday
done
-->

2005 03 30 debug OK

請教大家,怎麼讓 read從管道裡讀輸入?
現在我的程序是先生成臨時文件,在讓read從臨時文件裡讀

enjoy

頂上去,
ps:我這個程序只下載1024x768的和400x300的圖片

引用:
-->
作者: downcloud

請教大家,怎麼讓 read從管道裡讀輸入?
現在我的程序是先生成臨時文件,在讓read從臨時文件裡讀
-->
read只能從標準輸入或是文件描述符中得到輸入,管道不行。這個在man裡有描述了,我簡略看了一下兄弟的腳本,很不錯:)
下面這句:
tail -1 $LOGFILE
改成
mytmp=$(tail -1 $LOGFILE)

可以嗎?就不必用臨時文件了 :rolleyes:

這個腳本還是有些問題的,最嚴重的是生成的htm在ie裡不是想要的效果(好像不能算bug^_^)
中午我把下載完的圖片給同事看才發現,還有其它的小問題,不一一說了

過兩天改完再發一版本上來

我正想要一個下載國家地理POD的腳本,但現在Windows下,進入Linux後一定去研究一下。

引用:
-->
作者: kiron

read只能從標準輸入或是文件描述符中得到輸入,管道不行。這個在man裡有描述了,我簡略看了一下兄弟的腳本,很不錯:)
下面這句:
tail -1 $LOGFILE
改成
mytmp=$(tail -1 $LOGFILE)

可以嗎?就不必用臨時文件了 :rolleyes:
-->

under zsh, this can be done.
代碼:
-->echo "abc" | read a
echo $a
abc-->
under bash, you do something like
代碼:
-->read a < <(echo "abc")
echo $a
abc-->


最后由 oo-BOON-too 编辑于 2008-10-09 17:46,总共编辑了 1 次

页首
 用户资料  
 
2 楼 
 文章标题 : Re: 寻找在Linux下自动更换国家地理壁纸的脚本,并[转贴]獻上一個下載NGPOD的腳本
帖子发表于 : 2008-09-29 18:03 
头像

注册: 2005-08-14 21:55
帖子: 58428
地址: 长沙
送出感谢: 4
接收感谢: 272
大于30行的,别脚本了。


_________________
● 鸣学


页首
 用户资料  
 
3 楼 
 文章标题 : Re: [问题]寻找在Linux下自动更换国家地理壁纸的脚本,并[转贴]獻上一個下載NGPOD的腳本
帖子发表于 : 2013-05-05 17:18 
头像

注册: 2008-11-01 20:43
帖子: 17322
系统: 自动检测比较好
送出感谢: 44
接收感谢: 11
代码:
#!/bin/bash
#Description:
# download national geographic photo of the day and set it as background

# wait for network connection to be established
[[ -n $1 ]] && sleep $1

resolution=$(xrandr |grep -o 'current [^,]*'|tr -d " "|sed 's:current::')

rss=`wget -q -O -
http://feeds.nationalgeographic.com/ng/photography/photo-of-the-day/`

img_url=$(echo $rss|grep -o "http://[^\"]*exposure[^\"]*"|head -1)

img_url=$(echo $img_url|sed "s:360x270:$resolution:")

img=/tmp/pod.jpg

wget -q -O $img $img_url

feh --bg-scale $img


_________________
谁是我?KL.Uiq8 YinMeng


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 3 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 2 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译