Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import requests import datetime
- from bs4 import BeautifulSoup
- def get_hn_items(username):
- url = f"https://hacker-news.firebaseio.com/v0/user/{username}.json"
- resp = requests.get(url) if resp.status_code != 200: sys.exit(f"Failed to fetch HN user: {resp.status_code}")
- return resp.json().get("submitted", []) def get_hn_item(item_id):
- url = f"https://hacker-news.firebaseio.com/v0/item/{item_id}.json" resp = requests.get(url) if resp.status_code != 200:
- return None
- return resp.json()
- def fetch_lobsters_time(article_url):
- params = {"q": article_url, "what": "stories", "order": "newest"} resp = requests.get("https://lobste.rs/search", params=params)
- if resp.status_code != 200:
- return None soup = BeautifulSoup(resp.text, "html.parser") for link in soup.find_all("a", class_="u-url"): if link.get("href") != article_url:
- continue time_tag = link.find_next("time") if not time_tag or not time_tag.has_attr("datetime"):
- continue
- raw = time_tag["datetime"] try:
- lob_dt = datetime.datetime.strptime(raw, "%Y-%m-%d %H:%M:%S%z")
- except ValueError: continue return lob_dt.astimezone(datetime.timezone.utc).replace(tzinfo=None) return None
- def main():
- if len(sys.argv) != 2: sys.exit("Usage: script.py <hn_username>")
- username = sys.argv[1]
- item_ids = get_hn_items(username)
- count_hn_earlier = 0 count_hn_after = 0
- time_deltas = []
- earliest_hn_time = None for item_id in item_ids: item = get_hn_item(item_id)
- if not item or item.get("type") != "story": continue url = item.get("url")
- if not url: continue
- 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:
- time_deltas.append(abs(delta_seconds))
- if delta_seconds < 0:
- count_hn_earlier += 1 elif delta_seconds > 0:
- count_hn_after += 1
- if earliest_hn_time is None or hn_time < earliest_hn_time:
- earliest_hn_time = hn_time
- print(f"Processed item {item_id}: within ten minutes (delta {delta_seconds} seconds)")
- else:
- 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")
- print(f"Final: {count_hn_earlier} posts where HN is earlier within ten minutes")
- print(f"Final: {count_hn_after} posts where HN is after within ten minutes") if earliest_hn_time:
- print(f"Earliest instance within ten minutes: {earliest_hn_time.isoformat()}Z") average = sum(time_deltas) / len(time_deltas)
- print(f"Average time to repost: {int(average)} seconds") else: print("No reposts within ten minutes found")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment