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']}
class EtsyAPI(BasePlatformAPI): persistent_identifier = "etsy" webhook_enabled = False def __init__(self, **kwargs): super().__init__(**kwargs) etsyAuthToken = ProtectedResource.objects(name="etsyAuthToken").first() etsyAuthSecret = ProtectedResource.objects( name="etsyAuthSecret").first() if etsyAuthToken is None or etsyAuthSecret is None: loginURL, temp_oauth_token_secret = EtsyOAuthHelper.get_request_url_and_token_secret( consumer_key, consumer_secret, requiredPermissions) temp_oauth_token = parse_qs( urlparse(loginURL).query).get("oauth_token").pop() productDBLogger.warn( "Etsy is not authenticated!!! Visit this URL and input the verification code to authenticate!" ) productDBLogger.warn(loginURL) productDBLogger.warn(temp_oauth_token) productDBLogger.warn(temp_oauth_token_secret) verificationCode = input("Verification Code> ") oauth_token, oauth_token_secret = EtsyOAuthHelper.get_oauth_token_via_verifier( consumer_key, consumer_secret, temp_oauth_token, temp_oauth_token_secret, verificationCode) etsyAuthToken = ProtectedResource(name="etsyAuthToken", value=oauth_token) etsyAuthSecret = ProtectedResource(name="etsyAuthSecret", value=oauth_token_secret) etsyAuthToken.save() etsyAuthSecret.save() etsyOAuthClient = EtsyOAuthClient( client_key=consumer_key, client_secret=consumer_secret, resource_owner_key=etsyAuthToken.value, resource_owner_secret=etsyAuthSecret.value) self.EtsyClient = Etsy(etsy_oauth_client=etsyOAuthClient) newEtsyListing = models.EtsyParityRecord(listingType="foo", listingID="738914494", productID="3779581207") print(newEtsyListing.getRawListingProductsJSON(self.EtsyClient)) print(newEtsyListing.pushQuantityToEtsy(10, self.EtsyClient)) # print(self._getListing("738914494")) exit() def getAllStockCounts(self): pass def _bulkFetchListings(self): finishedReading = False totalAmountOfResourcesFetched = 0 page = 1 limit = 100 fetchedResourceJSONList = list() while not finishedReading: responseJSON = self.EtsyClient.findAllShopListingsActive( shop_id=shop_id, limit=limit, page=page) totalAmountOfResourcesOnEtsy = self.EtsyClient.count totalAmountOfResourcesFetched += len(responseJSON) fetchedResourceJSONList = fetchedResourceJSONList + responseJSON if totalAmountOfResourcesOnEtsy == totalAmountOfResourcesFetched: finishedReading = True else: page += 1 finishedReading = False return fetchedResourceJSONList def _getListing(self, listing_id): responseJSON = self.EtsyClient.getListing(listing_id=listing_id) return responseJSON