import requests import gspread from etsy2 import Etsy import json from gspread_dataframe import get_as_dataframe, set_with_dataframe from gspread_formatting.dataframe import format_with_dataframe import pandas as pd # import schedule # import time #key allowing access to etsy api etsy = Etsy(api_key= 'j8mpgzb24pbhd5z1hax579mo') #data from api to df and filtering columns that are necassary listedItems = etsy.findAllListingActive() etsyListed = pd.DataFrame(listedItems) df1 = etsyListed[['title', 'taxonomy_path', 'price', 'currency_code', 'quantity', 'views', 'num_favorers', 'materials']] TrendingItems = etsy.getTrendingListings() etsyTrending = pd.DataFrame(TrendingItems) df2 = etsyTrending[['title', 'taxonomy_path', 'price', 'currency_code', 'quantity', 'views', 'num_favorers', 'materials']] CurrentFeaturedItems = etsy.findAllCurrentFeaturedListings() etsyFeatured = pd.DataFrame(CurrentFeaturedItems) df3 = etsyFeatured[['title', 'taxonomy_path', 'price', 'currency_code', 'quantity', 'views', 'num_favorers', 'materials']] #changing name of columns in df df1.columns = ['Title', 'Categories', 'Price', 'Currency', 'Quantity Available', 'Views', 'No. of Favourites', 'Materials Used'] df2.columns = ['Title', 'Categories', 'Price', 'Currency', 'Quantity Available', 'Views', 'No. of Favourites', 'Materials Used'] df3.columns = ['Title', 'Categories', 'Price', 'Currency', 'Quantity Available', 'Views', 'No. of Favourites', 'Materials Used']
class EtsyController(): """ Controller class for talking to the Etsy API. Will return useful results based off of need """ def __init__(self, api_key=''): self.conn = Etsy(api_key=api_key) self.limit = 25 def get_products_keywords(self, keywords=''): # Get product name, description, tags, and URL results = self.conn.findAllListingActive(keywords=keywords, limit=self.limit, includes="Images") #print(results[0].keys()) needed_elems = [ 'title', 'description', 'price', 'url', 'views', 'listing_id', 'Images' ] for i in range(0, len(results)): results[i] = dict( filter(lambda elem: elem[0] in needed_elems, results[i].items())) try: results[i]['Images'] = results[i]['Images'][0]['url_170x135'] except IndexError as e: print(results[i]['Images']) return results def get_products_tags(self, tags=[]): results = self.conn.findAllListingActive(tags=tags, limit=self.limit) needed_elems = ['title', 'description', 'price', 'url'] for i in range(0, len(results)): results[i] = dict( filter(lambda elem: elem[0] in needed_elems, results[i])) return results def get_products_images(self, pids=[]): results = [] results = list( map(lambda pid: self.conn.getImage_Listing(listing_id=pid), pids)) return results def get_product_id(self, pid=''): results = self.conn.getListing(listing_id=pid) results = json.loads(results) return results def get_url(self, pid=''): result = self.conn.getListing(listing_id=pid) return result[0][ 'url'] if result and result[0]['state'] == 'active' else '' def get_current_price(self, pid): """ Grab current price of an item from Etsy by using its listing id """ needed_elems = ['title', 'price', 'url', 'listing_id'] results = self.conn.getListing(listing_id=pid) if results[0]['state'] != 'active': return None else: return {'price': results[0]['price']}