Created
          August 11, 2017 14:54 
        
      - 
      
- 
        Save cfc603/b2f7a58c3c059de7aaae0de5ef39a770 to your computer and use it in GitHub Desktop. 
    Use Quickbooks Inventory file and scraped data from old website to populate Shopify Import File
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import csv | |
| import re | |
| import unicodedata | |
| from unipath import Path | |
| DIR = Path.cwd() | |
| QB_FILE = Path(DIR, "from_qb.csv") | |
| WEBSITE_FILE = Path(DIR, "from_website.csv") | |
| SHOPIFY_FILE = Path(DIR, "shopify_import.csv") | |
| class QuickBooksItem(object): | |
| @classmethod | |
| def create_from_import(cls, row): | |
| instance = cls() | |
| instance.row = row | |
| return instance | |
| @property | |
| def inventory(self): | |
| if not hasattr(self, "_inventory"): | |
| self._inventory = int(self.row["Quantity On Hand"]) | |
| return self._inventory | |
| @property | |
| def sku(self): | |
| if not hasattr(self, "_sku"): | |
| self._sku = self.row["Item"] | |
| return self._sku | |
| class WebsiteItem(object): | |
| @classmethod | |
| def create_from_import(cls, row): | |
| instance = cls() | |
| instance.row = row | |
| return instance | |
| @property | |
| def dimensions(self): | |
| if not hasattr(self, "_dimensions"): | |
| self._dimensions = self.row["dimensions"] | |
| return self._dimensions | |
| @property | |
| def finish(self): | |
| if not hasattr(self, "_finish"): | |
| self._finish = self.row["finish"] | |
| return self._finish | |
| @property | |
| def item_collection(self): | |
| if not hasattr(self, "_item_collection"): | |
| self._item_collection = self.row["item_collection"] | |
| return self._item_collection | |
| @property | |
| def image_link(self): | |
| if not hasattr(self, "_image_link"): | |
| self._image_link = self.row["image_link"] | |
| return self._image_link | |
| @property | |
| def manufactured_by(self): | |
| if not hasattr(self, "_manufactured_by"): | |
| self._manufactured_by = self.row["manufactured_by"] | |
| return self._manufactured_by | |
| @property | |
| def price(self): | |
| if not hasattr(self, "_price"): | |
| self._price = float(self.row["price"]) | |
| return self._price | |
| @property | |
| def socket(self): | |
| if not hasattr(self, "_socket"): | |
| self._socket = self.row["socket"] | |
| return self._socket | |
| @property | |
| def title(self): | |
| if not hasattr(self, "_title"): | |
| self._title = "{} {}".format(self.manufactured_by, self.sku) | |
| return self._title | |
| @property | |
| def vendor_item_no(self): | |
| if not hasattr(self, "_vendor_item_no"): | |
| self._vendor_item_no = self.row["vendor_item_no"] | |
| return self._vendor_item_no | |
| class ShopifyItem(object): | |
| FIELDNAMES = [ | |
| "Handle", | |
| "Title", | |
| "Body (HTML)", | |
| "Vendor", | |
| "Type", | |
| "Tags", | |
| "Published", | |
| "Option 1 Name", | |
| "Option 1 Value", | |
| "Option 2 Name", | |
| "Option 2 Value", | |
| "Option 3 Name", | |
| "Option 3 Value", | |
| "Variant SKU", | |
| "Variant Grams", | |
| "Variant Inventory Tracker", | |
| "Variant Inventory Quantity", | |
| "Variant Inventory Policy", | |
| "Variant Fulfillment Service", | |
| "Variant Price", | |
| "Variant Compare at Price", | |
| "Variant Requires Shipping", | |
| "Variant Taxable", | |
| "Variant Barcode", | |
| "Image Src", | |
| "Image Position", | |
| "Image Alt Text", | |
| "Gift Card", | |
| "Variant Image", | |
| "Variant Weight Unit", | |
| "SEO Title", | |
| "SEO Description", | |
| "Google Shopping / Google Product Category", | |
| "Google Shopping / Gender", | |
| "Google Shopping / Age Group", | |
| "Google Shopping / MPN", | |
| "Google Shopping / Adwords Grouping", | |
| "Collection", | |
| ] | |
| item_type = "Lighting" | |
| option_1_name = "Title" | |
| option_1_value = "Default Title" | |
| option_2_name = "" | |
| option_2_value = "" | |
| option_3_name = "" | |
| option_3_value = "" | |
| variant_grams = "" | |
| variant_inventory_tracker = "shopify" | |
| variant_inventory_policy = "deny" | |
| variant_fulfillment_service = "manual" | |
| variant_compare_at_price = "" | |
| variant_requires_shipping = "TRUE" | |
| variant_taxable = "TRUE" | |
| variant_barcode = "" | |
| image_position = 1 | |
| image_alt_text = "" | |
| gift_card = "FALSE" | |
| variant_image = "" | |
| variant_weight_unit = "lb" | |
| seo_description = "" | |
| google_product_category = "" | |
| gender = "" | |
| age_group = "" | |
| mpn = "" | |
| adwords_grouping = "" | |
| collection = "Shop Lighting" | |
| @classmethod | |
| def create_from_import(cls, website_item, quickbooks_item): | |
| instance = cls() | |
| instance.website_item = website_item | |
| instance.quickbooks_item = quickbooks_item | |
| return instance | |
| @property | |
| def body(self): | |
| if not hasattr(self, "_body"): | |
| self._body = "" | |
| if self.website_item.item_collection: | |
| self._body += "<p>Item Collection: {}</p>\n".format( | |
| self.website_item.item_collection | |
| ) | |
| if self.website_item.finish: | |
| self._body += "<p>Finish: {}</p>\n".format( | |
| self.website_item.finish | |
| ) | |
| if self.website_item.dimensions: | |
| self._body += "<p>Dimensions: {}</p>\n".format( | |
| self.website_item.dimensions | |
| ) | |
| if self.website_item.socket: | |
| self._body += "<p>Socket: {}</p>".format( | |
| self.website_item.socket | |
| ) | |
| if self._body.endswith("\n"): | |
| self._body = self._body[:-1] | |
| return self._body | |
| @property | |
| def handle(self): | |
| if not hasattr(self, "_handle"): | |
| self._handle = unicodedata.normalize('NFKC', unicode(self.title)) | |
| self._handle = re.sub( | |
| r'[^\w\s-]', '', self._handle | |
| ).strip().lower() | |
| self._handle = re.sub(r'[-\s]+', '-', self._handle) | |
| return self._handle | |
| @property | |
| def image_source(self): | |
| if not hasattr(self, "_image_source"): | |
| self._image_source = self.website_item.image_link | |
| return self._image_source | |
| @property | |
| def published(self): | |
| if not hasattr(self, "_published"): | |
| if self.variant_inventory_quantity > 0: | |
| self._published = "TRUE" | |
| else: | |
| self._published = "FALSE" | |
| return self._published | |
| @property | |
| def seo_title(self): | |
| if not hasattr(self, "_seo_title"): | |
| self._seo_title = self.title | |
| return self._seo_title | |
| @property | |
| def tags(self): | |
| if not hasattr(self, "_tags"): | |
| tags = [] | |
| tag_categories = [ | |
| self.website_item.manufactured_by, | |
| self.website_item.finish, | |
| self.website_item.item_collection | |
| ] | |
| for tag_category in tag_categories: | |
| if tag_category: | |
| for tag in tag_category.split(","): | |
| tags.append(tag) | |
| self._tags = ", ".join(tags) | |
| return self._tags | |
| @property | |
| def title(self): | |
| if not hasattr(self, "_title"): | |
| self._title = "{} {}".format(self.vendor, self.variant_sku) | |
| return self._title | |
| @property | |
| def variant_inventory_quantity(self): | |
| if not hasattr(self, "_variant_inventory_quantity"): | |
| self._variant_inventory_quantity = self.quickbooks_item.inventory | |
| return self._variant_inventory_quantity | |
| @property | |
| def variant_price(self): | |
| if not hasattr(self, "_variant_price"): | |
| self._variant_price = self.website_item.price | |
| return self._variant_price | |
| @property | |
| def variant_sku(self): | |
| if not hasattr(self, "_variant_sku"): | |
| self._variant_sku = self.website_item.vendor_item_no | |
| return self._variant_sku | |
| @property | |
| def vendor(self): | |
| if not hasattr(self, "_vendor"): | |
| self._vendor = self.website_item.manufactured_by | |
| return self._vendor | |
| def to_dict(self): | |
| return { | |
| "Handle": self.handle, | |
| "Title": self.title, | |
| "Body (HTML)": self.body, | |
| "Vendor": self.vendor, | |
| "Type": self.item_type, | |
| "Tags": self.tags, | |
| "Published": self.published, | |
| "Option 1 Name": self.option_1_name, | |
| "Option 1 Value": self.option_1_value, | |
| "Option 2 Name": self.option_2_name, | |
| "Option 2 Value": self.option_2_value, | |
| "Option 3 Name": self.option_3_name, | |
| "Option 3 Value": self.option_3_value, | |
| "Variant SKU": self.variant_sku, | |
| "Variant Grams": self.variant_grams, | |
| "Variant Inventory Tracker": self.variant_inventory_tracker, | |
| "Variant Inventory Quantity": self.variant_inventory_quantity, | |
| "Variant Inventory Policy": self.variant_inventory_policy, | |
| "Variant Fulfillment Service": self.variant_fulfillment_service, | |
| "Variant Price": self.variant_price, | |
| "Variant Compare at Price": self.variant_compare_at_price, | |
| "Variant Requires Shipping": self.variant_requires_shipping, | |
| "Variant Taxable": self.variant_taxable, | |
| "Variant Barcode": self.variant_barcode, | |
| "Image Src": self.image_source, | |
| "Image Position": self.image_position, | |
| "Image Alt Text": self.image_alt_text, | |
| "Gift Card": self.gift_card, | |
| "Variant Image": self.variant_image, | |
| "Variant Weight Unit": self.variant_weight_unit, | |
| "SEO Title": self.seo_title, | |
| "SEO Description": self.seo_description, | |
| "Google Shopping / Google Product Category": self.google_product_category, | |
| "Google Shopping / Gender": self.gender, | |
| "Google Shopping / Age Group": self.age_group, | |
| "Google Shopping / MPN": self.mpn, | |
| "Google Shopping / Adwords Grouping": self.adwords_grouping, | |
| "Collection": self.collection, | |
| } | |
| with open(SHOPIFY_FILE, "w") as shopify_file: | |
| writer = csv.DictWriter(shopify_file, ShopifyItem.FIELDNAMES) | |
| writer.writeheader() | |
| with open(WEBSITE_FILE) as website_file: | |
| website_file_reader = csv.DictReader(website_file) | |
| for website_file_row in website_file_reader: | |
| website_item = WebsiteItem.create_from_import(website_file_row) | |
| with open(QB_FILE) as quickbooks_file: | |
| quickbooks_file_reader = csv.DictReader(quickbooks_file) | |
| for quickbooks_file_row in quickbooks_file_reader: | |
| quickbooks_item = QuickBooksItem.create_from_import( | |
| quickbooks_file_row | |
| ) | |
| if website_item.vendor_item_no == quickbooks_item.sku: | |
| # import pdb | |
| # pdb.set_trace() | |
| shopify_item = ShopifyItem.create_from_import( | |
| website_item, quickbooks_item | |
| ) | |
| writer.writerow(shopify_item.to_dict()) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment