This repository was archived by the owner on Jan 6, 2026. It is now read-only.
forked from charlesbel/Microsoft-Rewards-Farmer
-
-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathmain.py
More file actions
278 lines (225 loc) · 9.23 KB
/
Copy pathmain.py
File metadata and controls
278 lines (225 loc) · 9.23 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import csv
import json
import logging
import logging.config
import sys
from datetime import datetime
from enum import Enum, auto
from logging import handlers
from src import (
Browser,
Login,
PunchCards,
Searches,
ReadToEarn,
)
from src.activities import Activities
from src.browser import RemainingSearches
from src.loggingColoredFormatter import ColoredFormatter
from src.utils import CONFIG, APPRISE, getProjectRoot, formatNumber
def main():
setupLogging()
# Load previous day's points data
previous_points_data = load_previous_points_data()
foundError = False
for currentAccount in CONFIG.accounts:
try:
earned_points = executeBot(currentAccount)
except Exception as e1:
logging.error("", exc_info=True)
foundError = True
if CONFIG.get("apprise.notify.uncaught-exception"):
APPRISE.notify(
f"{type(e1).__name__}: {e1}",
f"⚠️ Error executing {currentAccount.email}, please check the log",
)
continue
previous_points = previous_points_data.get(currentAccount.email, 0)
# Calculate the difference in points from the prior day
points_difference = earned_points - previous_points
# Append the daily points and points difference to CSV and Excel
log_daily_points_to_csv(earned_points, points_difference)
# Update the previous day's points data
previous_points_data[currentAccount.email] = earned_points
logging.info(
f"[POINTS] Data for '{currentAccount.email}' appended to the file."
)
# Save the current day's points data for the next day in the "logs" folder
save_previous_points_data(previous_points_data)
logging.info("[POINTS] Data saved for the next day.")
if foundError:
sys.exit(1)
def log_daily_points_to_csv(earned_points, points_difference):
logs_directory = getProjectRoot() / "logs"
csv_filename = logs_directory / "points_data.csv"
# Create a new row with the date, daily points, and points difference
date = datetime.now().strftime("%Y-%m-%d")
new_row = {
"Date": date,
"Earned Points": earned_points,
"Points Difference": points_difference,
}
fieldnames = ["Date", "Earned Points", "Points Difference"]
is_new_file = not csv_filename.exists()
with open(csv_filename, mode="a", newline="", encoding="utf-8") as file:
writer = csv.DictWriter(file, fieldnames=fieldnames)
if is_new_file:
writer.writeheader()
writer.writerow(new_row)
def setupLogging():
_format = CONFIG.logging.format
terminalHandler = logging.StreamHandler(sys.stdout)
terminalHandler.setFormatter(ColoredFormatter(_format))
terminalHandler.setLevel(logging.getLevelName(CONFIG.logging.level.upper()))
logs_directory = getProjectRoot() / "logs"
logs_directory.mkdir(parents=True, exist_ok=True)
fileHandler = handlers.TimedRotatingFileHandler(
logs_directory / "activity.log",
when="midnight",
backupCount=2,
encoding="utf-8",
)
fileHandler.namer = lambda name: name.replace('.log.', '-') + '.log'
fileHandler.setLevel(logging.DEBUG)
logging.config.dictConfig({
"version": 1,
"disable_existing_loggers": True,
})
logging.basicConfig(
level=logging.DEBUG,
format=_format,
handlers=[fileHandler, terminalHandler],
force=True,
)
class AppriseSummary(Enum):
"""
configures how results are summarized via Apprise
"""
ALWAYS = auto()
"""
the default, as it was before, how many points were gained and goal percentage if set
"""
ON_ERROR = auto()
"""
only sends email if for some reason there's remaining searches
"""
NEVER = auto()
"""
never send summary
"""
def executeBot(currentAccount):
logging.info(f"********************{currentAccount.email}********************")
startingPoints: int | None = None
accountPoints: int
remainingSearches: RemainingSearches
goalTitle: str
goalPoints: int
if CONFIG.search.type in ("desktop", "both", None):
with Browser(mobile=False, account=currentAccount) as desktopBrowser:
utils = desktopBrowser.utils
Login(desktopBrowser).login()
startingPoints = utils.getAccountPoints()
logging.info(
f"[POINTS] You have {formatNumber(startingPoints)} points on your account - Searching First"
)
with Searches(desktopBrowser) as searches:
searches.bingSearches()
# Activities After Search For Debugging
Activities(desktopBrowser).completeActivities()
PunchCards(desktopBrowser).completePunchCards()
# VersusGame(desktopBrowser).completeVersusGame()
goalPoints = 100 # utils.getGoalPoints()
goalTitle = "Title" # utils.getGoalTitle()
remainingSearches = desktopBrowser.getRemainingSearches(
desktopAndMobile=True
)
accountPoints = utils.getAccountPoints()
if CONFIG.search.type in ("mobile", "both", None):
with Browser(mobile=True, account=currentAccount) as mobileBrowser:
utils = mobileBrowser.utils
Login(mobileBrowser).login()
if startingPoints is None:
startingPoints = utils.getAccountPoints()
try:
ReadToEarn(mobileBrowser).completeReadToEarn()
except Exception:
logging.exception("[READ TO EARN] Failed to complete Read to Earn")
with Searches(mobileBrowser) as searches:
searches.bingSearches()
goalPoints = utils.getGoalPoints()
goalTitle = utils.getGoalTitle()
remainingSearches = mobileBrowser.getRemainingSearches(
desktopAndMobile=True
)
accountPoints = utils.getAccountPoints()
logging.info(
f"[POINTS] You have earned {formatNumber(accountPoints - startingPoints)} points this run !"
)
logging.info(f"[POINTS] You are now at {formatNumber(accountPoints)} points !")
appriseSummary = AppriseSummary[CONFIG.apprise.summary]
if appriseSummary == AppriseSummary.ALWAYS:
goalStatus = ""
if goalPoints > 0:
logging.info(
f"[POINTS] You are now at {(formatNumber((accountPoints / goalPoints) * 100))}%"
f" of your goal ({goalTitle}) !"
)
goalStatus = (
f"🎯 Goal reached: {(formatNumber((accountPoints / goalPoints) * 100))}%"
f" ({goalTitle})"
)
APPRISE.notify(
"\n".join(
[
f"👤 Account: {currentAccount.email}",
f"⭐️ Points earned today: {formatNumber(accountPoints - startingPoints)}",
f"💰 Total points: {formatNumber(accountPoints)}",
goalStatus,
]
),
"Daily Points Update",
)
elif appriseSummary == AppriseSummary.ON_ERROR:
if remainingSearches.getTotal() > 0:
APPRISE.notify(
f"account email: {currentAccount.email}, {remainingSearches}",
"Error: remaining searches",
)
elif appriseSummary == AppriseSummary.NEVER:
pass
return accountPoints
def export_points_to_csv(points_data):
logs_directory = getProjectRoot() / "logs"
csv_filename = logs_directory / "points_data.csv"
with open(csv_filename, mode="a", newline="", encoding="utf-8") as file:
fieldnames = ["Account", "Earned Points", "Points Difference"]
writer = csv.DictWriter(file, fieldnames=fieldnames)
# Check if the file is empty, and if so, write the header row
if file.tell() == 0:
writer.writeheader()
for data in points_data:
writer.writerow(data)
# Define a function to load the previous day's points data from a file in the "logs" folder
def load_previous_points_data():
try:
with open(
getProjectRoot() / "logs" / "previous_points_data.json", encoding='utf-8') as file:
return json.load(file)
except FileNotFoundError:
return {}
# Define a function to save the current day's points data for the next day in the "logs" folder
def save_previous_points_data(data):
logs_directory = getProjectRoot() / "logs"
with open(logs_directory / "previous_points_data.json", "w", encoding="utf-8") as file:
json.dump(data, file, indent=4)
if __name__ == "__main__":
try:
main()
except Exception as e:
logging.exception("")
if CONFIG.get("apprise.notify.uncaught-exception"):
APPRISE.notify(
f"{type(e).__name__}: {e}",
"⚠️ Error occurred, please check the log",
)
sys.exit(1)