Explorar o código

Merge branch 'master' of http://git.choozmo.com:3000/choozmo/kw_tools

Jason hai 1 ano
pai
achega
c09318185a
Modificáronse 2 ficheiros con 107 adicións e 36 borrados
  1. 62 0
      hhh/SEO/themoo.py
  2. 45 36
      monitor/GA_innnews.py

+ 62 - 0
hhh/SEO/themoo.py

@@ -0,0 +1,62 @@
+from selenium import webdriver
+from selenium.webdriver.chrome.service import Service
+from selenium.webdriver.common.by import By
+from selenium.webdriver.common.keys import Keys
+import time
+
+
+def scrolling(driver, pgnum):
+    ub = driver.find_element("css selector",'body')
+    for i in range(pgnum):
+        ub.send_keys(Keys.PAGE_DOWN)
+        if pgnum > 1:
+            time.sleep(0.3)
+
+def process_query():
+    global driver
+    url = 'https://hhh.com.tw/HHH_NEW/designers/index_designerList.php?cid=807'
+    driver.get(url)
+
+    print('這裡')
+    time.sleep(3)
+    print(driver.current_url)
+    scrolling(driver, 1)
+    time.sleep(3)
+    elmt = driver.find_element(By.XPATH,'/html/body/div[2]/section[3]/div/div[1]/div/div[3]/div[1]/aside[6]/div[2]')
+    driver.maximize_window()
+
+    webdriver.ActionChains(driver).move_to_element(elmt).perform()
+    webdriver.ActionChains(driver).move_to_element(elmt).click().perform()
+
+    time.sleep(6)
+    driver.quit()
+
+
+def run_once():
+    global driver
+    result = []
+    user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36'
+    s = Service('C:\/Users\/s1301\/Downloads\/chromedriver_110\/chromedriver')
+    options = webdriver.ChromeOptions()
+    # options.add_argument('--headless')
+    # options.add_argument('--remote-debugging-port=9222')
+    # options.add_experimental_option("debuggerAddress", "192.168.192.45:9922")
+    options.add_argument("--user-agent=" + user_agent)
+    options.add_argument("--incognito")
+    # options.add_argument('--proxy-server=socks5://172.104.93.163:41800')
+    driver = webdriver.Chrome(
+        options=options, service=s)
+
+
+    process_query()
+    time.sleep(3)
+    driver.quit()
+
+count = 0
+while count<6:
+    try:
+        run_once()
+        count+=1
+    except:
+        pass
+

+ 45 - 36
monitor/GA_innnews.py

@@ -8,9 +8,9 @@ import pandas as pd
 import random
 import urllib
 import json
-import gspread
+# import gspread
 import datetime
-from gspread_pandas import Spread, Client
+# from gspread_pandas import Spread, Client
 from oauth2client.service_account import ServiceAccountCredentials
 import os
 import threading
@@ -27,14 +27,12 @@ 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()
-if platform_now == 'linux':
-  KEY_FILE_LOCATION = '/root/src/kw_tools/monitor/choozmo-ga-beee24b7a4c1.json' #'c:\\keys\\choozmo-ga-beee24b7a4c1.json'
-elif platform_now == 'windows':
-  KEY_FILE_LOCATION = 'c:\gitlab\kw_tools\monitor\corded-velocity-301807-9262189e4dd5.json'
-else:
-  KEY_FILE_LOCATION = '/Users/zooeytsai/Downloads/corded-velocity-301807-9262189e4dd5.json'
+
+KEY_FILE_LOCATION = "C:\/Users\/s1301\/Downloads\/corded-velocity-301807-9262189e4dd5.json"
+
 
 VIEW_ID = '213054263'
 
@@ -75,17 +73,18 @@ def send_msg_pg(pv):
     # 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)
+  credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)
 
   # Build the service object.
-  analytics = build('analyticsreporting', 'v4', credentials=credentials)
+  analytics = build('analyticsdata', 'v1beta', credentials=credentials)
 
   return analytics
 
@@ -98,12 +97,12 @@ def get_report(analytics,body):
   Returns:
     The Analytics Reporting API V4 response.
   """
-  return analytics.reports().batchGet(
-      body={
-        'reportRequests':body
-      }
-  ).execute()
+  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.
@@ -136,31 +135,41 @@ def print_response(response):
 #        print(ga_dict)
 
 def main():
-  creat_table()
+  # creat_table()
   analytics = initialize_analyticsreporting()
+  print(analytics)
 #(FB_|facebook|IG_|LINE_|LINEMP_|qsear.ch)
   current_time = datetime.now().strftime('%Y-%m-%d') #現在時間
-  body=[{ 'viewId': VIEW_ID,
-  'dateRanges': [{'startDate': current_time, 'endDate': current_time}],
-
-  'metrics': [{'expression': 'ga:users'},{'expression': 'ga:newusers'},{'expression': 'ga:sessions'},{'expression': 'ga:pageviews'},{'expression': 'ga:bounceRate'},{'expression': 'ga:pageviewsPerSession'}],
-#  'dimensions': [{'name': 'ga:pagePath'}],
-#  'orderBys':[{"fieldName": "ga:pageviews", "sortOrder": "DESCENDING"}],
-  'pageSize': '100'
-  }]
+  body={
+  "requests": [
+    {
+      "dateRanges": [
+        {
+          "startDate": "2023-08-08",
+          "endDate": "2023-08-08"
+        }
+      ],
+      "metrics": [
+        {
+          "name": "screenPageViews"
+        }
+      ],
+      "limit": "100"
+    }
+  ]
+}
 
   response = get_report(analytics,body)
-  ga_dict=print_response(response)
+  print(response)
+  # ga_dict=print_response(response)
 
-  result=[]
-  for elmt in ga_dict:
-    print(elmt)
-    hour = datetime.now().strftime('%H')
-    if int(hour)+1 > 8 :
-      message = send_msg_pg(elmt['ga:pageviews'])
-#      result.append(elmt)
-  print('inserting.....')
-  return message
+  # 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()
+  # creat_table()
   main()