File size: 912 Bytes
57b8366
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# core/visits.py
from pathlib import Path

COUNTER_FILE = Path("visits.txt")

def get_and_update_visits():
    """Reads the current visit count, increments it, writes it back, and returns the new count."""
    if not COUNTER_FILE.exists():
        count = 1
    else:
        try:
            count = int(COUNTER_FILE.read_text()) + 1
        except (ValueError, IOError):
            count = 1  # Reset counter if file is corrupted
    
    try:
        COUNTER_FILE.write_text(str(count))
    except IOError as e:
        print(f"Error writing to counter file: {e}")

    return count

def get_current_visit_count():
    """Reads and returns the current visit count without incrementing it."""
    if not COUNTER_FILE.exists():
        return 0
    try:
        return int(COUNTER_FILE.read_text())
    except (ValueError, IOError):
        return 0 # Return 0 if file is corrupted