Guest User

Untitled

a guest
Jun 1st, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import sys
  3. import requests import datetime
  4. from bs4 import BeautifulSoup
  5. def get_hn_items(username):
  6. url = f"https://hacker-news.firebaseio.com/v0/user/{username}.json"
  7. resp = requests.get(url) if resp.status_code != 200: sys.exit(f"Failed to fetch HN user: {resp.status_code}")
  8. return resp.json().get("submitted", []) def get_hn_item(item_id):
  9. url = f"https://hacker-news.firebaseio.com/v0/item/{item_id}.json" resp = requests.get(url) if resp.status_code != 200:
  10. return None
  11. return resp.json()
  12.  
  13. def fetch_lobsters_time(article_url):
  14. params = {"q": article_url, "what": "stories", "order": "newest"} resp = requests.get("https://lobste.rs/search", params=params)
  15. if resp.status_code != 200:
  16. return None soup = BeautifulSoup(resp.text, "html.parser") for link in soup.find_all("a", class_="u-url"): if link.get("href") != article_url:
  17. continue time_tag = link.find_next("time") if not time_tag or not time_tag.has_attr("datetime"):
  18. continue
  19. raw = time_tag["datetime"] try:
  20. lob_dt = datetime.datetime.strptime(raw, "%Y-%m-%d %H:%M:%S%z")
  21. except ValueError: continue return lob_dt.astimezone(datetime.timezone.utc).replace(tzinfo=None) return None
  22.  
  23. def main():
  24. if len(sys.argv) != 2: sys.exit("Usage: script.py <hn_username>")
  25. username = sys.argv[1]
  26. item_ids = get_hn_items(username)
  27. count_hn_earlier = 0 count_hn_after = 0
  28. time_deltas = []
  29. earliest_hn_time = None for item_id in item_ids: item = get_hn_item(item_id)
  30. if not item or item.get("type") != "story": continue url = item.get("url")
  31. if not url: continue
  32. hn_ts = item.get("time", 0) hn_time = datetime.datetime.fromtimestamp(hn_ts, datetime.timezone.utc).replace(tzinfo=None) lob_time = fetch_lobsters_time(url) if not lob_time: print(f"Processed item {item_id}: no Lobsters match") print(f"Running totals: {count_hn_earlier} HN earlier, {count_hn_after} HN after") continue delta_seconds = int((hn_time - lob_time).total_seconds()) if abs(delta_seconds) < 600:
  33. time_deltas.append(abs(delta_seconds))
  34. if delta_seconds < 0:
  35. count_hn_earlier += 1 elif delta_seconds > 0:
  36. count_hn_after += 1
  37. if earliest_hn_time is None or hn_time < earliest_hn_time:
  38. earliest_hn_time = hn_time
  39. print(f"Processed item {item_id}: within ten minutes (delta {delta_seconds} seconds)")
  40. else:
  41. print(f"Processed item {item_id}: outside ten minutes (delta {delta_seconds} seconds)") print(f"Running totals: {count_hn_earlier} HN earlier, {count_hn_after} HN after")
  42. print(f"Final: {count_hn_earlier} posts where HN is earlier within ten minutes")
  43. print(f"Final: {count_hn_after} posts where HN is after within ten minutes") if earliest_hn_time:
  44. print(f"Earliest instance within ten minutes: {earliest_hn_time.isoformat()}Z") average = sum(time_deltas) / len(time_deltas)
  45. print(f"Average time to repost: {int(average)} seconds") else: print("No reposts within ten minutes found")
  46.  
  47. if __name__ == "__main__":
  48. main()
Advertisement
Add Comment
Please, Sign In to add comment