Skip to content

Instantly share code, notes, and snippets.

@Lucretiel
Created October 4, 2014 01:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lucretiel/603a8a3a07e8f874e8d8 to your computer and use it in GitHub Desktop.
Save Lucretiel/603a8a3a07e8f874e8d8 to your computer and use it in GitHub Desktop.
Monitor a kickstarter
from bs4 import BeautifulSoup
from requests import get as http_get
from datetime import datetime
from time import sleep, perf_counter as now
import argparse
def get_properties(url):
'''
returns the number of backers, and total amount backed
'''
soup = BeautifulSoup(http_get(url).content)
return (
int(soup.find(itemprop='Project[backers_count]')
.text.replace(',', '')),
int(soup.find(itemprop='Project[pledged]')
.text.lstrip('$').replace(',', '')))
def generate_properties(url, sleeptime):
'''
Repeatedly yield the properties (backers, pledged) of a kickstarter. Only
yields when the properties change. Only poll the page every sleeptime
seconds
'''
previous = None
while True:
start = now()
prop = get_properties(url)
if prop != previous:
yield prop
previous = prop
sleep(max(0, sleeptime - (now() - start)))
parser = argparse.ArgumentParser(epilog='''This project lets you poll a
kickstarter project, to keep in touch with how it is doing. It polls every
''')
arg = parser.add_argument
arg('-f', '--freq', help="Frequency in seconds to poll for updates", type=float,
default=60)
arg('url', help="URL of the Kickstarter project to poll")
def main(args):
args = parser.parse_args(args[1:])
for backers, pledge in generate_properties(args.url, args.freq):
print(datetime.now().isoformat(), backers, pledge)
if __name__ == '__main__':
from sys import argv
main(argv)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment