Skip to content

Instantly share code, notes, and snippets.

@averymd
Created January 11, 2016 00:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save averymd/f5eb7d72af751ace221b to your computer and use it in GitHub Desktop.
Save averymd/f5eb7d72af751ace221b to your computer and use it in GitHub Desktop.
Sanitized piwik ecommerce importer
""" Main executable for the Piwik sales importer. """
from process_sales_data import SalesDataProcessor
import logging, locale
logging.basicConfig(format='%(levelname)s: %(asctime)s %(message)s',
level=logging.DEBUG)
locale.setlocale(locale.LC_ALL, 'en_US')
def main():
""" Driver function """
processor = SalesDataProcessor()
processor.run()
main()
import logging, locale, csv, re, os
from datetime import datetime
from dateutil import parser
from track_order import EcommerceTracker
class SalesDataProcessor(object):
"""docstring for SalesDataProcessor"""
csv_directory = os.path.join(os.path.dirname(__file__), 'csv')
ts_filename = 'ts-sales-data.txt'
def __init__(self):
super(SalesDataProcessor, self).__init__()
self.input_files = [f for f in os.listdir(SalesDataProcessor.csv_directory) if re.search(r'(orders_product_ossuary_[0-9]+).*\.csv$', f)]
self.tracker = EcommerceTracker()
try:
with open(SalesDataProcessor.ts_filename, 'r') as timestamp_file:
date_line = timestamp_file.read()
self.last_run = parser.parse(date_line)
logging.info('Last run was {0}'.format(self.last_run))
except Exception:
self.last_run = datetime.utcnow().replace(hour=0, minute=0, second=0, microsecond=0)
logging.exception('Unable to retrieve a timestamp.')
def get_orders(self):
""" Retrieve orders for a given time range, then send the results
to piwik. """
orders = []
try:
for order_file in self.input_files:
order_path = os.path.join(SalesDataProcessor.csv_directory, order_file)
with open(order_path, 'rb') as csvfile:
order_reader = csv.DictReader(csvfile)
for order in order_reader:
if parser.parse(order['created']) > self.last_run:
orders.append(order)
logging.info('Retrieved {0} orders from CSV file.'.format(len(orders)))
except Exception:
logging.exception('Error retrieving orders from CSV file.')
for order in orders:
if order['active']:
self.tracker.compose_request(order['gamekey'],
parser.parse(order['created']),
float(order['gross_usd']),
float(order['paymentfee_usd']),
'game-distributor1')
def run(self):
""" Main execution function. """
logging.info('Starting to process game-distributor1 orders.')
self.get_orders()
self.tracker.send_requests()
import requests, json, time, logging
from dateutil import parser
class EcommerceTracker(object):
"""docstring for EcommerceTracker"""
def __init__(self):
super(EcommerceTracker, self).__init__()
self.requests = []
self.invalidation_requests = []
self.piwik_uri = 'https://piwikinstall.com/piwik.php'
self.piwik_analytics = 'https://piwikinstall.com/index.php'
self.site_id = 1
self.url = 'http://futureproofgames.com/games/ossuary/'
self.urlref_set = {
'game-distributor1': 'http://futureproofgames.com/games/ossuary/',
'game-distributor2': 'https://www.steampowered.com'
}
self.product_sku = 'mysku'
self.token_auth = '12345abcdef'
self.invalidation_params = {
'module': 'API',
'method': 'CoreAdminHome.invalidateArchivedReports',
'idSites': 1,
'token_auth': '12345abcdef'
}
def compose_request(self, order_id, order_time, gross, fee, store):
# idgoal=0
# ec_id=order_id
# revenue=(gross-fee)
# ec_st=gross
# ec_items=['']
# token_auth=token_auth
# cdt=order_time (2011-04-05 00:11:42 format)
try:
self.requests.append({
'idgoal': 0,
'ec_id': order_id,
'revenue': str(gross - fee),
'ec_st': gross,
'cdt': order_time.strftime('%Y-%m-%d %H:%M:%S'),
'idsite': self.site_id,
'url': self.url,
'urlref': self.urlref_set[store],
'ec_items': json.dumps([[self.product_sku, 'Ossuary',
'Video Games', 10, 1]]),
"token_auth": self.token_auth,
'rec': 1
})
logging.info('Piwik request for order ID %s from %s on %s.',
order_id, store,
order_time.strftime('%Y-%m-%d %H:%M:%S'))
except Exception:
logging.exception('Unable to create piwik ecommerce request.')
try:
self.invalidation_requests.append(
dict({'dates': order_time.strftime('%Y-%m-%d')},
**self.invalidation_params)
)
logging.info('Invalidating piwik reports for %s.',
order_time.strftime('%Y-%m-%d'))
except Exception:
logging.exception('Unable to create piwik invalidation request.')
def send_requests(self):
for ecomm in self.requests:
try:
requests.get(self.piwik_uri, params=ecomm, verify=False)
time.sleep(1.5)
except Exception:
logging.exception('Logging ecommerce item to piwik failed: %s',
ecomm)
for invalidate in self.invalidation_requests:
try:
requests.get(self.piwik_analytics, params=invalidate, verify=False)
time.sleep(1.5)
except Exception:
logging.exception('Invalidating piwik report for %s failed.',
parser.parse(invalidate['dates']).strftime('%Y-%m-%d'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment