import argparse
from collections import Counter
import re
import requests
from requests_oauthlib import OAuth1

API_KEY = "4wl4rm60yyv3b3vouqrpgmwn"
IGNORE = set(["quot"])
LISTINGS_PER_REQUEST = 100

def get_shops():
	with open("resources/shops.txt", "r") as f:
		return [s.strip() for s in f.readlines()]

def get_stopwords():
	with open("resources/stopwords.txt", "r") as f:
		return set([s.strip() for s in f.readlines()])

def build_etsy_uri(resource, params=None):
	uri = "https://openapi.etsy.com/v2/" + resource
	if params:
		uri += "?"
		for key,val in params.iteritems():
			uri += "&" + str(key) + "=" + str(val)
	return uri

def fetch_listings_by_shop_id(shop_id, api_key):
	all_listings = []
	next_page = 1
	while next_page:
		params = {
			'api_key': api_key,
			'limit': LISTINGS_PER_REQUEST, 
			'page': next_page}
		uri = build_etsy_uri("shops/" + shop_id + "/listings/active", params)
		result = requests.get(uri)
		print "GET", uri, ": status", result.status_code
		if result.status_code == 200:
			parsed = result.json()
			all_listings.extend(parsed["results"])
			next_page = parsed["pagination"]["next_page"]
		else:
			print "GET failed. Trying next shop."
	return all_listings

def valid_term(term, stopwords):
	return term not in IGNORE and term not in stopwords and not term.isdigit()

def get_terms(string, stopwords):
	words = re.findall(r'\w+', string.lower())
	return [word for word in words if valid_term(word, stopwords)]

def top_n(listings, n, stopwords):
	counter = Counter()
	for listing in listings:
		if "title" in listing:
			for term in get_terms(listing["title"], stopwords):
				counter[term] += 1
		if "description" in listing:
			for term in get_terms(listing["description"], stopwords):
				counter[term] += 1
	return counter.most_common(n)

def get_shops_to_top_n(shops, api_key, n, remove_stopwords):
	stopwords = set()
	if remove_stopwords:
		stopwords = get_stopwords()
	return {shop_id: top_n(fetch_listings_by_shop_id(shop_id, api_key), n, stopwords) for shop_id in shops}

def output(shop_id_to_top_n):
	for shop_id in shop_id_to_top_n:
		print shop_id, shop_id_to_top_n[shop_id]

def build_parser():
	parser = argparse.ArgumentParser(description='Get top words on Etsy')
	parser.add_argument('--remove-stopwords', action='store_true', default=False, help="Remove Stopwords?")
	parser.add_argument('--num', metavar='N', type=int, default=5, help="Top N to return")
	parser.add_argument('--api-key', default=API_KEY, help="Your Etsy API key")
	return parser

def run_most_common():
	parser = build_parser()
	args = parser.parse_args()
	shops = get_shops()
	shop_id_to_top_n = get_shops_to_top_n(shops, args.api_key, args.num, args.remove_stopwords)
	output(shop_id_to_top_n)

if __name__ == "__main__":
	run_most_common()

	