Skip to content

Instantly share code, notes, and snippets.

@GokulNC
Created June 1, 2020 11:07
Show Gist options
  • Save GokulNC/7520ee435d2be9b9f109e68dcace668a to your computer and use it in GitHub Desktop.
Save GokulNC/7520ee435d2be9b9f109e68dcace668a to your computer and use it in GitHub Desktop.
A script I wrote to search MyPustak for books
import json
import requests
import sys
from tabulate import tabulate
USAGE = 'python %s <search_query> <num_pages>' % sys.argv[0]
POST_URL = 'https://data.mypustak.com//search/get/SearchBooksCollection/%s/%d'
payload = {"query_by": "title,author,publication,isbn", "stock_filter_by":["N"], "bookType_filter_by":[0]}
def get_book_list(response):
hits = response['data']['hits']
books = []
for hit in hits:
d = hit['document']
details = (d['title'], d['author'])#, d['slug'])
books.append(details)
return books
def get_results(query, page=1):
response = requests.post(POST_URL % (query, page), data=payload)
result = json.loads(response.text)
return result if result['status'] == 200 else None
if __name__ == '__main__':
query, pages = sys.argv[1:]
if not query:
sys.exit(USAGE)
pages = 1 if not pages else int(pages)
books = []
for page in range(1, pages+1):
result = get_results(query, page)
if not result:
print("Page %d failed to fetch" % page)
continue
books.extend(get_book_list(result))
print('TOTAL RESULTS: %d' % result['data']['found'])
print('PRINT RESULTS: %d' % len(books))
print(tabulate(books))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment