Last active
August 9, 2024 07:32
-
-
Save theSoberSobber/bc922d4c5bd2acf1e264240dfcc39433 to your computer and use it in GitHub Desktop.
Termux CronTab Script Codeforces, (crontab -e, */15 * * * * /data/data/com.termux/files/home/monitor/data/main.sh >> /data/data/com.termux/files/home/monitor/data/logs.txt 2>&1 , nohup crond -n &>/dev/null &)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Function for colored logging | |
debug() { | |
local mode="$1" | |
local log="$2" | |
local color="$3" | |
case "$mode" in | |
fetch) echo -e "\e[1;36m[Fetch]\e[0m $log" ;; | |
value) echo -e "\e[1;34m[Value]\e[0m $log" ;; | |
update) echo -e "\e[1;32m[Update]\e[0m $log" ;; | |
notify) echo -e "\e[1;33m[Notify]\e[0m $log" ;; | |
*) echo "$log" ;; | |
esac | |
} | |
# Function to extract numeric value from the string | |
extract_numeric_value() { | |
local str="$1" | |
echo "$str" | grep -oP '\d+' | |
} | |
# Echo separators and current time | |
echo "Current Time: $(date)" | |
echo "---------------------" | |
# Function to fetch value from URL | |
fetch_value() { | |
local url="$1" | |
curl -s "$url" | grep -oP '(?<=<div class="_UserActivityFrame_counterValue">).*?(?=<\/div>)' | head -n 1 | |
} | |
# Create 'data' directory if it doesn't exist | |
if [ ! -d "/data/data/com.termux/files/home/monitor/data" ]; then | |
mkdir "/data/data/com.termux/files/home/monitor/data" | |
fi | |
# Read CSV file and process each entry | |
while IFS=, read -r entry; do | |
url="https://codeforces.com/profile/$entry" | |
debug fetch "Fetching value for $entry" cyan | |
value=$(fetch_value "$url") | |
if [ -n "$value" ]; then | |
debug value "Value for $entry: $value" blue | |
filename="/data/data/com.termux/files/home/monitor/data/$entry.txt" | |
stored_value="-1 problem" # Initialize stored value | |
last_checked_file="/data/data/com.termux/files/home/monitor/data/$entry-lastChecked.txt" | |
# Initialize last checked variable with current date | |
last_checked=$(date +"%b %d,%H:%M") | |
if [ -f "$last_checked_file" ]; then | |
last_checked=$(cat "$last_checked_file") | |
fi | |
if [ -f "$filename" ]; then | |
stored_value=$(cat "$filename") | |
fi | |
if [ "$value" != "$stored_value" ]; then | |
debug update "Updating file $entry" green | |
echo "$value" > "$filename" | |
debug notify "Notifying" yellow | |
# Extract numeric values for comparison | |
new_value_numeric=$(extract_numeric_value "$value") | |
stored_value_numeric=$(extract_numeric_value "$stored_value") | |
# Calculate change in values | |
change=$((new_value_numeric - stored_value_numeric)) | |
# Determine sign for change | |
sign="" | |
if [ "$change" -gt 0 ]; then | |
sign="+" | |
fi | |
# Construct notification content | |
notif_content="New value: $value | |
Previous value: $stored_value | |
Change: $sign$change | |
Last Checked: $last_checked" | |
# Send notification | |
termux-notification -t "[Codeforces] $entry $sign$change" -c "$notif_content" --priority high | |
fi | |
# Update last checked timestamp after each iteration | |
echo "$(date +"%b %d,%H:%M")" > "$last_checked_file" | |
else | |
debug fetch "Skipping $entry - Fetch failed" red | |
fi | |
echo "---------------------" # Separator after each entry's processing | |
sleep 2 # 2-second delay after each fetch | |
done < /data/data/com.termux/files/home/monitor/input.csv | |
#input.csv | |
#a, | |
#b, | |
#c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
untested but has rate limit