123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- #!/usr/bin/python3
- import sys
- import codecs
- import traceback
- import requests
- import re
- import pandas as pd
- import random
- import urllib
- import json
- # import gspread
- import datetime
- # from gspread_pandas import Spread, Client
- from oauth2client.service_account import ServiceAccountCredentials
- import os
- import threading
- from googleapiclient.discovery import build
- from oauth2client.service_account import ServiceAccountCredentials
- import dataset
- import pymysql
- pymysql.install_as_MySQLdb()
- from datetime import datetime
- import platform
- db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/hhh?charset=utf8mb4')
- db.query('delete from ga_pagepath')
- db.begin()
- table=db['ga_pagepath']
- property_id= 'properties/375530887'
- SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
- platform_now = platform.system().lower()
- KEY_FILE_LOCATION = "C:\/Users\/s1301\/Downloads\/corded-velocity-301807-9262189e4dd5.json"
- VIEW_ID = '213054263'
- # line notify header
- headers = {
- "Authorization": "Bearer " + "zsaS4I79fkbun9LLF8mQmeHL4PlEtBtN7OLzTp8m72Y",
- "Content-Type": "application/x-www-form-urlencoded"
- }
- com_table = []
- def creat_table():
- for i in range(0,24):
- com_table.append([i,6000])
- # com_table.append([24,70000])
- print(com_table)
- def send_msg_pg(pv):
- # line notify send message
- current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') #現在時間
- hour = datetime.now().strftime('%H')
- # 判斷是否達標
- complet="否"
- # for i in range(0,25):
- # if int(hour)+1==com_table[i][0]:
- # print(i)
- # if int(kw) > com_table[i][1] :
- # complet="是"
- # elif int(hour) == 24:
- # if int(kw) > 70000 :
- # complet="是"
- print('網頁瀏覽量',pv)
- if int(pv) > 6000:
- complet = "是"
- params = {"message": "\n現在時間: " + current_time + "\n當前pageViews: "+pv + "\n是否達標: " + complet}
- print(params)
- return params
- # r = requests.post("https://notify-api.line.me/api/notify",headers=headers, params=params)
- # print(r.text)
- 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('analyticsdata', 'v1beta', 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.
- """
- response = analytics.properties().batchRunReports(property=property_id, body=body).execute()
- print(response)
- report_data = response.get('reports')
- pv = report_data[0].get('rows')[0].get('metricValues')[0].get('value')
- return pv
- 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():
- # creat_table()
- analytics = initialize_analyticsreporting()
- print(analytics)
- #(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)
- current_time = datetime.now().strftime('%Y-%m-%d') #現在時間
- body={
- "requests": [
- {
- "dateRanges": [
- {
- "startDate": "2023-08-08",
- "endDate": "2023-08-08"
- }
- ],
- "metrics": [
- {
- "name": "screenPageViews"
- }
- ],
- "limit": "100"
- }
- ]
- }
- response = get_report(analytics,body)
- print(response)
- # ga_dict=print_response(response)
- # for elmt in ga_dict:
- # print(elmt)
- # hour = datetime.now().strftime('%H')
- # if int(hour)+1 > 8 :
- # message = send_msg_pg(elmt['ga:pageviews'])
- # return message
- if __name__ == '__main__':
- # creat_table()
- main()
|