-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcheck_free_games.py
223 lines (207 loc) · 7.87 KB
/
check_free_games.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
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
import requests
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
import os
import datetime
import logging
# Load environment variables from .env file
load_dotenv()
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
SMTP_SERVER = os.getenv("SMTP_SERVER")
SMTP_PORT = os.getenv("SMTP_PORT")
EMAIL = os.getenv("EMAIL") # This is your SMTP login email
PASSWORD = os.getenv("PASSWORD")
TO_EMAIL = os.getenv("TO_EMAIL")
FROM_EMAIL = os.getenv("FROM_EMAIL") # This is the email you want to appear in the "From" field
def format_date(date_string):
"""Format the date string to a more readable format."""
try:
date_obj = datetime.datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.000Z")
return date_obj.strftime("%B %d, %Y at %I:%M %p") # Example: January 09, 2025 at 04:00 PM
except ValueError:
logging.error(f"Error parsing date: {date_string}")
return date_string # Return the original string if parsing fails
def fetch_free_games():
"""Fetch free games from the Epic Games Store."""
url = "https://store-site-backend-static.ak.epicgames.com/freeGamesPromotions"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
except requests.RequestException as e:
logging.error(f"Error fetching free games: {e}")
return []
free_games = []
for game in data.get("data", {}).get("Catalog", {}).get("searchStore", {}).get("elements", []):
if game.get("promotions"):
for promo in game["promotions"].get("promotionalOffers", []):
for offer in promo.get("promotionalOffers", []):
# Check if the game is free (no price or marked as "free")
discounted_price = game.get("price", {}).get("totalPrice", {}).get("discountPrice", 0)
if discounted_price == 0: # Check if it's free
# Extracting the correct urlSlug from catalogNs.mappings
url_slug = next((mapping.get('pageSlug') for mapping in game.get('catalogNs', {}).get('mappings', []) if mapping.get('pageSlug')), None)
if url_slug: # Ensure url_slug is not None
free_games.append({
"title": game.get("title"),
"description": game.get("description", "No description available."),
"original_price": "Free",
"discounted_price": "Free",
"image_url": game.get("keyImages", [{}])[0].get("url", ""),
"url": f"https://store.epicgames.com/en-US/p/{url_slug}",
"start_date": offer.get("startDate"),
"end_date": offer.get("endDate"),
})
return free_games
def send_email(free_games):
"""Send an email with details about free games."""
if not free_games:
logging.info("No free games to notify.")
return
subject = "Free Games on Epic Games Store!"
# Modern, professional, and responsive email body with a border
body = """
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
padding: 20px;
margin: 0;
}
.container {
max-width: 600px;
margin: 0 auto;
background-color: #fff;
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border: 1px solid #ddd;
}
.header {
text-align: center;
color: #333;
}
.header h2 {
margin: 0;
font-size: 24px;
}
.content {
margin-top: 20px;
}
.game {
text-align: center;
border-bottom: 1px solid #ddd;
padding: 20px 0;
}
.game img {
border-radius: 8px;
max-width: 100%;
height: auto;
}
.game-details {
margin-top: 10px;
}
.game-details h3 {
margin: 0;
font-size: 18px;
color: #333;
}
.game-details p {
margin: 5px 0;
color: #777;
font-size: 14px;
}
.game-details .price {
font-size: 16px;
font-weight: bold;
color: #333;
}
.game-details .price span {
background-color: red;
color: white;
padding: 3px 6px;
text-decoration: none;
border-radius: 3px;
font-weight: bold;
display: inline-block;
margin-top: 10px;
}
.game-details a {
background-color: #fcb900;
color: black;
padding: 8px 16px;
text-decoration: none;
border-radius: 5px;
font-weight: bold;
display: inline-block;
margin-top: 10px;
}
.game-details .offer-end {
font-size: 14px;
color: #888;
margin-top: 20px;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h2>Free Games Available on the Epic Games Store!</h2>
<p>Don't miss out on these amazing FREE games:</p>
</div>
<div class="content">
"""
# Build the email body with the game's details, including image, description, and price
for game in free_games:
end_date = format_date(game['end_date']) # Format the end date for better readability
body += f"""
<div class="game">
<img src="{game['image_url']}" alt="{game['title']}" />
<div class="game-details">
<h3>{game['title']}</h3>
<p>{game['description']}</p>
<p class="price">Price: <span>{game['original_price']}</span></p>
<a href="{game['url']}">Claim Your Free Game Now!</a>
<p class="offer-end"><i>Offer ends: {end_date}</i></p>
</div>
</div>
"""
body += """
</div>
</div>
</body>
</html>
"""
msg = MIMEMultipart("alternative")
msg["From"] = FROM_EMAIL # Use the 'From' email address specified in .env
msg["To"] = TO_EMAIL
msg["Subject"] = subject
msg.attach(MIMEText(body, "html"))
try:
logging.info("Connecting to SMTP server...")
with smtplib.SMTP(SMTP_SERVER, int(SMTP_PORT)) as server:
server.set_debuglevel(1) # Enable debug logs
server.starttls()
logging.info("Logging in to SMTP server...")
server.login(EMAIL, PASSWORD)
logging.info("Sending email...")
server.send_message(msg)
logging.info("Email sent successfully.")
except Exception as e:
logging.error(f"Failed to send email: {e}")
def main():
"""Main function to fetch games and send notifications."""
logging.info("Fetching free games...")
free_games = fetch_free_games()
if free_games:
logging.info("Free games found! Sending notification...")
send_email(free_games)
else:
logging.info("No free games available at the moment.")
if __name__ == "__main__":
main()