|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from airflow.models import BaseOperator |
| 5 | + |
| 6 | +from airflow.hooks.S3_hook import S3Hook |
| 7 | + |
| 8 | +from GoogleAnalyticsPlugin.hooks.google_analytics_hook import GoogleAnalyticsHook |
| 9 | + |
| 10 | + |
| 11 | +class GoogleAnalyticsAccountSummariesToS3Operator(BaseOperator): |
| 12 | + template_fields = ('s3_key',) |
| 13 | + |
| 14 | + def __init__(self, |
| 15 | + google_analytics_conn_id, |
| 16 | + s3_conn_id, |
| 17 | + s3_bucket, |
| 18 | + s3_key, |
| 19 | + brand, |
| 20 | + space, |
| 21 | + *args, |
| 22 | + **kwargs): |
| 23 | + super().__init__(*args, **kwargs) |
| 24 | + |
| 25 | + self.google_analytics_conn_id = google_analytics_conn_id |
| 26 | + self.s3_conn_id = s3_conn_id |
| 27 | + self.s3_bucket = s3_bucket |
| 28 | + self.s3_key = s3_key |
| 29 | + self.brand = brand |
| 30 | + self.space = space |
| 31 | + |
| 32 | + def execute(self, context): |
| 33 | + ga_conn = GoogleAnalyticsHook(self.google_analytics_conn_id) |
| 34 | + s3_conn = S3Hook(self.s3_conn_id) |
| 35 | + |
| 36 | + account_summaries = ga_conn.get_account_summaries() |
| 37 | + |
| 38 | + file_name = '/tmp/{key}.jsonl'.format(key=self.s3_key) |
| 39 | + with open(file_name, 'w') as ga_file: |
| 40 | + data = [] |
| 41 | + for item in account_summaries.get('items', []): |
| 42 | + root_data_obj = { |
| 43 | + 'account_id': item['id'], |
| 44 | + 'pgv_brand': self.brand, |
| 45 | + 'pgv_space': self.space |
| 46 | + } |
| 47 | + |
| 48 | + for web_property in item.get('webProperties', []): |
| 49 | + data_obj = {} |
| 50 | + data_obj.update(root_data_obj) |
| 51 | + |
| 52 | + data_obj['property_id'] = web_property['id'] |
| 53 | + |
| 54 | + for profile in web_property.get('profiles', []): |
| 55 | + data_obj['profile_id'] = profile['id'] |
| 56 | + data_obj['profile_name'] = profile['name'] |
| 57 | + data.append(data_obj) |
| 58 | + |
| 59 | + json_data = '\n'.join([json.dumps(d) for d in data]) |
| 60 | + ga_file.write(json_data) |
| 61 | + |
| 62 | + s3_conn.load_file(file_name, self.s3_key, self.s3_bucket, True) |
| 63 | + os.remove(file_name) |
0 commit comments