-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_auto_commit.py
103 lines (83 loc) · 3.32 KB
/
github_auto_commit.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import random
import time
from datetime import datetime
import logging
from git import Repo
import schedule
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('github_commits.log'),
logging.StreamHandler()
]
)
class GitHubAutoCommitter:
def __init__(self, repo_path, target_file):
self.repo_path = repo_path
self.target_file = target_file
self.repo = Repo(repo_path)
self.daily_commits = 0
self.max_daily_commits = random.randint(8, 25)
# Configure git user
with self.repo.config_writer() as git_config:
git_config.set_value('user', 'email', '[email protected]')
git_config.set_value('user', 'name', 'rahulrajpvr7d')
def modify_file(self, add_comma=True):
try:
with open(self.target_file, 'r') as file:
content = file.read().strip()
new_content = content + ", " if add_comma else content.rstrip(", ")
with open(self.target_file, 'w') as file:
file.write(new_content)
return True
except Exception as e:
logging.error(f"Error modifying file: {str(e)}")
return False
def commit_changes(self, message):
try:
self.repo.index.add([self.target_file])
self.repo.index.commit(message)
origin = self.repo.remote('origin')
origin.push()
return True
except Exception as e:
logging.error(f"Error committing changes: {str(e)}")
return False
def make_random_commit(self):
if self.daily_commits >= self.max_daily_commits:
logging.info("Daily commit limit reached")
return
current_hour = datetime.now().hour
if not (9 <= current_hour <= 23): # Only commit between 9 AM and 11 PM
return
add_comma = random.choice([True, False])
action = "Adding" if add_comma else "Removing"
if self.modify_file(add_comma):
if self.commit_changes(f"{action} comma - Auto commit"):
self.daily_commits += 1
logging.info(f"Successfully made commit {self.daily_commits}/{self.max_daily_commits}")
# Schedule next commit
wait_time = random.randint(1800, 7200) # 30 mins to 2 hours
schedule.every(wait_time).seconds.do(self.make_random_commit).tag('commit_job')
def reset_daily_counter(self):
self.daily_commits = 0
self.max_daily_commits = random.randint(8, 25)
logging.info(f"Reset daily counter. New max commits: {self.max_daily_commits}")
def main():
# Update with actual paths
REPO_PATH = "C:/Users/rahul/OneDrive/Desktop/Auto encoder variable"
TARGET_FILE = "C:/Users/rahul/OneDrive/Desktop/Auto encoder variable/data.txt"
committer = GitHubAutoCommitter(REPO_PATH, TARGET_FILE)
# Schedule daily reset at midnight
schedule.every().day.at("00:00").do(committer.reset_daily_counter)
# Initial commit
committer.make_random_commit()
# Keep the script running
while True:
schedule.run_pending()
time.sleep(60)
if __name__ == "__main__":
main()