Example #1
0
    def __init__(self, access_key, secret_key, account_id, api_data):
        self.api_data = api_data
        self.client = self.api_data.client

        self.orders_api = mws.Orders(
            access_key=os.environ.get("MWS_ACCESS_KEY"),
            secret_key=os.environ.get("MWS_SECRET_KEY"),
            account_id=os.environ['MWS_ACCOUNT_ID'],
            region="DE",
        )
        self.products_api = mws.Products(
            access_key=os.environ.get("MWS_ACCESS_KEY"),
            secret_key=os.environ.get("MWS_SECRET_KEY"),
            account_id=os.environ['MWS_ACCOUNT_ID'],
            region="DE",
        )
        self.reports_api = mws.Reports(
            access_key=os.environ.get("MWS_ACCESS_KEY"),
            secret_key=os.environ.get("MWS_SECRET_KEY"),
            account_id=os.environ['MWS_ACCOUNT_ID'],
            region="DE",
        )
        self.reports_api.get_report_request_list()
        self.orders_namespace = self.orders_api.NAMESPACE
        self.products_namespace = self.products_api.NAMESPACE
        self.reports_namespace = "{http://mws.amazonaws.com/doc/2009-01-01/}"
        self.orders = []
        super().__init__()
Example #2
0
def get_reports_api(merchant_info):
    reports_api = mws.Reports(access_key=config['access_key'],
                              secret_key=config['secret_key'],
                              account_id=merchant_info['merchant_id'],
                              region=merchant_info['market_place'],
                              auth_token=merchant_info['auth_token'])
    return reports_api
Example #3
0
 def __init__(self, account):
     self.reports = []
     self.report = None
     self.account = account
     self.reportClient = mws.Reports(account_id=account['merchant_id'],
                                     access_key=account['access_key'],
                                     secret_key=account['secret_key'],
                                     region=account['marketplace'],
                                     auth_token=account['mws_access_token'])
def fetch_report():
    print('Triggering report ', datetime.datetime.now())
    mapping = get_mapping()
    for r in mapping:
        try:
            orders_api = mws.Reports(
                access_key='AKIAI5SXHL3KQ5TMYPQQ',
                secret_key='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',
                account_id='A3TUGG788NOEF7',
                region='UK')
            orders_api.request_report('_GET_MERCHANT_LISTINGS_ALL_DATA_')
        except Exception as e:
            import traceback
            traceback.print_exc()
            pass
Example #5
0
def connection_test():
    config = configparser.ConfigParser()
    config.read('../../account.ini')
    manne_conf = config['MANNE']

    access_key = manne_conf['aws_access_key_id']
    secret_key = manne_conf['secret_key']
    seller_id = manne_conf['seller_id']
    mws_auth_token = manne_conf['mws_auth_token']
    marketplace_de = 'A1PA6795UKMFR9'

    # mws test
    reports_api = mws.Reports(access_key,
                              secret_key,
                              seller_id,
                              auth_token=mws_auth_token,
                              region='DE')
    report_list = reports_api.get_report_list()

    print('amazon geht: ' + str(report_list.response.status_code == 200))
Example #6
0
config = configparser.ConfigParser()
config.read('../../account.ini')
manne_conf = config['MANNE']

access_key = manne_conf['aws_access_key_id']
seller_id = manne_conf['seller_id']
secret_key = manne_conf['secret_key']
mws_auth_token = manne_conf['mws_auth_token']
marketplace_de = 'A1PA6795UKMFR9'

# mws test
products_api = mws.Products(access_key, secret_key, seller_id, region='DE')
inventory_api = mws.Inventory(access_key, secret_key, seller_id, region='DE')
reports_api = mws.Reports(access_key,
                          secret_key,
                          seller_id,
                          auth_token=mws_auth_token,
                          region='DE')
# products = products_api.list_matching_products(marketplaceid=marketplace_de, query='tv')
# print('amazon geht: ' + str(products.response.status_code == 200))

#
# # bigbuy test
# headers = {'Authorization': 'Bearer ' + manne_conf['bigbuy_api_key']}
# test_url = '/rest/catalog/categories.json?isoCode=de&_format=json'
# url = '/rest/shipping/carriers.json?_format=json'
# production_api = 'https://api.bigbuy.eu'
# sandbox_api = 'https://api.sandbox.bigbuy.eu'
# # r = requests.get(production_api + url, headers=headers)
# # print('bigbuy geht: ' + str(r.status_code == 200))
#
def get_products(region, marketplace, pageNum, prodname):
    global cached_records
    print(region, marketplace)
    fetch_report()
    orders_api = mws.Reports(
        access_key='AKIAI5SXHL3KQ5TMYPQQ',
        secret_key='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',
        account_id='A3TUGG788NOEF7',
        region='UK')
    products_api = mws.Products(
        access_key='AKIAI5SXHL3KQ5TMYPQQ',
        secret_key='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',
        account_id='A3TUGG788NOEF7',
        region=region)
    for i in (orders_api.get_report_request_list(
            types=['_GET_MERCHANT_LISTINGS_ALL_DATA_'
                   ]).parsed['ReportRequestInfo']):
        if (i['ReportType']['value'] == '_GET_MERCHANT_LISTINGS_ALL_DATA_'
            ) and 'GeneratedReportId' in i:
            report_id = i['GeneratedReportId']['value']
            break
    report_lines = ((
        orders_api.get_report(report_id).parsed).decode('latin1').split('\n'))
    columns = {}
    for idx, column in enumerate(report_lines[0].strip().split('\t')):
        columns[idx] = column.strip().replace('\ufeff', '')
    records = []
    if prodname == '' or prodname == 'undefined':
        for i in report_lines[1:]:
            record = {}
            if i:
                for idx, j in enumerate(i.split('\t')):
                    record[columns[idx]] = j.strip().replace('\ufeff', '')
                record['SmallImage'] = products_api.get_matching_product(
                    marketplace,
                    [record['asin1']]).parsed['Product']['AttributeSets'][
                        'ItemAttributes']['SmallImage']['URL']['value']
                record['handle'] = ''.join(
                    ([c for c in record['item-name'].lower() if c.isalnum()]))

                record['in-shopify'] = bool(get_prod_id(record))
                data = get_prod(record)
                if data and '|' in data['title']:
                    record['restock-date'] = data['title'][data['title'].
                                                           rfind(' ') + 1:]
                records += [record]
    else:
        for i in report_lines[1:]:
            print(i)
            record = {}
            if i and prodname in i:
                for idx, j in enumerate(i.split('\t')):
                    record[columns[idx]] = j.strip().replace('\ufeff', '')
                record['SmallImage'] = products_api.get_matching_product(
                    marketplace,
                    [record['asin1']]).parsed['Product']['AttributeSets'][
                        'ItemAttributes']['SmallImage']['URL']['value']
                record['handle'] = ''.join(
                    ([c for c in record['item-name'].lower() if c.isalnum()]))
                record['in-shopify'] = bool(get_prod_id(record))
                data = get_prod(record)
                if data and '|' in data['title']:
                    record['restock-date'] = data['title'][data['title'].
                                                           rfind(' ') + 1:]
                records += [record]
    cached_records = records
    afile = open('records', 'wb')
    pickle.dump(records, afile)
    afile.close()
    return records
def getYesterdaysData():

    mydb = s.s1()

    # Consumer keys and access tokens, used for OAuth, get these from our amazon account
    accessKey = mydb.access_key
    secretKey = mydb.secret_key
    merchantId = 'A2AU8QYORH0MX'

    #create client for amazons mws api

    #http://docs.developer.amazonservices.com/en_US/dev_guide/index.html

    reportClient = mws.Reports(
        access_key=accessKey,
        secret_key=secretKey,
        account_id=merchantId,
    )

    #find id of the report you need
    #amazon auto generates a capture report everyday (if there is data) so we will use that

    #get report list, defaults to last 90 days
    reportList = reportClient.get_report_list()

    notFound = True
    reportId = 0

    #go through the list of reports and find the id of the capture report from yesterday
    while (notFound == True) and (reportList.parsed['HasNext']['value']
                                  == 'true'):

        for x in reportList.parsed['ReportInfo']:
            if x['ReportType'][
                    'value'] == '_GET_FLAT_FILE_OFFAMAZONPAYMENTS_CAPTURE_DATA_':
                # make sure the report is from yesterday
                yesterday = date.today() - timedelta(1)
                if (str(yesterday) == x['AvailableDate']['value'][0:10]):
                    #capture report from yesterday found, record its id and stop looking
                    reportId = x['ReportId']['value']
                    notFound = False

        #get next report
        reportList = reportClient.get_report_list(
            next_token=reportList.parsed['NextToken']['value'])

    #request report and parse report into dataframe

    #if a report was found
    if (reportId != 0):

        #request report
        report = reportClient.get_report(report_id=reportId)

        #parse report to the dataframe rp
        streport = str(report.parsed, 'utf-8')
        text_file = open("t.txt", "w")
        text_file.write(streport)
        text_file.close()

        lnames = [
            "AmazonCaptureId", "CaptureReferenceId", "AmazonAuthorizationId",
            "AmazonOrderReferenceId", "CaptureAmount", "CurrencyCode",
            "SellerCaptureNote", "CaptureStatus", "LastUpdateTimestamp",
            "ReasonCode", "CreationTimestamp", "BuyerName",
            "BuyerEmailAddress", "BillingAddressLine1", "BillingAddressLine2",
            "BillingAddressLine3", "BillingAddressCity",
            "BillingAddressDistrictOrCounty", "BillingAddressStateOrRegion",
            "BillingAddressPostalCode", "BillingAddressCountryCode"
        ]
        rp = pd.read_csv("t.txt", sep=',', skiprows=(1), names=lnames)

        #add suggested station based on zipcode from our endpoint
        potStation = []
        for z in rp['BillingAddressPostalCode']:
            station = npr.Stations(z[0:5])
            potStation.append(station.name)

        #add stations to dataframe
        rp['PotentialStation'] = potStation

        # https://pay.amazon.com/us/developer/documentation/apireference/201751630

        #make sure types are all correct

        rp.AmazonCaptureId = rp.AmazonCaptureId.astype(str)
        rp.CaptureReferenceId = rp.CaptureReferenceId.astype(str)
        rp.AmazonAuthorizationId = rp.AmazonAuthorizationId.astype(str)
        rp.AmazonOrderReferenceId = rp.AmazonOrderReferenceId.astype(str)
        rp.CaptureAmount = rp.CaptureAmount.astype(float)
        rp.CurrencyCode = rp.CurrencyCode.astype(str)
        rp.SellerCaptureNote = rp.SellerCaptureNote.astype(str)
        rp.CaptureStatus = rp.CaptureStatus.astype(str)
        rp.LastUpdateTimestamp = pd.to_datetime(rp['LastUpdateTimestamp'],
                                                format='%Y-%m-%d %H:%M:%S',
                                                errors='coerce')
        rp.ReasonCode = rp.ReasonCode.astype(str)
        rp.CreationTimestamp = pd.to_datetime(rp['CreationTimestamp'],
                                              format='%Y-%m-%d %H:%M:%S',
                                              errors='coerce')
        rp.SellerCaptureNote = rp.SellerCaptureNote.astype(str)
        rp.BuyerName = rp.BuyerName.astype(str)
        rp.BuyerEmailAddress = rp.BuyerEmailAddress.astype(str)
        rp.BillingAddressLine1 = rp.BillingAddressLine1.astype(str)
        rp.BillingAddressLine2 = rp.BillingAddressLine2.astype(str)
        rp.BillingAddressLine3 = rp.BillingAddressLine3.astype(str)
        rp.BillingAddressCity = rp.BillingAddressCity.astype(str)
        rp.BillingAddressDistrictOrCounty = rp.BillingAddressDistrictOrCounty.astype(
            str)
        rp.BillingAddressStateOrRegion = rp.BillingAddressStateOrRegion.astype(
            str)
        rp.BillingAddressPostalCode = rp.BillingAddressPostalCode.astype(str)
        rp.BillingAddressCountryCode = rp.BillingAddressCountryCode.astype(str)
        rp.PotentialStation = rp.PotentialStation.astype(str)

        #push update to bigquery
        projectId = "reportsserviceproduction"
        datasetId = "PodcastActions"
        tableId = "alexa_donations"

        # Instantiates a client
        bigquery_client = bigquery.client.Client(projectId)
        dataset_ref = bigquery_client.dataset(datasetId)
        table_ref = dataset_ref.table(tableId)

        #create big query job configuration
        job_config = bigquery.LoadJobConfig()
        job_config.source_format = 'Parquet'
        job_config.schema = [
            bigquery.SchemaField(
                'AmazonAuthorizationId',
                'STRING',
                description=
                "Unique id created during this donations authorization event in the amazon api"
            ),
            bigquery.SchemaField(
                'AmazonCaptureId',
                'STRING',
                description=
                "Unique id created during this donations capture event in the amazon api"
            ),
            bigquery.SchemaField(
                'AmazonOrderReferenceId',
                'STRING',
                description=
                "Unique id for this order (A donation is still an order in Amazons system)"
            ),
            bigquery.SchemaField(
                'BillingAddressCity',
                'STRING',
                description="City for this donations billing address"),
            bigquery.SchemaField(
                'BillingAddressCountryCode',
                'STRING',
                description="Country code for this donations billing address"),
            bigquery.SchemaField(
                'BillingAddressDistrictOrCounty',
                'STRING',
                description=
                "District or county for this donations billing address (normally nan)"
            ),
            bigquery.SchemaField(
                'BillingAddressLine1',
                'STRING',
                description="Address line 1 for this donations billing address"
            ),
            bigquery.SchemaField(
                'BillingAddressLine2',
                'STRING',
                description=
                "Address line 2 for this donations billing address (normally nan)"
            ),
            bigquery.SchemaField(
                'BillingAddressLine3',
                'STRING',
                description=
                "Address line 3 for this donations billing address (normally nan)"
            ),
            bigquery.SchemaField(
                'BillingAddressPostalCode',
                'STRING',
                description="Postal code for this donations billing address"),
            bigquery.SchemaField(
                'BuyerName',
                'STRING',
                description="Name of the user whos account donated"),
            bigquery.SchemaField('CaptureAmount',
                                 'FLOAT',
                                 description="Amount donated"),
            bigquery.SchemaField(
                'CaptureReferenceId',
                'STRING',
                description="The id for this capture transaction"),
            bigquery.SchemaField(
                'CaptureStatus',
                'STRING',
                description="Type of case (almost always an email)"),
            bigquery.SchemaField(
                'CreationTimestamp',
                'TIMESTAMP',
                description="Timestamp of when donation was made"),
            bigquery.SchemaField('CurrencyCode',
                                 'STRING',
                                 description="Currency donation was made in"),
            bigquery.SchemaField(
                'LastUpdateTimestamp',
                'TIMESTAMP',
                description="Timestamp of the last time the event was updated"
            ),
            bigquery.SchemaField(
                'ReasonCode',
                'STRING',
                description="Reason for donation (will probably always be nan)"
            ),
            bigquery.SchemaField(
                'SellerCaptureNote',
                'STRING',
                description=
                "Note left by seller during capture event (we do not currently have one, will probably always be nan)"
            ),
            bigquery.SchemaField(
                'PotentialStation',
                'STRING',
                description=
                "Potential station of donation generated using the npr api with the postal code"
            )
        ]

        #create bigquery job
        job = bigquery_client.load_table_from_dataframe(rp,
                                                        table_ref,
                                                        job_config=job_config)

        # Waits for job to complete
        while True:
            try:
                job.result()
            finally:
                if (job.errors != 'None'):
                    print(job.errors)
                if (job.error_result != 'None'):
                    print(job.error_result)
            if job.state == 'DONE':
                if job.error_result:
                    raise RuntimeError(job.errors)

                print('Loaded {} rows into {}:{}.'.format(
                    job.output_rows, datasetId, tableId))
                break
            time.sleep(1)
    else:
        #no capture report was found for yesterday meaning np donations were made
        print("No donations yesterday")
Example #9
0
    # if not dynamodb:
    # dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")

    table = dynamodb.Table('merchants')
    params = {'Select': "ALL_ATTRIBUTES"}
    response = table.scan(**params)
    # print(response)
    # if response['Count'] > 0:
    #     return response['Items']

    return response


reports_api = mws.Reports(access_key=config['access_key'],
                          secret_key=config['secret_key'],
                          account_id=config['merchant_id'],
                          region='US',
                          auth_token=config['mws_auth_token'])


def get_reports_api(merchant_info):
    reports_api = mws.Reports(access_key=config['access_key'],
                              secret_key=config['secret_key'],
                              account_id=merchant_info['merchant_id'],
                              region=merchant_info['market_place'],
                              auth_token=merchant_info['auth_token'])
    return reports_api


def get_report_id(report_type_str):
 def setUp(self):
     self.api = mws.Reports(self.CREDENTIAL_ACCESS,
                            self.CREDENTIAL_SECRET,
                            self.CREDENTIAL_ACCOUNT,
                            auth_token=self.CREDENTIAL_TOKEN)
     self.api._test_request_params = True
Example #11
0
region = 'DE'

#initialising
bigbuy_url = 'https://api.bigbuy.eu'
bb_header = {'Authorization': 'Bearer ' + bigbuy_api_key}
feed_api = mws.Feeds(access_key=access_key,
                     secret_key=secret_key,
                     account_id=account_id,
                     auth_token=auth_token)
products_api = mws.Products(access_key=access_key,
                            secret_key=secret_key,
                            account_id=account_id,
                            auth_token=auth_token)
reports_api = mws.Reports(access_key=access_key,
                          secret_key=secret_key,
                          account_id=account_id,
                          auth_token=auth_token,
                          region=region)


def get_sku_list():
    #changed report_type
    report_request = reports_api.request_report(
        report_type='_GET_MERCHANT_LISTINGS_ALL_DATA_',
        marketplaceids=de_marketplace)
    if report_request.response.status_code == 200:
        starting_date = datetime.datetime.now() - datetime.timedelta(hours=6)
        sleep(60)
        report_list = reports_api.get_report_list(fromdate=starting_date)
        newest_report_meta_list = list(
            filter(