Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- from bs4 import BeautifulSoup
- from datetime import datetime
- import matplotlib.pyplot as plt
- import numpy as np
- import sys
- # Read the work id from the arguments
- work_id = sys.argv[1]
- # Stick the work id into the rest of the link
- link = f'https://archiveofourown.org/works/{work_id}/navigate'
- # Download the webpage and keep it around as html text
- html_text = requests.get(link).text;
- # Parse the html text into whatever tree thing BS uses
- soup = BeautifulSoup(html_text, 'html.parser')
- # Search the tree for all the datetime spans
- spans = soup.find_all('span', {'class': 'datetime'})
- # Grab the story title and author name
- info = soup.find_all('h2', {'class': 'heading'})[0]
- title, author = [v.text for v in info.find_all('a')]
- # Convert a list of spans to a list of datetime objects
- dates = [datetime.strptime(date.text, '(%Y-%m-%d)') for date in spans]
- # Codegolf version
- #dates = list(map(lambda d: datetime.strptime(d.text, '(%Y-%m-%d)'), BeautifulSoup(requests.get(link).text, 'html.parser').find_all('span', {'class': 'datetime'})))
- days = []
- for i in range(len(dates)-1):
- delta = (dates[i+1] - dates[i]).days
- days.append(delta)
- ax = plt.figure().gca()
- ax.yaxis.get_major_locator().set_params(integer=True)
- plt.title(f'Intervals for {title} by {author}')
- plt.xlabel('Chapters')
- coef = np.polyfit(range(0,len(days)), days, 1)
- poly1d_fn = np.poly1d(coef)
- plt.plot(days, 'k.', poly1d_fn(range(0,len(days))), '--k')
- plt.show()
Add Comment
Please, Sign In to add comment