|
#!/bin/bash |
|
# FILENAME: get_nec_info.sh |
|
# HOW TO RUN: ./get_nec_info.sh |
|
# WARNING: this script is meant for modification, minimum modification is just adjust UN1, and PW1. The rest depends on if you have certain programs that I use, and if you want to send the results to a webserver you should modify the last ssh line to meet your needs |
|
# BEFORE RUNNING SCRIPT: make sure you have bash version 3 (4 is preferable), also make sure you have curl |
|
# BEFORE RUNNING SCRIPT: edit UN1 and PW1 variables at the top to match your next-episode.net username and password information |
|
# BEFORE RUNNING SCRIPT: make sure you have basename and logger program as well, if not just comment those lines out. Also comment out the bottom section if you dont want to send to a web server (I use ssh to sent to a webserver, you can edit it for whatever) |
|
# -- variables -- # |
|
UN1="username-for-next-episode" # https://next-episode.net/ username |
|
PW1="password-for-next-episode" # and password |
|
DIRNAME=$(dirname $(readlink -f "$0")) |
|
COOKIE1="$DIRNAME/xcookie.txt" |
|
OUT1="$DIRNAME/xout1_full-cal-html.txt" |
|
OUT2="$DIRNAME/xout2_days-html.txt" |
|
OUT3="$DIRNAME/xout3_todays-html.txt" |
|
OUT4="$DIRNAME/xout4_final-full-month.txt" |
|
OUT5="$DIRNAME/xout5_final-today.txt" |
|
OUTNICE="$DIRNAME/xout_nice.txt" |
|
AUTHOR="infotinks" |
|
###### Commented out section that copied to my infotinks server with 6 hashmarks |
|
###### # for sending to remote server |
|
###### SSH_KEY="/root/keys/kvknew2016.private.openssh" |
|
###### REMOTE_PATH="root@www.infotinks.com:/var/www/nec/next-episode-nice.txt" |
|
# -- loggin -- # |
|
SCRIPTNAME=`basename $0` |
|
logger "${SCRIPTNAME} - getting next_episode.net today and full month info" |
|
# print header |
|
echo "=========== [`date +%D-%T`|`date +%s`s] `basename $0` ===========" |
|
# -- web calls -- # |
|
echo "- login to next-episode.net" |
|
curl -s -c ${COOKIE1} --data "username=${UN1}&password=${PW1}" https://next-episode.net/userlogin 2>&1 > /dev/null || { echo "ERROR: failed at login; Error code $?"; logger "${SCRIPTNAME} - ERROR: failed at login"; exit 1; } |
|
echo "- view calendar & save it to a file" |
|
curl -s -b ${COOKIE1} https://next-episode.net/calendar/ -o "${OUT1}" 2>&1 > /dev/null || { echo "ERROR: failed at downloading calendar"; logger "${SCRIPTNAME} - ERROR: failed at downloading calendar"; exit 2; } |
|
echo "- logout out of next-episode.net" |
|
curl -s -c ${COOKIE1} -b ${COOKIE1} https://next-episode.net/logout 2>&1 > /dev/null |
|
rm ${COOKIE1} |
|
# if OUT1 is empty that means we didnt download the script well |
|
RESULTS1LENGTH=$(cat "${OUT1}" | wc -l) |
|
if [ ${RESULTS1LENGTH} -eq 0 ]; then echo "ERROR: downloaded an empty file"; exit -1; fi; |
|
# TO DO: PARSE SCRIPT |
|
# --- FIRST --- #: i noticed that all of the calendar data in the /calendar page output is between beforedayboxes & afterdayboxes |
|
cat ${OUT1} | grep -A1000000 "beforedayboxes" | grep -B1000000 "afterdayboxes" > ${OUT2} # extracting all days of month portion |
|
# if first way didnt find anything its because they removed afterdayboxes, but thats fine right below the results (a couple lines below at least) we get </table></td> unique marker (its not as good as afterdayboxes but it does thee job) |
|
RESULTS2LENGTH=$(cat "${OUT2}" | wc -l) |
|
if [ ${RESULTS2LENGTH} -eq 0 ]; then |
|
echo "* First Parse Script didnt work with 'afterdayboxes' keyword-marker using second method with html tags"; |
|
# --- SECOND --- #: i noticed that all of the calendar data in the /calendar page output is just after beforedayboxes (afterdayboxes stopped existing & OUT2 started being empty so results were null, this fixed it) |
|
cat ${OUT1} | grep -A1000000 "beforedayboxes" | grep -B1000000000 '</table></td>' > ${OUT2} # extracting all days of month portion |
|
RESULTS2LENGTH=$(cat "${OUT2}" | wc -l)"second" # recalculate RESULTS2LENGTH for debug output at the end |
|
RESULTS2ALENGTH=$(cat "${OUT2}" | wc -l) # checking if empty again (so results1,results2, and now results3 which i call results 2a) |
|
if [ ${RESULTS2ALENGTH} -eq 0 ]; then |
|
# copy contents of out1 to out2, as out1 by itself does fine.. meaning out1 can be used as out2 and generate results |
|
# first results is looking between "beforedayboxes" and "afterdayboxes" |
|
# second results is looking after "beforedayboxes" |
|
# third is just the whole OUT1 (which is the calendar output) |
|
# --- THIRD --- #: i noticed that it doesnt matter if you look for afterdayboxes or beforedayboxes, because the prasing sed commands will correctly parse everything, so instead of trying to make OUT2 smaller by triangulating in on the calendar data in OUT1 (which is the /calendar page), we just use the calendar output as OUT2 and parse thru that. So basically we can just skip FIRST and SECOND and just do THIRD always & it will be better.. but too lazy to just convery to THIRD, even though it would only be a few line comments (but then there is also all of those debug statements which are annoying) |
|
cat "${OUT1}" > "${OUT2}" |
|
RESULTS2LENGTH=$(cat "${OUT2}" | wc -l)"third" # recalculate the length again & store it into resutls2length (technically should be called results3 but that is taken up) |
|
fi |
|
fi |
|
# Get today and tommorow day so we can look inbetween those days |
|
EPOCHNOW=$(date +%s) # get todays epoch sec time |
|
EPOCHTOMMOROW=$((EPOCHNOW+$((60*60*24)))) |
|
DAYNUMNOW=$(date --date "@${EPOCHNOW}" +%d | sed 's/^0*//') # removing leading zero with sed |
|
DAYNUMTOMMOROW=$(date --date "@${EPOCHTOMMOROW}" +%d | sed 's/^0*//') |
|
if [ $DAYNUMTOMMOROW -gt $DAYNUMNOW ]; then |
|
echo "- date is NOT End of Month, Looking Between Day $DAYNUMNOW and $DAYNUMTOMMOROW" |
|
cat ${OUT2} | grep -A1000000 "^${DAYNUMNOW}</span> </div>" | grep -B1000000 "^${DAYNUMTOMMOROW}</span> </div>" > ${OUT3} # extracting day |
|
else |
|
echo "- date is end of the Month, Looking Between Day $DAYNUMNOW and $DAYNUMTOMMOROW" |
|
cat ${OUT2} | grep -A1000000 "^${DAYNUMNOW}</span> </div>" | grep -B1000000 "\"afterdayboxes\"" > ${OUT3} # extracting last day |
|
fi |
|
# shows from all month (adds new line before "a title" tag, greps out dates and a title (so now only episodes and dates with html tags), then looks for title in a title to remove html tag on episode, then looks for day on date tag and surrounds it with ---DAY--- |
|
cat ${OUT2} | sed 's/<a title/\\\n<a title/g' | egrep "a title|^[0-9]*</span>\ </div>" | sed 's/^.*a title = "\(.*\)" href.*$/\1/g' | sed 's/^\(.* - [0-9]*x[0-9]*\) -.*$/\1/g;s/^\([0-9]*\)<\/span> <\/div>$/---\1---/g' > ${OUT4} |
|
cat ${OUT3} | sed 's/<a title/\\\n<a title/g' | sed -n 's/^.*a title = "\(.*\)" href.*$/\1/pg' | sed 's/^\(.* - [0-9]*x[0-9]*\) -.*$/\1/g' > ${OUT5} |
|
RESULTS3LENGTH=$(cat "${OUT3}" | wc -l) |
|
RESULTS4LENGTH=$(cat "${OUT4}" | wc -l) |
|
RESULTS5LENGTH=$(cat "${OUT5}" | wc -l) |
|
echo "- This Months output from the next-episode: ${OUT4}" |
|
echo "cat ${OUT4}" |
|
echo "- Todays output from the next-episode: ${OUT5}" |
|
echo "*********** todays episode list **************" |
|
cat ${OUT5} |
|
echo "**********************************************" |
|
# -- save to nice -- # |
|
echo "Episodes To Watch Today from next-episode.net" > ${OUTNICE} |
|
echo "By: $AUTHOR - Date: `date` - Epoch: `date +%s` s" >> ${OUTNICE} |
|
echo >> ${OUTNICE} |
|
echo "#### --- TODAY: `date +%D` --- ####" >> ${OUTNICE} |
|
cat ${OUT5} >> ${OUTNICE} |
|
echo >> ${OUTNICE} |
|
echo "#### --- WHOLE MONTH --- ###" >> ${OUTNICE} |
|
cat ${OUT4} >> ${OUTNICE} |
|
echo "- deleting extra files (out1, out2, out3, and cookie1)" |
|
rm -f "${OUT1}" "${OUT2}" "${OUT3}" "${COOKIE1}" |
|
###### optional: send to webserver over ssh (provided ssh key) |
|
###### echo -n "- sending to webserver: $REMOTE_PATH:" |
|
###### rsync -av -e "ssh -i $SSH_KEY" "$OUTNICE" "$REMOTE_PATH" && { echo ' success!'; SENT="success"; } || { echo " fail! Error Code $?"; SENT="fail (error $?)"; } |
|
logger "${SCRIPTNAME} - Complete & saved to disk - optional: sent to webserver: ${SENT}" |
|
### show lines in results ### |
|
echo "- DEBUG: Number of lines in xout1,2,3,4,5: ${RESULTS1LENGTH}/${RESULTS2LENGTH}/${RESULTS3LENGTH}/${RESULTS4LENGTH}/${RESULTS5LENGTH}" |
|
echo "- DEBUG: Number of lines in this Months(xout4) / Today(xout5) Results: ${RESULTS4LENGTH}/${RESULTS5LENGTH}" |
|
# EOF |