Munashedov

Generate Plot of chapters from Ao3 work ID

Oct 14th, 2022 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import requests
  2. from bs4 import BeautifulSoup
  3. from datetime import datetime
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import sys
  7.  
  8. # Read the work id from the arguments
  9. work_id = sys.argv[1]
  10.  
  11. # Stick the work id into the rest of the link
  12. link = f'https://archiveofourown.org/works/{work_id}/navigate'
  13.  
  14. # Download the webpage and keep it around as html text
  15. html_text = requests.get(link).text;
  16.  
  17. # Parse the html text into whatever tree thing BS uses
  18. soup = BeautifulSoup(html_text, 'html.parser')
  19.  
  20. # Search the tree for all the datetime spans
  21. spans = soup.find_all('span', {'class': 'datetime'})
  22.  
  23. # Grab the story title and author name
  24. info = soup.find_all('h2', {'class': 'heading'})[0]
  25. title, author = [v.text for v in info.find_all('a')]
  26.  
  27. # Convert a list of spans to a list of datetime objects
  28. dates = [datetime.strptime(date.text, '(%Y-%m-%d)') for date in spans]
  29.  
  30. # Codegolf version
  31. #dates = list(map(lambda d: datetime.strptime(d.text, '(%Y-%m-%d)'), BeautifulSoup(requests.get(link).text, 'html.parser').find_all('span', {'class': 'datetime'})))
  32.  
  33. days = []
  34. for i in range(len(dates)-1):
  35.     delta = (dates[i+1] - dates[i]).days
  36.     days.append(delta)
  37.    
  38. ax = plt.figure().gca()
  39. ax.yaxis.get_major_locator().set_params(integer=True)
  40.  
  41. plt.title(f'Intervals for {title} by {author}')
  42. plt.xlabel('Chapters')
  43.  
  44. coef = np.polyfit(range(0,len(days)), days, 1)
  45. poly1d_fn = np.poly1d(coef)
  46. plt.plot(days, 'k.', poly1d_fn(range(0,len(days))), '--k')
  47. plt.show()
Add Comment
Please, Sign In to add comment