Bash Monitor

Discussion in 'Scripts' started by buttlust, Jan 10, 2014.

  1. buttlust

    buttlust User

    Joined:
    Nov 2, 2013
    Messages:
    512
    Likes Received:
    0
    Updated 1/14/2014: fixed an error involving the same text twice overriding the previous blacklist. And fixed the title for multiple images from RnR.

    Code:
    #!/bin/bash
    set -o pipefail
    #Downloads search result page from mturk and looks for certain text.
    #Requires curl, espeak optional
    #v3 - 1/14/2014
    # Note: Certain special characters in the Text array need their HTML counterpart, ex.: ' shows up as '
    # A nice chart to help with the above:
    # http://www.ic.unicamp.br/~stolfi/EXPORT/www/ISO-8859-1-Encoding.html
    
    Links=('https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&requesterId=A2BAP2QO7MMQI9' 'https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&requesterId=A2BAP2QO7MMQI9' 'https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&requesterId=A11L036EBWKONR' 'https://www.mturk.com/mturk/searchbar?selectedSearchType=hitgroups&requesterId=A35GBZ8TKR3UKC')
    Text=('Identify whether these images are adult.  (WARNING: This HIT may contain adult content. Worker discretion is advised.)' 'Identify whether these multiple images are adult.  (WARNING: This HIT may contain adult content. Worker discretion is advised.)' 'sortresults_form' 'sortresults_form')
    blist () {
    while read F ; do
        if [[ $F == "$i ${Text[i]}" ]]; then #If text matches file
            return 1
        fi
    done < /tmp/blacklist.txt
    }
    curly () {
    curl -s -L $1 | grep "${Text[i]}" | grep -v '<td' | sed -e 's/^[ \t]*//'
    if [[ "$?" == "0" ]]; then
        if blist $1; then #Add to blacklist and notify
            echo "$i ${Text[i]}" >> /tmp/blacklist.txt
            if [[ $((i+1)) == 8 ]]; then #Rename because vague
                rename='Project Endor'
            elif [[ $((i+1)) == 11 ]]; then
                rename='Andy K'
            else
                rename=''
            fi
            #notify-send "${Text[i]} $rename" #This doesn't work because of cron
            espeak -ven+f6 -k5 -s140 "number $((i+1)). ${Text[i]} $rename" #Talking computers
            echo "`date +%l:%M%p` [`date +%x`] ($((i+1))) ${Text[i]} $rename" >> /home/cody/Desktop/MISSED_HITS.txt
        fi
    else
        sed -i "/$i ${Text[i]}/d" /tmp/blacklist.txt #Offline? Remove from blacklist.
    fi
    }
    if [ ! -f /tmp/blacklist.txt ]; then
        touch /tmp/blacklist.txt
        chmod 777 /tmp/blacklist.txt
    fi
    for (( i = 0 ; i < ${#Text[@]} ; i++ )) do
        curly $(echo ${Links[i]})
        sleep 1
    done
    
    You need some form of GNU/Linux to run this script, and a little experience doesn't hurt. It was tested on Ubuntu and Debian. Might be able to run it on Windows using Cygwin, but I don't know anything about doing that.

    A little about this script:
    It downloads each search page, then looks for the corresponding text on the page. If it finds it, it notifies you (with espeak) through your speakers, and places a text file on your desktop. It also adds the successful result to a blacklist, so that if it finds it again the next time, it won't notify you. There's also a section to add a name to some results, because you may want to catch everything from that requester. This script is particularly useful for headless "servers" running in or near your room, or any computer that you leave on 24/7. Old laptops work well, your desktop works well, and I have it running 24/7 on a Raspberry Pi right now.

    To use:
    First, install curl and espeak.
    Code:
    sudo apt-get install curl espeak
    Next, copy the script into a file, named BM.sh in your user's home folder.
    Make the file executable:
    Code:
    chmod +x BM.sh
    Plug in some speakers, and test espeak:
    Code:
    espeak "This is a test."
    If everything is good and you just heard a computer voice, execute the script:
    Code:
    ./BM.sh
    If you see no errors or warnings, the script worked successfully. Now you can add it to your cron:
    Code:
    crontab -e
    
    */5 * * * * /home/username/BM.sh
    This will tell the script to run every 5 minutes. If you want some hits to be checked at 15m, some to be checked at 1m, etc., make multiple scripts and put a new line for each script in your cron.

    That's all there is to it. If you want your missed hits to appear every time you login/open a terminal, run sudo nano ~/.bashrc
    Add these lines at the bottom:
    Code:
    if [ -f /home/username/Desktop/MISSED_HITS.txt ]; then
        cat /home/username/Desktop/MISSED_HITS.txt
        rm /home/username/Desktop/MISSED_HITS.txt
    else
        echo "No hits were missed."
    fi
    I added a few examples to help you get started. Be sure to replace "username" with your actual username in the scripts.
    Feel free to edit this script in any way you see fit, redistribute however you want, etc.
     
    #1 buttlust, Jan 10, 2014
    Last edited by a moderator: Jan 14, 2014

Share This Page