def find_all_user_feedback_as_subject(user_id): """ Retrieves a set of Feedback objects associated to a User. /users/:user_id/feedback/as-subject """ feedbacks = [] url_suffix = ''.join(('/users/', user_id, '/feedback/as-subject')) url = etsy_auth.url + url_suffix payload = {'limit': '50', 'offset': ''} et.info(msg='send request to ' + url) offset = 0 while 1: payload['offset'] = str(offset) # refresh offset for pagination. r = requests.get(url, auth=etsy_auth.oauth) print(r.content, r.headers) data = json.loads(r.content.decode('utf-8')) for res in data['results']: try: user_id = res['user_id'] except KeyError: # hidden user. pass feedbacks.append(user_id) if data['pagination']['next_offset'] is None: # last page of listings. break offset += int(payload['limit']) return feedbacks
def get_circles_containing_user(user_id): """ /users/:user_id/connected_users Returns a set of users who have circled this user. usually this number is less than 100. :param user_id: login_name or id of user. :return: set. """ circled_users = set() url_suffix = ''.join(('/users/', str(user_id), '/circles')) url = etsy_auth.url + url_suffix payload = {'limit': '50', 'offset': ''} et.info(msg='send request to ' + url) offset = 0 while 1: payload['offset'] = str(offset) # refresh offset for pagination. r = requests.get(url, payload, auth=etsy_auth.oauth) data = json.loads(r.content.decode('utf-8')) for res in data['results']: try: user_id = res['user_id'] except KeyError: # hidden user. continue circled_users.add(user_id) if data['pagination']['next_offset'] is None: # last page of listings. break offset += int(payload['limit']) return circled_users
def find_all_user_favored_by(user_id): """ all users who add this user to his/her favorites. /users/:user_id/favored-by :param: user_id: user id or name. """ users = [] url_suffix = ''.join(('/users/', user_id, '/favored-by')) url = etsy_auth.url + url_suffix payload = {'limit': '50', 'offset': ''} et.info(msg='send request to ' + url) offset = 0 while 1: payload['offset'] = str(offset) # refresh offset for pagination. r = requests.get(url, payload, auth=etsy_auth.oauth) data = json.loads(r.content.decode('utf-8')) for res in data['results']: try: user_id = res['user_id'] except KeyError: # hidden user. pass users.append(user_id) if data['pagination']['next_offset'] is None: # last page of listings. break offset += int(payload['limit']) return users
def create_user_favorite_user(user_id, to_user_id): """ creates a new favorite user for a user (me) :param: user_id: user for whom create favorite user. :param: to_user_id: user who would be favorited. """ url_suffix = ''.join(('/users/', user_id, '/favorites/users/', to_user_id)) url = etsy_auth.url + url_suffix et.info(msg='send request to ' + url) requests.post(url, auth=etsy_auth.oauth)
def unconnect_user_db(user_id, to_user_id): """ add pair of connected users into db. :param user_id: :param to_user_id: """ sql = "SELECT set_user_unconnect(%s, %s)" cur = cnn.cursor() try: cur.execute(sql, (user_id, to_user_id)) cnn.commit() et.info("pair of user ({0} - {1}) unconnected".format( user_id, to_user_id)) except: cnn.rollback() et.error("error during unconnect_user_db\n" + traceback.format_exc())
def connect_user_db(user_id, to_user_id): """ add pair of connected users into db. :param user_id: :param to_user_id: """ sql = "SELECT add_connected_user(%s, %s)" cur = cnn.cursor() try: cur.execute(sql, (user_id, to_user_id)) cnn.commit() et.info("pair of user ({0} - {1}) add".format(user_id, to_user_id)) except psycopg2.IntegrityError: cnn.rollback() et.info("pair of user ({0},{1}) already exists".format( user_id, to_user_id))
def unconnect_users_of_user(user_name): """ :param: user_id: user for whom to_user_id would be unconnected, """ et.info(msg="START unconnect_users();") user_id = get_user_id_or_login_name(user_name) # retrieve users who should be unconnected. to_users_id = get_users_to_unconnect_db(user_id) for to_user_id in to_users_id: unconnect_user(user_id, to_user_id[0]) unconnect_user_db(user_id, to_user_id[0]) et.info(msg="FINISH unconnect_users();")
def circle_users_db(user_name): """ circle users. :param user_name: user, for whom users connected him should be written to db. """ et.info("START connect_users_to_db") # get id of user_name by login name. user_id = get_user_id_or_login_name(user_name) # retrieve set of users id, who connected user_name. to_users = get_circles_containing_user(user_id) for to_user_id in to_users: connect_user_db(to_user_id, user_id) # reverse order of args! et.info("FINISH connect_users_to_db")
def connect_users_db(user_name): """ connect users. :param user_name: user, whose connected user should be written to db. """ et.info("START write_connected_users_to_db") # get id of user_name by login name. user_id = get_user_id_or_login_name(user_name) # retrieve set of users, whom user_name connected. to_users_id = get_connected_users(user_id) for to_user_id in to_users_id: connect_user_db(user_id, to_user_id) et.info("FINISH write_connected_users_to_db")
def create_user_favorite_listing(user_id, listing_id): """ creates a new favorite user for a user. /users/:user_id/favorites/listings/:listing_id :param: listing_id: listing which would be favorited. :param: user_id: user who would be favorited. """ # https://openapi.etsy.com/v2/users/andreytaleyko/favorites/listings/488799483?api_key=gbhgd9nyejoj3b3x4ezz055x url_suffix = ''.join( ('/users/', user_id, '/favorites/listings/', str(listing_id))) url = etsy_auth.url + url_suffix et.info(msg='send request to ' + url) r = requests.post(url, auth=etsy_auth.oauth)
def get_users_to_unconnect_db(user_id): """ get to_users_id who didn't connect user_name. :param user_id: user, for whom users should be unconnected. """ et.info("START get_users_to_unconnect_db") cur = cnn.cursor() users = {} sql = "SELECT get_users_to_unconnect(%s)" try: cur.execute(sql, (user_id, )) users = cur.fetchall() cnn.commit() except: cnn.rollback() et.info("FINISH get_users_to_unconnect_db") return users
def get_users_left_feedback_to_shop(shop_id): """ generator function yield users who left feedback to shop. :param shop_id: name of shop :yield: set of users """ et.info(msg='START get_users_left_feedback_to_shop: ' + shop_id) # users in current shop page new_users = set() # users in all searched pages new_users_all = set() for url in gen_pages_for_shop(shop_id): et.info(msg='send request to ' + url) r = requests.get(url) print (r.reason) # page not found. if r.status_code >=400: return -1 data = r.content.decode('utf-8') try: new_users = get_users_from_xml(data) new_users_all = new_users | new_users_all except: print("get_users_from_xml. can't retrieve data from xml") continue et.info(msg='FINISH get_users_left_feedback_to_shop: ' + shop_id) return new_users_all
def was_user_connected_to_user(user_id, to_user_id): """ check if to_user_id was ever connected to user_id. :param user_id: :param to_user_id: :return boolean. """ et.info(msg="START was_user_connected_to_user();") sql = "SELECT was_user_connected_to_user(%s, %s)" cur = cnn.cursor() res = False try: cur.execute(sql, (user_id, to_user_id)) res = cur.fetchone()[0] cnn.commit() except: cnn.rollback() et.error("error during was_user_connected_to_user\n" + traceback.format_exc()) et.info(msg="FINISH was_user_connected_to_user();") return res
def find_all_listing_favored_by(listing_id): """ Retrieves a set of FavoriteListing objects associated to a Listing. NB: no pagination, so generator can't be used. /listings/:listing_id/favored-by :param listing_id: id of listing. :return: set of user who added listing to favorite. """ users = [] url_suffix = ''.join(('/listings/', listing_id, '/favored-by')) url = etsy_auth.url + url_suffix et.info(msg='send request to ' + url) r = requests.get(url, auth=etsy_auth.oauth) data = json.loads(r.content.decode('utf-8')) for res in data['results']: try: users.append(res['user_id']) except KeyError: # hidden info. pass return set(users)
def get_user_id_or_login_name(user_id): """ Retrieves a User by id. /users/:user_id :param user_id: login_name or id of user. :return: id of user, if login_name was passed, login_name, if id. """ et.info("START get_user_id_or_login_name") url_suffix = ''.join(('/users/', user_id)) url = etsy_auth.url + url_suffix et.info(msg='send request to ' + url) r = requests.get(url, auth=etsy_auth.oauth) remain = r.headers['X-RateLimit-Remaining'] et.info(msg='X-RATE-REMAINING: ' + remain) print("REMAIN:", remain) if r.status_code > 400: et.error(r.content) et.error("FINISH get_user_id_or_login_name") return False try: data = json.loads(r.content.decode('utf-8')) except: et.error(traceback.format_exc()) return False login_name = False try: int(user_id) except ValueError: login_name = True et.info("FINISH get_user_id_or_login_name") time.sleep(0.1) # only 1 result in list of results. if login_name: try: user_id = data['results'][0]['user_id'] except IndexError: user_id = 0 # anonymus return user_id else: return data['results'][0]['login_name']
def unconnect_user(user_id, to_user_id): """ /users/:user_id/circles/:to_user_id Removes a user (to_user_id) from the logged in user's (user_id) circle :param: user_id: user for whom to_user_id would be unconnected, :param: to_user_id: user, who would be unconnected from user_id. """ et.info(msg="START unconnect_user();") url_suffix = ''.join( ('/users/', str(user_id), '/circles/', str(to_user_id))) url = etsy_auth.url + url_suffix et.info(msg='send request to ' + url) r = requests.delete(url, auth=etsy_auth.oauth) # todo: bugs with utf-8 et.info(msg=''.join((str(r.status_code), r.content.decode('cp1251')))) et.info(msg="FINISH unconnect_user();")
def connect_user(user_id, to_user_id): """ /users/:user_id/connected_users Adds user (to_user_id) to the user's (user_id) circle. :param: user_id: user who connected to_user_id, :param: to_user_id: user, who would be connected to user_id. """ et.info(msg="START connect_user();") url_suffix = ''.join(('/users/', str(user_id), '/connected_users')) url = etsy_auth.url + url_suffix payload = {'to_user_id': str(to_user_id)} et.info(msg='send request to ' + url) r = requests.post(url, payload, auth=etsy_auth.oauth) # todo: bugs with utf-8 (with user names) et.info(msg=''.join((str(r.status_code), r.content.decode("utf-8")))) et.info(msg="FINISH connect_user();")
def find_all_shop_listings_active(shop_id): """ generator function, finds all active Listings associated with a Shop. pagination supported by limit-offset mechanism. yield set of listings which contained one page, number of listing yielded = limit defined in payload section. /shops/:shop_id/listings/active :param: shop_id: name or id of shop. """ listings = [] url_suffix = ''.join(('/shops/', shop_id, '/listings/active')) url = etsy_auth.url + url_suffix payload = {'limit': '50', 'offset': '0'} et.info(msg="START find_all_shop_listings_active();") offset = 0 while 1: payload['offset'] = str(offset) # refresh offset for pagination. et.info(msg=''.join(('send request to ', url, ' offset=', str(offset)))) r = requests.get(url, payload, auth=etsy_auth.oauth) data = json.loads(r.content.decode('utf-8')) for res in data['results']: # number of appended listings = limit. listings.append(res['listing_id']) if data['pagination']['next_offset'] is None: # last page of listings. et.info("next_offset is null.") break offset += int(payload['limit']) yield listings msg = ''.join(("FINISH find_all_shop_listings_active(); ", str(len(listings)), " listings found")) et.info(msg=msg)
def get_connected_users_name(user_id): """ retrieve names of connected users. /users/:user_id/connected_users :param: user_id: id of user for whom connected users're retrieved. :return: set of connected users. """ et.info(msg='START get_connected_users: ') connected_users = set() connected_user_name = set() url_suffix = ''.join(('/users/', str(user_id), '/connected_users')) url = etsy_auth.url + url_suffix payload = {'limit': '50', 'offset': ''} et.info(msg='send request to ' + url) offset = 0 while 1: payload['offset'] = str(offset) # refresh offset for pagination. r = requests.get(url, payload, auth=etsy_auth.oauth) data = json.loads(r.content.decode('utf-8')) for res in data['results']: try: connected_user_name = res['login_name'] except KeyError: # hidden user. pass connected_users.add(connected_user_name) if data['pagination']['next_offset'] is None: # last page of listings. break offset += int(payload['limit']) et.info(msg='FINISH get_connected_users: ') return connected_users
def find_all_users_favored_listing_in_shop(shop_id): """ generator function yield a set of users. pagination supported by limit-offset mechanism. yield set of listings which contained one page, number of listing yielded = limit defined in payload section. :param shop_id: id of shop. :yield: set of user who added any listing of shop to favorite. """ et.info(msg="START find_all_users_favored_listing_in_shop();") all_users = set() # traverse all active listings in shop. for listing_batch in find_all_shop_listings_active(shop_id): for listing in listing_batch: users = find_all_listing_favored_by(str(listing)) # yield only those users, who is absent in all_users set (new users). yield users - all_users all_users = all_users | users # add users to set. et.info(msg="FINISH find_all_users_favored_listing_in_shop();") msg = ''.join( (str(len(all_users)), " users liked listings in shop ", shop_id)) et.info(msg=msg)
def main(): et.info("*" * 80) #connect_all_users('Lylyspecial') connect_fresh_users('Lylyspecial')