def parse_listings(listings): if listings is not None: response = {"listings": []} for l in listings.listing: listing_json = { "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [] } for country in l.ships_to: listing_json["ships_to"].append( str(CountryCode.Name(country))) response["listings"].append(listing_json) request.setHeader('content-type', "application/json") request.write(json.dumps(response, indent=4)) request.finish() else: request.write(json.dumps({})) request.finish()
def respond(l, node): if l is not None: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [], "avatar_hash": l.avatar_hash.encode("hex"), "handle": l.handle } } for country in l.ships_to: listing_json["listing"]["ships_to"].append( str(CountryCode.Name(country))) self.transport.write( str( bleach.clean(json.dumps(listing_json, indent=4), tags=ALLOWED_TAGS)))
def handle_response(listings, node): count = 0 if listings is not None: for l in listings.listing: try: if l.contract_hash not in self.factory.outstanding_listings[message_id]: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "handle": listings.handle, "avatar_hash": listings.avatar_hash.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [] } } for country in l.ships_to: listing_json["listing"]["ships_to"].append(str(CountryCode.Name(country))) if not os.path.isfile(os.path.join( \ DATA_FOLDER, 'cache', l.thumbnail_hash.encode("hex"))): self.factory.mserver.get_image(node, l.thumbnail_hash) if not os.path.isfile(os.path.join( \ DATA_FOLDER, 'cache', listings.avatar_hash.encode("hex"))): self.factory.mserver.get_image(node, listings.avatar_hash) self.transport.write(str(bleach.clean( json.dumps(listing_json, indent=4), tags=ALLOWED_TAGS))) count += 1 self.factory.outstanding_listings[message_id].append(l.contract_hash) if count == 3: break except Exception: pass if node.id in vendors: del vendors[node.id] else: if node.id in vendors: del vendors[node.id] if node.id in self.factory.mserver.protocol.multiplexer.vendors: del self.factory.mserver.protocol.multiplexer.vendors[node.id] self.factory.db.vendors.delete_vendor(node.id.encode("hex")) if only_following: vendor_list = get_following_from_vendors(vendors) else: vendor_list = vendors.values() if len(vendor_list) > 0: shuffle(vendor_list) node_to_ask = vendor_list[0] if node_to_ask is not None: self.factory.mserver.get_listings(node_to_ask).addCallback(handle_response, node_to_ask)
def handle_response(listings, node): count = 0 if listings is not None: for l in listings.listing: if l.contract_hash not in self.factory.outstanding[ message_id]: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "handle": listings.handle, "avatar_hash": listings.avatar_hash.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [] } } for country in l.ships_to: listing_json["listing"]["ships_to"].append( str(CountryCode.Name(country))) if not os.path.isfile(DATA_FOLDER + 'cache/' + l.thumbnail_hash.encode("hex")): self.factory.mserver.get_image( node, l.thumbnail_hash) if not os.path.isfile( DATA_FOLDER + 'cache/' + listings.avatar_hash.encode("hex")): self.factory.mserver.get_image( node, listings.avatar_hash) self.sendMessage(json.dumps(listing_json, indent=4), False) count += 1 self.factory.outstanding[message_id].append( l.contract_hash) if count == 3: return count vendors.remove(node) else: VendorStore().delete_vendor(node.id) vendors.remove(node) return count
def parse_profile(profile): if profile is not None: profile_json = { "profile": { "name": profile.name, "location": str(CountryCode.Name(profile.location)), "encryption_key": profile.encryption_key.public_key.encode("hex"), "nsfw": profile.nsfw, "vendor": profile.vendor, "moderator": profile.moderator, "handle": profile.handle, "about": profile.about, "website": profile.website, "email": profile.email, "primary_color": profile.primary_color, "secondary_color": profile.secondary_color, "background_color": profile.background_color, "text_color": profile.text_color, "pgp_key": profile.pgp_key.public_key, "avatar_hash": profile.avatar_hash.encode("hex"), "header_hash": profile.header_hash.encode("hex"), "social_accounts": {} } } if "guid" in request.args: profile_json["profile"]["guid"] = request.args["guid"][0] else: profile_json["profile"][ "guid"] = self.keychain.guid.encode("hex") for account in profile.social: profile_json["profile"]["social_accounts"][str( objects.Profile.SocialAccount.SocialType.Name( account.type)).lower()] = { "username": account.username, "proof_url": account.proof_url } request.setHeader('content-type', "application/json") request.write(json.dumps(profile_json, indent=4)) request.finish() else: request.write(json.dumps({})) request.finish()
def respond(l, node): if l is not None: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [] } } for country in l.ships_to: listing_json["listing"]["ships_to"].append( str(CountryCode.Name(country))) self.sendMessage(json.dumps(listing_json, indent=4), False)
def migratev2(db): ser = db.listings.get_proto() if ser is not None: path = os.path.join(DATA_FOLDER, "listings.csv") with open(path, 'w') as csvfile: fieldnames = [ "contract_type", "pricing_currency", "language", "title", "description", "processing_time", "price", "nsfw", "image_urls", "categories", "condition", "quantity", "sku_number", "shipping_option1_name", "shipping_option1_countries", "shipping_option1_service1_name", "shipping_option1_service1_estimated_delivery", "shipping_option1_service1_estimated_price", "shipping_option2_name", "shipping_option2_countries", "shipping_option2_service1_name", "shipping_option2_service1_estimated_delivery", "shipping_option2_service1_estimated_price" ] writer = csv.DictWriter(csvfile, fieldnames=fieldnames) writer.writeheader() l = objects.Listings() l.ParseFromString(ser) for listing in l.listing: with open( db.filemap.get_file( listing.contract_hash.encode("hex")), "r") as filename: contract = json.loads(filename.read(), object_pairs_hook=OrderedDict) price = "" if listing.currency_code.lower() == "btc": price = contract["vendor_offer"]["listing"]["item"][ "price_per_unit"]["bitcoin"] price = int(price * 100000000) else: price = contract["vendor_offer"]["listing"]["item"][ "price_per_unit"]["fiat"]["price"] sku = "" if "sku" in contract["vendor_offer"]["listing"]["item"]: sku = contract["vendor_offer"]["listing"]["item"]["sku"] condition = "" if "condition" in contract["vendor_offer"]["listing"]["item"]: condition = contract["vendor_offer"]["listing"]["item"][ "condition"] category = "" if "category" in contract["vendor_offer"]["listing"]["item"]: category = contract["vendor_offer"]["listing"]["item"][ "category"] contract_type = "PHYSICAL_GOOD" if contract["vendor_offer"]["listing"]["metadata"][ "category"] == "digital good": contract_type = "DIGITAL_GOOD" row = { 'contract_type': contract_type, 'pricing_currency': listing.currency_code, 'language': 'english', 'title': listing.title, 'description': contract["vendor_offer"]["listing"]["item"]["description"], 'processing_time': contract["vendor_offer"]["listing"]["item"] ["process_time"], 'price': price, 'nsfw': str(listing.nsfw), 'image_urls': '', 'categories': category, 'condition': condition, 'quantity': '-1', 'sku_number': sku } img64 = [] for img_hash in contract["vendor_offer"]["listing"]["item"][ "image_hashes"]: image_path = db.filemap.get_file(img_hash) with open(image_path, "rb") as image_file: encoded_string = base64.b64encode(image_file.read()) img64.append(encoded_string) if len(img64) == 1: row["image_urls"] = img64[0] else: img_csv = '' r = 0 for img in img64: r += 1 img_csv += img if r != len(img64): img_csv += "," row["image_urls"] = img_csv if contract_type == "PHYSICAL_GOOD": if "free" in contract["vendor_offer"]["listing"][ "shipping"]: row["shipping_option1_name"] = "Free Shipping" countries = [] for country in listing.ships_to: countries.append(str(CountryCode.Name(country))) if len(countries) == 1: row["shipping_option1_countries"] = countries[0] else: country_csv = '' r = 0 for c in countries: r += 1 country_csv += c if r != len(countries): country_csv += "," row["shipping_option1_countries"] = country_csv row["shipping_option1_service1_name"] = "default service" ed = "standard shipping time" row["shipping_option1_service1_estimated_delivery"] = ed row["shipping_option1_service1_estimated_price"] = "0" elif "flat_fee" in contract["vendor_offer"]["listing"][ "shipping"]: cc = "bitcoin" if listing.currency_code.lower() != "btc": cc = "fiat" if "domestic" in contract["vendor_offer"]["listing"][ "shipping"]["flat_fee"][cc]["price"]: row["shipping_option1_name"] = "Domestic Shipping" row["shipping_option1_countries"] = contract[ "vendor_offer"]["listing"]["shipping"][ "shipping_origin"] row["shipping_option1_service1_name"] = "default service" ed = contract["vendor_offer"]["listing"][ "shipping"]["est_delivery"]["domestic"] if ed == "": ed = "standard shipping time" row["shipping_option1_service1_estimated_delivery"] = ed ship_price = contract["vendor_offer"]["listing"][ "shipping"]["flat_fee"][cc]["price"][ "domestic"] if cc == "bitcoin": ship_price = int(ship_price * 100000000) row["shipping_option1_service1_estimated_price"] = ship_price if "international" in contract["vendor_offer"][ "listing"]["shipping"]["flat_fee"][cc][ "price"]: row["shipping_option2_name"] = "International Shipping" countries = [] for country in listing.ships_to: countries.append(str( CountryCode.Name(country))) if len(countries) == 1: row["shipping_option2_countries"] = countries[ 0] else: country_csv = '' r = 0 for c in countries: r += 1 country_csv += c if r != len(countries): country_csv += "," row["shipping_option2_countries"] = country_csv row["shipping_option2_service1_name"] = "default service" ed = contract["vendor_offer"]["listing"][ "shipping"]["est_delivery"]["international"] if ed == "": ed = "standard shipping time" row["shipping_option2_service1_estimated_delivery"] = ed row["shipping_option2_service1_estimated_price"] = contract[ "vendor_offer"]["listing"]["shipping"][ "flat_fee"][cc]["price"]["domestic"] writer.writerow(row) return path else: raise Exception("failed to deserialize listings")
def handle_response(listings, node): count = 0 if listings is not None: for l in listings.listing: try: if l.contract_hash not in self.factory.outstanding_listings[ message_id]: listing_json = { "id": message_id, "listing": { "guid": node.id.encode("hex"), "handle": listings.handle, "avatar_hash": listings.avatar_hash.encode("hex"), "title": l.title, "contract_hash": l.contract_hash.encode("hex"), "thumbnail_hash": l.thumbnail_hash.encode("hex"), "category": l.category, "price": l.price, "currency_code": l.currency_code, "nsfw": l.nsfw, "origin": str(CountryCode.Name(l.origin)), "ships_to": [] } } for country in l.ships_to: listing_json["listing"]["ships_to"].append( str(CountryCode.Name(country))) if not os.path.isfile( DATA_FOLDER + 'cache/' + l.thumbnail_hash.encode("hex")): self.factory.mserver.get_image( node, l.thumbnail_hash) if not os.path.isfile( DATA_FOLDER + 'cache/' + listings.avatar_hash.encode("hex")): self.factory.mserver.get_image( node, listings.avatar_hash) self.transport.write( str( bleach.clean(json.dumps(listing_json, indent=4), tags=ALLOWED_TAGS))) count += 1 self.factory.outstanding_listings[ message_id].append(l.contract_hash) if count == 3: break except Exception: pass vendors.remove(node) else: vendors.remove(node) if node.id in self.factory.mserver.protocol.multiplexer.vendors: del self.factory.mserver.protocol.multiplexer.vendors[ node.id]