Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
access-token
email
pass
vk_auth_token.temp
vk_auth_token.temp
friends-state.json
9 changes: 7 additions & 2 deletions Application/acceptAllFriendRequests.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
var requests = API.friends.getRequests({ count: 23 }).items;
var i = 0;
var accepted = [];
var errors = [];
while(i < requests.length)
{
API.friends.add({ user_id: requests[i] });
var result = API.friends.add({ user_id: requests[i] });
if (result == 1 || result == 2 || result == 4) {
accepted.push(requests[i]);
}
i = i + 1;
}
return requests;
return { "accepted": accepted, "total_requests": requests.length };
1 change: 1 addition & 0 deletions Application/getFriendsCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
return API.friends.get({ count: 1 }).count;
10 changes: 4 additions & 6 deletions do-all.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
#!/bin/bash
set -e

./delete-out-friend-request.sh
# Initial run with friends limit management
./manage-friends-limit.sh

sleep 16

./delete-first-deactivated-friend.sh

sleep 16

./accept-all-friend-requests-once.sh

# Main loop with friends limit checks
while :
do
sleep 600

./delete-out-friend-request.sh
./manage-friends-limit.sh

sleep 600

./delete-first-deactivated-friend.sh

sleep 600

./accept-all-friend-requests-once.sh
done
87 changes: 87 additions & 0 deletions experiments/test-friends-limit-logic.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash
# Test script for friends limit logic
# This script simulates different scenarios to test the friends limit handling

set -e

echo "Testing friends limit management logic..."
echo "=========================================="
echo ""

# Test 1: Check if get-friends-count.sh works
echo "Test 1: Check friends count retrieval"
echo "--------------------------------------"
if [ -f "../get-friends-count.sh" ]; then
echo "✓ get-friends-count.sh exists"
else
echo "✗ get-friends-count.sh not found"
exit 1
fi

# Test 2: Check if manage-friends-limit.sh exists
echo ""
echo "Test 2: Check manage-friends-limit.sh exists"
echo "---------------------------------------------"
if [ -f "../manage-friends-limit.sh" ]; then
echo "✓ manage-friends-limit.sh exists"
if [ -x "../manage-friends-limit.sh" ]; then
echo "✓ manage-friends-limit.sh is executable"
else
echo "✗ manage-friends-limit.sh is not executable"
exit 1
fi
else
echo "✗ manage-friends-limit.sh not found"
exit 1
fi

# Test 3: Check if VK API scripts are present
echo ""
echo "Test 3: Check VK API scripts"
echo "-----------------------------"
for script in "Application/getFriendsCount.js" "Application/acceptAllFriendRequests.js" "Application/deleteInactiveFriends.js" "Application/deleteOutFriendRequests.js"; do
if [ -f "../$script" ]; then
echo "✓ $script exists"
else
echo "✗ $script not found"
exit 1
fi
done

# Test 4: Check updated acceptAllFriendRequests.js structure
echo ""
echo "Test 4: Validate acceptAllFriendRequests.js changes"
echo "----------------------------------------------------"
if grep -q "accepted" "../Application/acceptAllFriendRequests.js" && grep -q "total_requests" "../Application/acceptAllFriendRequests.js"; then
echo "✓ acceptAllFriendRequests.js has been updated with proper return structure"
else
echo "✗ acceptAllFriendRequests.js missing expected changes"
exit 1
fi

# Test 5: Check .gitignore
echo ""
echo "Test 5: Check .gitignore includes friends-state.json"
echo "------------------------------------------------------"
if grep -q "friends-state.json" "../.gitignore"; then
echo "✓ friends-state.json is in .gitignore"
else
echo "✗ friends-state.json not in .gitignore"
exit 1
fi

# Test 6: Check do-all.sh integration
echo ""
echo "Test 6: Check do-all.sh uses manage-friends-limit.sh"
echo "-----------------------------------------------------"
if grep -q "manage-friends-limit.sh" "../do-all.sh"; then
echo "✓ do-all.sh integrated with manage-friends-limit.sh"
else
echo "✗ do-all.sh not using manage-friends-limit.sh"
exit 1
fi

echo ""
echo "=========================================="
echo "All tests passed! ✓"
echo "=========================================="
8 changes: 8 additions & 0 deletions get-friends-count.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
set -e

ACCESS_TOKEN=`cat access-token`

./execute.sh "https://api.vk.com/method/execute.getFriendsCount?access_token=${ACCESS_TOKEN}&v=5.131"

echo
87 changes: 87 additions & 0 deletions manage-friends-limit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#!/bin/bash
set -e

# Configuration
FRIENDS_LIMIT=10000
STATE_FILE="friends-state.json"
SAFETY_MARGIN=50 # Keep some room below the limit

# Get current friends count from VK API
get_friends_count() {
local result=$(./get-friends-count.sh 2>/dev/null | jq -r '.response // 0')
echo "$result"
}

# Save state to file
save_state() {
local count=$1
local timestamp=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
echo "{\"friends_count\": $count, \"last_updated\": \"$timestamp\"}" > "$STATE_FILE"
}

# Load state from file
load_state() {
if [ -f "$STATE_FILE" ]; then
cat "$STATE_FILE" | jq -r '.friends_count // 0'
else
echo "0"
fi
}

# Check if we're at or near the limit
is_at_limit() {
local count=$1
if [ "$count" -ge $((FRIENDS_LIMIT - SAFETY_MARGIN)) ]; then
return 0 # true, at limit
else
return 1 # false, not at limit
fi
}

# Main logic
echo "Checking friends count..."
CURRENT_COUNT=$(get_friends_count)
echo "Current friends count: $CURRENT_COUNT"

# Save state
save_state "$CURRENT_COUNT"

if is_at_limit "$CURRENT_COUNT"; then
echo "⚠️ Friends limit reached ($CURRENT_COUNT >= $((FRIENDS_LIMIT - SAFETY_MARGIN)))"
echo "Starting cleanup process..."

echo "Step 1: Canceling outgoing friend requests..."
./delete-out-friend-request.sh

echo "Step 2: Deleting inactive friends..."
# Try to delete inactive friends in batches
for offset in {0..9}; do
echo " Deleting inactive friends batch $offset..."
./delete-inactive-friends.sh "$offset" || true
sleep 2
done

# Update count after cleanup
echo "Updating friends count after cleanup..."
CURRENT_COUNT=$(get_friends_count)
echo "Friends count after cleanup: $CURRENT_COUNT"
save_state "$CURRENT_COUNT"

if is_at_limit "$CURRENT_COUNT"; then
echo "⚠️ Still at limit after cleanup. Skipping friend request acceptance."
exit 0
else
echo "✓ Cleanup successful. Below limit now."
fi
else
echo "✓ Friends count is below limit."
fi

# Only accept friend requests if we're below the limit
echo "Accepting friend requests..."
./accept-all-friend-requests-once.sh

# Final count update
FINAL_COUNT=$(get_friends_count)
echo "Final friends count: $FINAL_COUNT"
save_state "$FINAL_COUNT"