Skip to content

Instantly share code, notes, and snippets.

@nakami
Last active July 17, 2020 02:53
Show Gist options
  • Save nakami/2feb3679e8b02faaaa1daa26ed5b765e to your computer and use it in GitHub Desktop.
Save nakami/2feb3679e8b02faaaa1daa26ed5b765e to your computer and use it in GitHub Desktop.
To what extend do brands advertise via coupons on social media? #nindo #coupons #socialmedia
import requests
import json
import time
from collections import Counter
from pprint import pprint
# avoid stressing nindo too much
# by adding a delay between fetch requests
FETCH_SLEEP = 1
url_base = "https://api.nindo.de/coupons"
url_offset = "https://api.nindo.de/coupons?offset={}"
coupon_list = []
curr_offset = 0
# fetch initial coupons
curr_json = json.loads(requests.get(url_base).content)
coupon_list.extend(curr_json.get('coupons'))
# fetch coupons while hasMore is True
while curr_json.get('hasMore'):
print('fetching more coupons...')
curr_offset += 20
curr_json = json.loads(requests.get(url_offset.format(curr_offset)).content)
coupon_list.extend(curr_json.get('coupons'))
print(f"len(coupon_list): {len(coupon_list)}")
time.sleep(FETCH_SLEEP)
# count brand names
brand_names = [coupon.get("_brand").get("name") for coupon in coupon_list]
pprint(Counter(brand_names))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment