Skip to content

Instantly share code, notes, and snippets.

@Samuel-IH
Last active January 20, 2022 17:32
Show Gist options
  • Save Samuel-IH/2e6fd60f98f8234a3c99c4683a74dc65 to your computer and use it in GitHub Desktop.
Save Samuel-IH/2e6fd60f98f8234a3c99c4683a74dc65 to your computer and use it in GitHub Desktop.
Python script for populating a Facebook store with items from Ebay
#!/path/to/python3
## About ##
## Ebay2FacebookLite created by Samuel H. 2019 ##
## Problem: Facebook does not support ebay store integration ##
## Bad Solution: manually put items in facebook store and link each item to the ebay store ##
## Better Solution: use python and ebay's APIs to automate the process and export the store
## dynamically as a product feed for facebook to use ##
## How to use on my server:
## 1. Create a link for the script, see def printHelp() below for the link specifications
## 2. Set up facebook item feed, paste in the link, and set a query frequency in facebook.
## How to use on your server:
## 1. Set this script up on a server as a cgi, it needs to be accessible from a web point for facebook to use.
## 2. Create a link for the script, see def printHelp() below for the link specifications
## 3. Set up facebook item feed, paste in the link, and set a query frequency in facebook.
###
from ebaysdk.finding import Connection
from ebaysdk.shopping import Connection as Shopping
import cgi #for input
import re #obviously for parsing
import cgitb #formatting
cgitb.enable()
#this will be printed when the user feeds us invalid input, also serves as a quick reference when viewing the cgi
def printHelp():
print("You have provided Ebay2FacebookLite with incorrect input. Please read and recorrect your input.")
print("5amproductions.net/nClaudelStore/products.cgi?s_name=VALUE&b_name=VALUE&g_c=VALUE")
print("s_name, Seller Name: The name of the seller you wish to data to facebook with.")
print("b_name, Brand Name: Facebook requires each of your items to have a brand name, this will be added to all the items.")
print("g_c, Google Category: Facebook requires your items to have a google product category, here is a list of all the possible values: https://www.google.com/basepages/producttype/taxonomy-with-ids.en-US.txt")
exit()
#name of the "file" sent to the browser, or ebay
filename = "output.csv"
#Obtained from the ebay developer portal
ebayAppID = '<REAL ID OMITTED>'
#prepare the headers for a file download
print("Content-type: application/octet-stream")
print("Content-Disposition: attachment; filename=%s" %(filename))
print()
#first line of the csv, defines all the fields that we have.. these are currently compatible with Facebook's fields
csvHeader = "ID,Availability,Brand,CONDITION,Title,IMAGE_LINK,LINK,Inventory,google_product_category,Description,PRICE,ADDITIONAL_IMAGE_LINK"
#end definitions, begin the actual work
#fetch input, will default to "" if not availble
inputData = cgi.FieldStorage()
seller_name = inputData.getvalue('s_name')
brand_name = inputData.getvalue('b_name')
google_class = inputData.getvalue('g_c')
#begin ebay Connection API, this is what allows us to query for items that a seller has
api = Connection(siteid='EBAY-US', appid=ebayAppID,config_file=None)
#begin Shopping API, this gives us the ability to fetch more detail about the items in question
itemApi = Shopping(appid=ebayAppID, config_file=None)
#ebay, will you please tell us what items seller_name is selling?
response = api.execute('findItemsAdvanced', {
'itemFilter': [
{'name': 'Seller', 'value': seller_name}
]
})
#Ebay: "no I will not tell you because you have given me incorrect input"
if response.reply.ack == "Failure":
#tell the user about it, and exit
printHelp()
#Ebay: "yes that's correct input, but that seller isn't selling anything!"
if response.reply.searchResult._count == "0":
#tell the user about it, and exit
print("No items found for the that seller!\n")
printHelp()
#assuming there were no errors above, iterate through every item that we found
for item in response.reply.searchResult.item:
if item.sellingStatus.sellingState == "Active":
#begin populating the table
csvHeader += "\n"
csvHeader += item.itemId + ",in stock,"
csvHeader += brand_name + ",new,"
csvHeader += item.title + ","
csvHeader += item.galleryURL + ","
csvHeader += item.viewItemURL + ",1,"
csvHeader += google_class + ","
itemInf = itemApi.execute('GetSingleItem', {'ItemID': item.itemId, 'IncludeSelector': 'TextDescription'})
csvHeader += '"' + re.sub('"', '""', itemInf.reply.Item.Description) + '",'
#because some bids start at $0.01, and Facebook has a minimum price of 50 cents, we need to raise the price
p = item.sellingStatus.currentPrice.value
if p == "0.01":
p = "0.51"
csvHeader += p + ',"'
#lastly, appent the urls for the pictures
for u in itemInf.reply.Item.PictureURL:
csvHeader += u + ","
if len(itemInf.reply.Item.PictureURL) > 1:
csvHeader = csvHeader[:-1]
csvHeader += '"'
#spit out the data, and we are good to go!
print(csvHeader.encode('ascii', 'ignore').decode('ascii'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment