Skip to content

Instantly share code, notes, and snippets.

@allanburleson
Last active May 2, 2023 19:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save allanburleson/037bd04bdc8a208e3a61b376cb4b1884 to your computer and use it in GitHub Desktop.
Save allanburleson/037bd04bdc8a208e3a61b376cb4b1884 to your computer and use it in GitHub Desktop.
Checks Brandon Sanderson’s website for book progress bar updates and tweets them.
import json
import os
from bs4 import BeautifulSoup
import requests
import twitter
TWITTER_CLIENT = twitter.Twitter(
auth=twitter.OAuth('REDACTED',
'REDACTED',
'REDACTED', 'REDACTED'))
def get_progress():
soup = BeautifulSoup(requests.get('http://brandonsanderson.com').text, 'html5lib')
progressdiv = soup.find('div', attrs={'class': 'vc_progress_bar'})
books = [i.text for i in progressdiv.findAll('small', attrs={'class': 'vc_label'})]
progress = {}
for i, book in enumerate(books):
s = book.split(' ')
percent = s.pop()
title = ' '.join(s)
progress[title] = percent
return progress
def print_progress(progress):
for k, v in progress.items():
print(f'{k}: {v}')
def save_progress(progress):
with open('progress.json', 'w') as f:
json.dump(progress, f)
def compare_progress(current):
previous = json.load(open('progress.json'))
if previous != current:
for title, percent in previous.items():
if title in current:
if percent != current[title]:
report(f'"{title}" has progressed from {percent} to {current[title]}.')
else:
report(f'"{title}" was removed from the progress list at {percent}.')
for title, percent in current.items():
if title not in previous:
report(f'"{title}" was added and is {percent} complete.')
return False
return True
def report(update):
print(update)
try:
TWITTER_CLIENT.statuses.update(status=update)
except twitter.api.TwitterHTTPError as err:
print(err)
def main():
os.chdir(os.path.dirname(os.path.realpath(__file__)))
progress = get_progress()
if not compare_progress(progress):
with open('progress.json', 'w') as f:
json.dump(progress, f)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment