Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
748 lines (629 loc) · 30.1 KB
/
Copy pathmain.py
File metadata and controls
748 lines (629 loc) · 30.1 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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
"""
Daily.dev Auto Reader
=====================
Automatically reads random articles from daily.dev feed.
Skips promoted/ad articles and reads 2-4 articles per run.
Uses Playwright with cookie-based authentication.
Sends notifications via Telegram bot.
"""
import base64
import json
import os
import random
import time
import urllib.parse
import urllib.request
from datetime import datetime
from pathlib import Path
from dotenv import load_dotenv
from loguru import logger
from playwright.sync_api import sync_playwright, Page, BrowserContext
# Load environment variables
load_dotenv(Path(__file__).parent / ".env")
# ── Configuration ──────────────────────────────────────────────────────────────
BASE_URL = "https://app.daily.dev"
COOKIE_FILE = Path(__file__).parent / "user-cookie.json"
MIN_ARTICLES = 2
MAX_ARTICLES = 4
# Scroll & read timing (seconds)
SCROLL_PAUSE_MIN = 1.0
SCROLL_PAUSE_MAX = 3.0
READ_TIME_MIN = 15
READ_TIME_MAX = 40
FEED_LOAD_WAIT = 5
EXTERNAL_READ_WAIT = 20
# ── Telegram Configuration ────────────────────────────────────────────────────
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN", "")
TELEGRAM_CHAT_ID = os.getenv("TELEGRAM_CHAT_ID", "")
TELEGRAM_THREAD_ID = os.getenv("TELEGRAM_THREAD_ID", "")
TELEGRAM_ENABLED = bool(TELEGRAM_BOT_TOKEN and TELEGRAM_CHAT_ID
and TELEGRAM_BOT_TOKEN != "your_bot_token_here")
# ── Telegram Helper ───────────────────────────────────────────────────────────
def send_telegram(message: str, parse_mode: str = "HTML"):
"""Send a message via Telegram Bot API using urllib (no extra deps)."""
if not TELEGRAM_ENABLED:
logger.debug("Telegram not configured, skipping notification")
return False
try:
url = f"https://api.telegram.org/bot{TELEGRAM_BOT_TOKEN}/sendMessage"
payload = {
"chat_id": TELEGRAM_CHAT_ID,
"text": message,
"parse_mode": parse_mode,
"disable_web_page_preview": "true",
}
if TELEGRAM_THREAD_ID:
payload["message_thread_id"] = TELEGRAM_THREAD_ID
data = urllib.parse.urlencode(payload).encode("utf-8")
req = urllib.request.Request(url, data=data, method="POST")
with urllib.request.urlopen(req, timeout=10) as resp:
result = json.loads(resp.read())
if result.get("ok"):
logger.success("Telegram notification sent")
return True
else:
logger.warning(f"Telegram API error: {result}")
return False
except Exception as e:
logger.warning(f"Failed to send Telegram notification: {e}")
return False
def load_cookies(cookie_path: Path) -> list[dict]:
"""
Load cookies and convert to Playwright format.
Accepts both the browser-extension export format (expirationDate, lowercase
sameSite) and the native Playwright format (expires, Capitalized sameSite)
written back by save_cookies() after a run.
"""
logger.info(f"Loading cookies from {cookie_path}")
with open(cookie_path, "r") as f:
raw_cookies = json.load(f)
same_site_map = {
"no_restriction": "None",
"lax": "Lax",
"strict": "Strict",
"none": "None",
}
pw_cookies = []
for cookie in raw_cookies:
pw_cookie = {
"name": cookie["name"],
"value": cookie["value"],
"domain": cookie["domain"],
"path": cookie.get("path", "/"),
"secure": cookie.get("secure", False),
"httpOnly": cookie.get("httpOnly", False),
}
raw_same_site = str(cookie.get("sameSite") or "lax")
pw_cookie["sameSite"] = same_site_map.get(raw_same_site.lower(), "Lax")
# Expiry: extension export uses "expirationDate", Playwright native uses "expires"
expiry = cookie.get("expirationDate", cookie.get("expires"))
if expiry and expiry > 0:
pw_cookie["expires"] = int(expiry)
pw_cookies.append(pw_cookie)
logger.success(f"Loaded {len(pw_cookies)} cookies")
return pw_cookies
def save_cookies(context: BrowserContext, cookie_path: Path):
"""
Persist the browser's current cookie jar back to disk.
daily.dev issues a short-lived (~15min) JWT access token (da3) that gets
silently refreshed by the client using the longer-lived session cookie
(__Secure-dast) on every page load. Without this, the script would keep
reloading the same stale export forever and the session would die exactly
when the original session cookie's fixed expiry passed, regardless of how
often the refresh actually happened in the browser.
"""
try:
cookies = context.cookies()
with open(cookie_path, "w") as f:
json.dump(cookies, f, indent=2)
logger.success(f"Saved {len(cookies)} refreshed cookies to {cookie_path}")
except Exception as e:
logger.warning(f"Failed to save refreshed cookies: {e}")
def _decode_da3(context: BrowserContext) -> dict | None:
"""Decode the 'da3' access-token JWT cookie's payload, if present."""
cookies = context.cookies()
da3 = next((c for c in cookies if c["name"] == "da3"), None)
if not da3:
return None
value = urllib.parse.unquote(da3["value"])
payload_b64 = value.split(".")[1]
padded = payload_b64 + "=" * (-len(payload_b64) % 4)
return json.loads(base64.urlsafe_b64decode(padded))
def check_auth_status(context: BrowserContext) -> bool | None:
"""
Verify the session is actually authenticated by decoding the 'da3' access-token
JWT cookie and checking its expiry. Returns True if valid, False if missing or
expired (session cookie could not refresh it), None if it couldn't be parsed.
"""
try:
payload = _decode_da3(context)
if payload is None:
return False
exp = payload.get("exp")
if exp is None:
return None
return exp > time.time()
except Exception as e:
logger.debug(f"Could not verify auth status: {e}")
return None
def get_logged_in_user(context: BrowserContext, page: Page) -> dict | None:
"""
Identify which daily.dev account the session cookies belong to.
The 'userId' comes straight from the signed 'da3' JWT (authoritative, works
even if the token has since expired). 'name' is a best-effort scrape of the
profile display name from the page, for a human-readable sanity check.
"""
try:
payload = _decode_da3(context)
if payload is None:
return None
user_id = payload.get("userId")
display_name = None
try:
display_name = page.evaluate("""
() => {
const candidates = document.querySelectorAll(
'[data-testid*="profile" i], [class*="profile" i] img, ' +
'[aria-label*="profile" i], a[href*="/profile"] img, nav img[alt]'
);
for (const el of candidates) {
const alt = el.getAttribute('alt');
if (alt && alt.trim().length > 1) return alt.trim();
const aria = el.getAttribute('aria-label');
if (aria && aria.trim().length > 1) return aria.trim();
}
return null;
}
""")
except Exception:
pass
return {"userId": user_id, "name": display_name}
except Exception as e:
logger.debug(f"Could not identify logged-in user: {e}")
return None
def human_like_delay(min_sec: float = 0.5, max_sec: float = 2.0):
"""Wait for a random duration to simulate human behavior."""
delay = random.uniform(min_sec, max_sec)
time.sleep(delay)
def scroll_feed(page: Page, scroll_count: int = 5):
"""Scroll through the feed to load more articles."""
logger.info(f"Scrolling feed {scroll_count} times to load articles...")
for i in range(scroll_count):
page.mouse.wheel(0, random.randint(300, 700))
human_like_delay(SCROLL_PAUSE_MIN, SCROLL_PAUSE_MAX)
logger.debug(f"Scroll {i + 1}/{scroll_count}")
def get_reading_streak(page: Page) -> int | None:
"""
Extract the current reading streak count from the daily.dev page.
Looks for streak indicators in the sidebar/profile area.
Returns the streak number or None if not found.
"""
try:
streak = page.evaluate("""
() => {
// Strategy 1: Look for streak-related elements by text content
// daily.dev shows streak with a fire icon and number
const allElements = document.querySelectorAll('a, button, div, span, p');
for (const el of allElements) {
const text = el.textContent?.trim() || '';
const ariaLabel = el.getAttribute('aria-label') || '';
// Match patterns like "🔥 5" or "5 day streak" or aria-label with streak
if (ariaLabel.toLowerCase().includes('streak')) {
const match = ariaLabel.match(/(\\d+)/);
if (match) return parseInt(match[1]);
}
// Look for elements that contain just a number near a fire/streak icon
if (text.match(/^\\d+$/) && el.closest('[class*="streak" i], [class*="Streak"]')) {
return parseInt(text);
}
}
// Strategy 2: Look for streak in class names or data attributes
const streakEls = document.querySelectorAll(
'[class*="streak" i], [class*="Streak"], ' +
'[data-testid*="streak"], [id*="streak"], ' +
'[aria-label*="streak" i], [title*="streak" i]'
);
for (const el of streakEls) {
const text = el.textContent?.trim() || '';
const match = text.match(/(\\d+)/);
if (match) return parseInt(match[1]);
// Check aria-label too
const ariaLabel = el.getAttribute('aria-label') || '';
const ariaMatch = ariaLabel.match(/(\\d+)/);
if (ariaMatch) return parseInt(ariaMatch[1]);
}
// Strategy 3: Look for fire emoji 🔥 near a number
const body = document.body.innerHTML;
const fireMatch = body.match(/🔥\\s*(\\d+)/);
if (fireMatch) return parseInt(fireMatch[1]);
const fireMatch2 = body.match(/(\\d+)\\s*🔥/);
if (fireMatch2) return parseInt(fireMatch2[1]);
return null;
}
""")
return streak
except Exception as e:
logger.debug(f"Could not extract streak: {e}")
return None
def collect_article_links(page: Page) -> list[dict]:
"""
Collect article links from the daily.dev feed.
Filters out promoted/ad articles.
Returns list of dicts with 'title' and 'url'.
"""
logger.info("Collecting article links from feed...")
# Wait for article links to appear
try:
page.wait_for_selector('a[href*="/posts/"]', timeout=15000)
except Exception:
logger.warning("No /posts/ links found, trying to wait longer...")
time.sleep(5)
# Take debug screenshot for diagnostics
page.screenshot(path="debug_screenshot.png", full_page=False)
logger.debug("Debug screenshot saved to debug_screenshot.png")
# Collect all article card elements using JavaScript
articles = page.evaluate("""
() => {
const results = [];
const seen = new Set();
// Helper: extract a readable title from URL slug
function titleFromUrl(url) {
try {
const parts = url.split('/posts/');
if (parts.length < 2) return null;
let slug = parts[1].split('?')[0].split('#')[0];
// Remove trailing hash/id (e.g., --0wzq4dgre)
slug = slug.replace(/--[a-z0-9]+$/i, '');
// Convert hyphens to spaces and capitalize
return slug.replace(/-/g, ' ').split(' ').map(w => w.charAt(0).toUpperCase() + w.slice(1)).join(' ');
} catch { return null; }
}
// Find all links to posts
const allLinks = document.querySelectorAll('a[href*="/posts/"]');
for (const link of allLinks) {
const href = link.href;
if (!href || seen.has(href)) continue;
if (!href.includes('/posts/')) continue;
// Walk up to find the containing card/article element
const card = link.closest('article, [class*="card"], [class*="Card"], [role="listitem"]')
|| link.parentElement?.parentElement?.parentElement;
// Check if promoted/ad - look in the card context
let isPromoted = false;
if (card) {
const cardHtml = card.innerHTML || '';
// Check for "Promoted" text that appears as a label
const promotedIndicators = card.querySelectorAll('span, div, p, label');
for (const el of promotedIndicators) {
const text = el.textContent?.trim();
if (text === 'Promoted' || text === 'promoted' ||
text === 'Sponsored' || text === 'sponsored' ||
text === 'Ad' || text === 'AD') {
isPromoted = true;
break;
}
}
}
if (isPromoted) continue;
// Try multiple strategies to get the title
let title = null;
// Strategy 1: aria-label on the link itself
if (!title) {
const ariaLabel = link.getAttribute('aria-label');
if (ariaLabel && ariaLabel.length > 10) title = ariaLabel;
}
// Strategy 2: title attribute
if (!title) {
const titleAttr = link.getAttribute('title');
if (titleAttr && titleAttr.length > 10) title = titleAttr;
}
// Strategy 3: Look for heading elements inside the link
if (!title) {
const heading = link.querySelector('h1, h2, h3, h4');
if (heading && heading.textContent.trim().length > 10) {
title = heading.textContent.trim();
}
}
// Strategy 4: Look for heading in parent card
if (!title && card) {
const heading = card.querySelector('h1, h2, h3, h4');
if (heading && heading.textContent.trim().length > 10) {
title = heading.textContent.trim();
}
}
// Strategy 5: Extract title from URL slug (most reliable fallback)
if (!title) {
title = titleFromUrl(href);
}
// Final fallback
if (!title || title.length < 5) {
title = 'Article: ' + href.split('/posts/')[1]?.substring(0, 50);
}
seen.add(href);
results.push({ title: title.substring(0, 120), url: href });
}
return results;
}
""")
logger.info(f"Found {len(articles)} non-promoted articles")
return articles
def read_article(page: Page, context: BrowserContext, article: dict, index: int, total: int):
"""
Navigate to an article and simulate reading it.
Scrolls through the content with human-like timing.
If a "Read post" button exists, clicks it to open the external article.
"""
title = article["title"][:80]
url = article["url"]
logger.info(f"[{index}/{total}] Reading: {title}")
logger.debug(f"URL: {url}")
# Navigate to the article
page.goto(url, wait_until="domcontentloaded", timeout=30000)
human_like_delay(2, 4)
# Wait for the page content to load
try:
page.wait_for_load_state("networkidle", timeout=15000)
except Exception:
logger.warning("Network idle timeout, continuing anyway...")
# Simulate reading by scrolling through the article
read_time = random.randint(READ_TIME_MIN, READ_TIME_MAX)
logger.info(f"Simulating reading for ~{read_time} seconds...")
start_time = time.time()
scroll_step = 0
while time.time() - start_time < read_time:
# Scroll down a bit
scroll_amount = random.randint(100, 400)
page.mouse.wheel(0, scroll_amount)
scroll_step += 1
# Random pause between scrolls (like a human reading)
pause = random.uniform(2.0, 6.0)
time.sleep(pause)
# Occasionally scroll back up a tiny bit (human-like behavior)
if random.random() < 0.15:
page.mouse.wheel(0, -random.randint(50, 150))
time.sleep(random.uniform(0.5, 1.5))
elapsed = time.time() - start_time
logger.success(f"Finished reading '{title}' ({elapsed:.0f}s, {scroll_step} scrolls)")
# ── Try to click "Read post" to open the external article ──────────────
try:
read_post_btn = page.locator(
'a:has-text("Read post"), '
'button:has-text("Read post"), '
'a:has-text("Read Post"), '
'[class*="readPost"], '
'[data-testid*="read-post"]'
).first
if read_post_btn.is_visible(timeout=3000):
logger.info("Found 'Read post' button, clicking to open external article...")
# Listen for new page (tab) to open
with context.expect_page(timeout=15000) as new_page_info:
read_post_btn.click()
external_page = new_page_info.value
# Wait for external page to load
try:
external_page.wait_for_load_state("domcontentloaded", timeout=15000)
except Exception:
logger.warning("External page load timeout, continuing...")
external_url = external_page.url
logger.info(f"External article opened: {external_url[:100]}")
logger.info(f"Waiting {EXTERNAL_READ_WAIT}s on external article...")
# Simulate reading external page with some scrolling
ext_start = time.time()
while time.time() - ext_start < EXTERNAL_READ_WAIT:
external_page.mouse.wheel(0, random.randint(100, 300))
time.sleep(random.uniform(2.0, 5.0))
logger.success(f"Finished reading external article ({EXTERNAL_READ_WAIT}s)")
# Close the external tab
external_page.close()
logger.debug("External tab closed")
else:
logger.debug("No 'Read post' button found on this article")
except Exception as e:
logger.debug(f"No external 'Read post' available: {e}")
def run():
"""Main function to run the auto reader."""
start_time_total = time.time()
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
logger.info("=" * 60)
logger.info("Daily.dev Auto Reader - Starting")
logger.info("=" * 60)
# Determine how many articles to read (2-4)
num_articles = random.randint(MIN_ARTICLES, MAX_ARTICLES)
logger.info(f"Will read {num_articles} articles this run")
# Load cookies
if not COOKIE_FILE.exists():
logger.error(f"Cookie file not found: {COOKIE_FILE}")
logger.error("Please export your daily.dev cookies to user-cookie.json")
return
cookies = load_cookies(COOKIE_FILE)
with sync_playwright() as p:
# Launch browser
logger.info("Launching browser...")
browser = p.chromium.launch(
headless=True,
args=[
"--disable-blink-features=AutomationControlled",
"--no-sandbox",
],
)
# Create context with realistic viewport and user agent
context = browser.new_context(
viewport={"width": 1366, "height": 768},
user_agent=(
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 "
"(KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36"
),
locale="en-US",
timezone_id="Asia/Jakarta",
)
# Add cookies
context.add_cookies(cookies)
try:
# Open the daily.dev feed
page = context.new_page()
logger.info(f"Navigating to {BASE_URL}...")
page.goto(BASE_URL, wait_until="domcontentloaded", timeout=30000)
# Wait for the feed to load
logger.info(f"Waiting {FEED_LOAD_WAIT}s for feed to load...")
time.sleep(FEED_LOAD_WAIT)
try:
page.wait_for_load_state("networkidle", timeout=15000)
except Exception:
logger.warning("Network idle timeout on feed, continuing...")
# ── Verify the session actually authenticated ──────────────────
# daily.dev refreshes the short-lived 'da3' access token using the
# long-lived session cookie on every page load. If that refresh
# didn't happen, the session cookie itself is dead and no amount
# of retrying will fix it — cookies need to be re-exported.
authenticated = check_auth_status(context)
user_info = get_logged_in_user(context, page)
if user_info and user_info.get("userId"):
if user_info.get("name"):
logger.info(f"👤 Logged in as: {user_info['name']} ({user_info['userId']})")
user_line = f"👤 Login as: <b>{user_info['name']}</b> (<code>{user_info['userId']}</code>)"
else:
logger.info(f"👤 Logged in as user ID: {user_info['userId']}")
user_line = f"👤 Login as: <code>{user_info['userId']}</code>"
else:
logger.warning("👤 Could not identify logged-in user (no valid session cookie)")
user_line = "👤 Login as: <i>unknown (no valid session)</i>"
# ── Send Telegram START notification ──────────────────────────────
send_telegram(
f"🚀 <b>Daily.dev Auto Reader — Started</b>\n"
f"━━━━━━━━━━━━━━━━━━━━━━━━\n"
f"🕐 Time: <code>{now}</code>\n"
f"{user_line}\n"
f"📰 Plan: Read <b>{num_articles}</b> articles\n"
f"━━━━━━━━━━━━━━━━━━━━━━━━"
)
if authenticated is False:
logger.error("Not authenticated — session cookies are invalid or expired!")
send_telegram(
"🔒 <b>Daily.dev Auto Reader — Auth Failed</b>\n"
"━━━━━━━━━━━━━━━━━━━━━━━━\n"
"Session cookies are expired and could not be refreshed.\n"
"Please re-export cookies from your browser and update "
"<code>user-cookie.json</code>.\n"
"━━━━━━━━━━━━━━━━━━━━━━━━"
)
return
elif authenticated is None:
logger.warning("Could not verify auth status, proceeding anyway...")
else:
logger.success("Session authenticated ✅")
# ── Capture streak BEFORE reading ─────────────────────────────────
streak_before = get_reading_streak(page)
if streak_before is not None:
logger.info(f"🔥 Current reading streak: {streak_before}")
else:
logger.warning("Could not detect reading streak (will try again after reading)")
# Scroll to load more articles
scroll_feed(page, scroll_count=random.randint(3, 6))
# Collect article links
articles = collect_article_links(page)
if not articles:
logger.error("No articles found! The page structure may have changed.")
logger.info("Taking a screenshot for debugging...")
page.screenshot(path="debug_screenshot.png", full_page=False)
logger.info("Screenshot saved to debug_screenshot.png")
return
# Randomly select articles to read
num_to_read = min(num_articles, len(articles))
selected = random.sample(articles, num_to_read)
logger.info(f"Selected {num_to_read} articles to read:")
for i, art in enumerate(selected, 1):
logger.info(f" {i}. {art['title'][:70]}")
# Read each selected article
articles_read = []
for i, article in enumerate(selected, 1):
try:
read_article(page, context, article, i, num_to_read)
articles_read.append(article)
except Exception as e:
logger.error(f"Error reading article: {e}")
continue
# Pause between articles
if i < num_to_read:
pause = random.uniform(3, 8)
logger.info(f"Pausing {pause:.1f}s before next article...")
time.sleep(pause)
# ── Navigate back to feed and capture streak AFTER reading ────────
logger.info("Navigating back to feed to check updated streak...")
page.goto(BASE_URL, wait_until="domcontentloaded", timeout=30000)
time.sleep(5)
try:
page.wait_for_load_state("networkidle", timeout=15000)
except Exception:
pass
streak_after = get_reading_streak(page)
if streak_after is not None:
logger.info(f"🔥 Reading streak after: {streak_after}")
else:
logger.warning("Could not detect reading streak after reading")
# ── Final Report ──────────────────────────────────────────────────
elapsed_total = time.time() - start_time_total
elapsed_min = int(elapsed_total // 60)
elapsed_sec = int(elapsed_total % 60)
logger.info("")
logger.info("=" * 60)
logger.info("📊 DAILY.DEV AUTO READER — REPORT")
logger.info("=" * 60)
logger.info(f" 📰 Articles read: {len(articles_read)}/{num_to_read}")
for i, art in enumerate(articles_read, 1):
logger.info(f" {i}. {art['title'][:65]}")
# Build streak report text
streak_text = ""
if streak_before is not None and streak_after is not None:
logger.info(f" 🔥 Streak before: {streak_before}")
logger.info(f" 🔥 Streak after: {streak_after}")
if streak_after > streak_before:
logger.success(
f" 🎉 Streak increased! {streak_before} → {streak_after} (+{streak_after - streak_before})"
)
streak_text = f"🔥 Streak: {streak_before} → <b>{streak_after}</b> (+{streak_after - streak_before}) 🎉"
elif streak_after == streak_before:
logger.info(f" ℹ️ Streak unchanged: {streak_after} (already read today?)")
streak_text = f"🔥 Streak: <b>{streak_after}</b> (unchanged)"
else:
logger.warning(f" ⚠️ Streak changed: {streak_before} → {streak_after}")
streak_text = f"🔥 Streak: {streak_before} → <b>{streak_after}</b>"
elif streak_after is not None:
logger.info(f" 🔥 Current streak: {streak_after}")
streak_text = f"🔥 Streak: <b>{streak_after}</b>"
elif streak_before is not None:
logger.info(f" 🔥 Streak (before): {streak_before}")
streak_text = f"🔥 Streak: <b>{streak_before}</b> (before)"
else:
logger.warning(" 🔥 Streak: could not be detected")
streak_text = "🔥 Streak: <i>not detected</i>"
logger.info(f" ⏱️ Duration: {elapsed_min}m {elapsed_sec}s")
logger.info("=" * 60)
logger.success("✅ Done!")
logger.info("=" * 60)
# ── Send Telegram COMPLETION notification ─────────────────────────
articles_list = "\n".join(
f" {i}. <a href=\"{art['url']}\">{art['title'][:60]}</a>"
for i, art in enumerate(articles_read, 1)
)
if not articles_list:
articles_list = " <i>No articles read</i>"
done_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
send_telegram(
f"✅ <b>Daily.dev Auto Reader — Completed</b>\n"
f"━━━━━━━━━━━━━━━━━━━━━━━━\n"
f"🕐 Time: <code>{done_time}</code>\n"
f"⏱️ Duration: {elapsed_min}m {elapsed_sec}s\n"
f"━━━━━━━━━━━━━━━━━━━━━━━━\n"
f"📰 Articles read: <b>{len(articles_read)}/{num_to_read}</b>\n"
f"{articles_list}\n"
f"━━━━━━━━━━━━━━━━━━━━━━━━\n"
f"{streak_text}\n"
f"━━━━━━━━━━━━━━━━━━━━━━━━"
)
finally:
# Always persist the (possibly server-refreshed) session cookies,
# even if something above failed, so the next run starts from the
# freshest possible session instead of the original static export.
save_cookies(context, COOKIE_FILE)
browser.close()
if __name__ == "__main__":
run()