Your Name 3 年 前
コミット
b61b5529a7

+ 147 - 0
hhh/GATest_customer.py

@@ -0,0 +1,147 @@
+"""Hello Analytics Reporting API V4."""
+#!/usr/bin/python3
+import sys
+import codecs
+import traceback
+import requests
+import re
+import pandas as pd
+import random
+import urllib
+import dataset
+import json
+import gspread
+import datetime
+from gspread_pandas import Spread, Client
+from oauth2client.service_account import ServiceAccountCredentials
+import os
+import threading
+from apiclient.discovery import build
+from oauth2client.service_account import ServiceAccountCredentials
+import dataset
+
+db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
+#db.query('delete from hhh_tmp_customer')
+#db.query('delete from hhh_weekly_affinity')
+
+table=db['hhh_tmp_customer']
+
+SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
+KEY_FILE_LOCATION = 'c:\\keys\\choozmo-ga-beee24b7a4c1.json'
+VIEW_ID = '188916214'
+
+
+def initialize_analyticsreporting():
+  """Initializes an Analytics Reporting API V4 service object.
+
+  Returns:
+    An authorized Analytics Reporting API V4 service object.
+  """
+  credentials = ServiceAccountCredentials.from_json_keyfile_name(
+      KEY_FILE_LOCATION, SCOPES)
+
+  # Build the service object.
+  analytics = build('analyticsreporting', 'v4', credentials=credentials)
+
+  return analytics
+
+
+def get_report(analytics,body):
+  """Queries the Analytics Reporting API V4.
+
+  Args:
+    analytics: An authorized Analytics Reporting API V4 service object.
+  Returns:
+    The Analytics Reporting API V4 response.
+  """
+  return analytics.reports().batchGet(
+      body={
+        'reportRequests':body
+        # [
+        #{
+#          'viewId': VIEW_ID,
+#          'dateRanges': [{'startDate': '14daysAgo', 'endDate': 'today'}],
+#          'dateRanges': [{'startDate': '2021-05-30', 'endDate': '2021-06-05'}],
+
+#          'metrics': [{'expression': 'ga:users'}],
+#          'dimensions': [{'name': 'ga:sourceMedium'}]
+#          'dimensions': [{'name': 'ga:date'},{'name': 'ga:sourceMedium'}]
+
+#        }]
+      }
+  ).execute()
+
+
+def print_response(response):
+  """Parses and prints the Analytics Reporting API V4 response.
+
+  Args:
+    response: An Analytics Reporting API V4 response.
+  """
+  result=[]
+  for report in response.get('reports', []):
+    columnHeader = report.get('columnHeader', {})
+    dimensionHeaders = columnHeader.get('dimensions', [])
+    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
+
+    for row in report.get('data', {}).get('rows', []):
+      dimensions = row.get('dimensions', [])
+      dateRangeValues = row.get('metrics', [])
+      ga_dict={}
+
+      for header, dimension in zip(dimensionHeaders, dimensions):
+#        print(header + ': ', dimension)
+        ga_dict[header]=dimension
+      
+      for i, values in enumerate(dateRangeValues):
+#        print('Date range:', str(i))
+        for metricHeader, value in zip(metricHeaders, values.get('values')):
+          ga_dict[metricHeader.get('name')]=value
+#          print(metricHeader.get('name') + ':', value)
+        result.append(ga_dict)
+    return result
+#        print(ga_dict)
+#product-post.php?id=2381
+
+q=''
+#for i in range (2367,2382):
+#for i in range (2352,2368):
+#for i in range (2352,2363):
+for i in range (2363,2368):
+#  if i==2381:
+  if i==2367:
+#  if i==2362:
+
+    q+='id='+str(i)
+  else:
+    q+='id='+str(i)+"|"
+
+print(q)
+#sys.exit()
+
+def main():
+  analytics = initialize_analyticsreporting()
+
+  body=[{ 'viewId': VIEW_ID,
+  'dateRanges': [{'startDate': '2021-01-01', 'endDate': '2021-06-10'}],
+  'filtersExpression': 'ga:pagePath=~('+q+')',
+  'metrics': [{'expression': 'ga:pageviews'}],
+  'dimensions': [{'name': 'ga:date'},{'name': 'ga:pagePath'}]
+  }]
+
+  response = get_report(analytics,body)
+
+
+  ga_dict=print_response(response)
+#  print(ga_dict)
+  for elmt in ga_dict:
+    if 'product-post' in elmt ['ga:pagePath']:
+      table.insert(elmt)
+      print(elmt)
+
+#        print(elmt['ga:sourceMedium'])
+
+
+if __name__ == '__main__':
+  main()
+

+ 49 - 0
hhh/GA_DB_pagetitle_to_sheet.py

@@ -0,0 +1,49 @@
+#!/usr/bin/python3
+import sys
+import codecs
+import traceback
+import requests
+import re
+import pandas as pd
+import random
+import urllib
+import dataset
+import json
+import gspread
+import datetime
+from gspread_pandas import Spread, Client
+from oauth2client.service_account import ServiceAccountCredentials
+import os
+import threading
+
+def save_sheet(df,filename,tabname,startpos='A1'):
+
+    scope = ['https://spreadsheets.google.com/feeds',
+            'https://www.googleapis.com/auth/drive']
+
+    credentials = ServiceAccountCredentials.from_json_keyfile_name('c:\\keys\\spread2.json', scope)
+#    credentials = ServiceAccountCredentials.from_json_keyfile_name('/var/keys/spread2.json', scope)
+
+    gc = gspread.authorize(credentials)
+    spread = Spread(filename,creds=credentials)
+
+    spread.df_to_sheet(df, index=False, sheet=tabname, start=startpos, replace=False)
+
+
+def do_jobs():
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
+    cursor=db.query("SELECT p1.`ga:pageTitle` as title ,p1.`ga:users` as prev_week ,p2.`ga:users` as cur_week, (p2.`ga:users`-p1.`ga:users`)/p1.`ga:users`*100 as growth FROM hhh.hhh_weekly_pagetitle p1, hhh_weekly_pagetitle p2 where p1.`ga:pageTitle`= p2.`ga:pageTitle` and p1.`ga:isoWeek`='22' and  p2.`ga:isoWeek`=23 order by (p2.`ga:users`-p1.`ga:users`)/p1.`ga:users` desc;")
+    df = pd.DataFrame(columns=('title','prev_week','cur_week','growth'))
+
+    idx=0
+
+    for c in cursor:
+        df.loc[idx]=[c['title'],c['prev_week'],c['cur_week'],c['growth']]
+    #    df.loc[idx]=['okok',333]
+        idx+=1
+    save_sheet(df,'WeeklyReport','pagetitle')
+
+t = threading.Thread(target = do_jobs)
+t.start()
+t.join()
+

+ 135 - 0
hhh/GA_Path.py

@@ -0,0 +1,135 @@
+"""Hello Analytics Reporting API V4."""
+#!/usr/bin/python3
+import sys
+import codecs
+import traceback
+import requests
+import re
+import pandas as pd
+import random
+import urllib
+import dataset
+import json
+import gspread
+import datetime
+from gspread_pandas import Spread, Client
+from oauth2client.service_account import ServiceAccountCredentials
+import os
+import threading
+from apiclient.discovery import build
+from oauth2client.service_account import ServiceAccountCredentials
+import dataset
+
+db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
+#db.query('delete from hhh_weekly_affinity')
+
+table=db['hhh_weekly_pagetitle']
+
+SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
+KEY_FILE_LOCATION = 'c:\\keys\\choozmo-ga-beee24b7a4c1.json'
+VIEW_ID = '188916214'
+
+
+def initialize_analyticsreporting():
+  """Initializes an Analytics Reporting API V4 service object.
+
+  Returns:
+    An authorized Analytics Reporting API V4 service object.
+  """
+  credentials = ServiceAccountCredentials.from_json_keyfile_name(
+      KEY_FILE_LOCATION, SCOPES)
+
+  # Build the service object.
+  analytics = build('analyticsreporting', 'v4', credentials=credentials)
+
+  return analytics
+
+
+def get_report(analytics,body):
+  """Queries the Analytics Reporting API V4.
+
+  Args:
+    analytics: An authorized Analytics Reporting API V4 service object.
+  Returns:
+    The Analytics Reporting API V4 response.
+  """
+  return analytics.reports().batchGet(
+      body={
+        'reportRequests':body
+        # [
+        #{
+#          'viewId': VIEW_ID,
+#          'dateRanges': [{'startDate': '14daysAgo', 'endDate': 'today'}],
+#          'dateRanges': [{'startDate': '2021-05-30', 'endDate': '2021-06-05'}],
+
+#          'metrics': [{'expression': 'ga:users'}],
+#          'dimensions': [{'name': 'ga:sourceMedium'}]
+#          'dimensions': [{'name': 'ga:date'},{'name': 'ga:sourceMedium'}]
+
+#        }]
+      }
+  ).execute()
+
+
+def print_response(response):
+  """Parses and prints the Analytics Reporting API V4 response.
+
+  Args:
+    response: An Analytics Reporting API V4 response.
+  """
+  result=[]
+  for report in response.get('reports', []):
+    columnHeader = report.get('columnHeader', {})
+    dimensionHeaders = columnHeader.get('dimensions', [])
+    metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
+
+    for row in report.get('data', {}).get('rows', []):
+      dimensions = row.get('dimensions', [])
+      dateRangeValues = row.get('metrics', [])
+      ga_dict={}
+
+      for header, dimension in zip(dimensionHeaders, dimensions):
+        print(header + ': ', dimension)
+        ga_dict[header]=dimension
+      
+      for i, values in enumerate(dateRangeValues):
+        print('Date range:', str(i))
+        for metricHeader, value in zip(metricHeaders, values.get('values')):
+          ga_dict[metricHeader.get('name')]=value
+#          print(metricHeader.get('name') + ':', value)
+        result.append(ga_dict)
+    return result
+#        print(ga_dict)
+
+def main():
+  analytics = initialize_analyticsreporting()
+
+  body=[{ 'viewId': VIEW_ID,
+#  'dateRanges': [{'startDate': '2021-05-30', 'endDate': '2021-06-05'}],
+  'dateRanges': [{'startDate': '2021-05-28', 'endDate': '2021-06-11'}],
+  'metrics': [{'expression': 'ga:users'}],
+
+#  'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
+#  'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
+#  'dimensions': [{'name': 'ga:date'},{'name': 'ga:sourceMedium'}]
+#  'dimensions': [{'name': 'ga:date'}]
+  'dimensions': [{'name': 'ga:pageTitle'},{'name': 'ga:isoWeek'},]
+
+  }]
+
+  response = get_report(analytics,body)
+  ga_dict=print_response(response)
+#  print(ga_dict)
+#  print(ga_dict)
+  for elmt in ga_dict:
+#    elmt['ga:users']=int(elmt['ga:users'])
+    if int(elmt['ga:users']) > 2:
+#     print(elmt['ga:interestAffinityCategory'])
+      table.insert(elmt)
+
+#        print(elmt['ga:sourceMedium'])
+
+
+if __name__ == '__main__':
+  main()
+

+ 1 - 0
hhh/gspace_fetch_ranks.py

@@ -172,6 +172,7 @@ def get_designer_statistics(designer_list):
 qlist=[]
 #cursor=db.query('select name,vip from customer_list order by updated asc limit 20')
 cursor=db.query('select name,vip from customer_list where vip =  0 order by updated asc limit 20')
+#cursor=db.query('select name,vip from customer_list where vip =  1 order by updated asc limit 20')
 
 for c in cursor:
     qlist.append((c['name'],c['vip']))

+ 48 - 0
hhh/tests/phantomtest.py

@@ -0,0 +1,48 @@
+from selenium import webdriver
+from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
+import time
+import os
+import urllib.parse
+from selenium.webdriver.support.ui import WebDriverWait
+from selenium.webdriver.common.by import By
+from selenium.webdriver.support import expected_conditions as EC
+import codecs
+import random
+
+def process_query(q):
+    googleurl='https://www.google.com/search?q='+urllib.parse.quote(q)
+    driver = webdriver.Remote(
+    command_executor='http://127.0.0.1:4444/wd/hub',
+    desired_capabilities=DesiredCapabilities.CHROME)
+    driver.set_window_size(1400,1000)
+    driver.get(googleurl)
+    time.sleep(3)
+
+    elmts=driver.find_elements_by_xpath("//div[@class='g']//div[@class='yuRUbf']//a")
+    idx=1
+    ranking=-1
+    for elmt in elmts:
+        href=elmt.get_attribute('href')
+        txt=elmt.text
+        if len(txt)>10:
+            if 'hhh.com.tw' in href:
+                webdriver.ActionChains(driver).move_to_element(elmt).perform()
+                webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
+                break
+    fw=codecs.open('c:/tmp/gg.html','w','utf-8')
+    fw.write(driver.page_source)
+    fw.close()
+    time.sleep(10)
+
+#    driver.save_screenshot("c:/tmp/screen.png")
+#    os.startfile("c:/tmp/screen.png")
+    driver.quit()
+
+
+qlist=['上築空間設計','豐聚室內裝修','富億空間設計','浩室設計','京璽國際','IS國際設計','優尼客','摩登雅舍','亞維空間設計','比沙列','富億設計','豐聚設計']
+
+while True:
+    q=random.choice(qlist)
+    print(q)
+    process_query(q)
+

+ 2 - 4
tests/mtest.py

@@ -10,10 +10,8 @@ gmail_user = 'edm@choozmo.com'
 gmail_password='wqdsyqwvppmubitv'
 
 sent_from = gmail_user
-to = ['jared@choozmo.com','nina.huang@choozmo.com','ana@choozmo.com','ming@choozmo.com','mike@choozmo.com','andy@choozmo.com','hana@choozmo.com','stacy@choozmo.com','wen@choozmo.com','yukyo@choozmo.com','fxp87257@gmail.com','noodlesloves@gmail.com']
-img_data=None
-with open('c:/tmp/coupon.png', 'rb') as f:
-    img_data = f.read()
+#to = ['jared@choozmo.com','nina.huang@choozmo.com','ana@choozmo.com','ming@choozmo.com','mike@choozmo.com','andy@choozmo.com','hana@choozmo.com','stacy@choozmo.com','wen@choozmo.com','yukyo@choozmo.com','fxp87257@gmail.com','noodlesloves@gmail.com']
+to = ['jared@choozmo.com']
 
 msg = MIMEMultipart()
 msg['Subject'] = '[2021-06-11] 趨勢日報 | ChoozMo '