|
2 | 2 | # -*- coding: utf-8 -*- |
3 | 3 | '''PageSpeed Insights Single + Google Cloud Functions''' |
4 | 4 | import os |
5 | | -from googleapiclient.discovery import build |
| 5 | +import requests |
6 | 6 |
|
7 | 7 | # Access Token, generated from GCP Console Credentials page. |
8 | 8 | API_KEY = os.getenv('GCP_API_KEY') |
9 | 9 |
|
10 | | -# For local development, setup http proxy as needed. |
11 | | -HTTP = None |
| 10 | +GAPI_PSI = "https://www.googleapis.com/pagespeedonline/v5/runPagespeed" |
| 11 | + |
| 12 | +SESSION = requests.Session() |
| 13 | + |
| 14 | +PROXIES = None |
12 | 15 |
|
13 | | -URL = "https://m.ctrip.com/webapp/flight/schedule/detail.html" |
14 | 16 |
|
15 | 17 | def run(url): |
16 | | - pagespeedonline = build( |
17 | | - serviceName = 'pagespeedonline', |
18 | | - version = 'v5', |
19 | | - http = HTTP, |
20 | | - developerKey = API_KEY |
21 | | - ) |
22 | | - response = pagespeedonline.pagespeedapi().runpagespeed(url = url).execute() |
23 | | - print(response) |
| 18 | + try: |
| 19 | + payload = {"url": url, |
| 20 | + "category": "performance", |
| 21 | + "locale": "zh", |
| 22 | + "strategy": "mobile", |
| 23 | + "key": API_KEY |
| 24 | + } |
| 25 | + response = SESSION.get(url=GAPI_PSI, params=payload, proxies=PROXIES) |
| 26 | + print(response.status_code) |
| 27 | + print(response.json()) |
| 28 | + except requests.RequestException as _e: |
| 29 | + print(_e) |
24 | 30 | return ('OK', 200) |
25 | 31 |
|
26 | | -def run_http(request): |
27 | | - request_json = request.get_json() |
28 | | - try: |
29 | | - url = request_json['url'] |
30 | | - return run(url) |
31 | | - except KeyError: |
32 | | - return ('', 400) |
33 | 32 |
|
34 | 33 | def run_pubsub(event, context): |
35 | 34 | import base64 |
36 | 35 | pubsub_message = base64.urlsafe_b64decode(event['data']).decode('utf-8') |
37 | 36 | run(pubsub_message) |
38 | 37 | return 'OK' |
39 | 38 |
|
40 | | -def test_run_http(): |
41 | | - from flask import Request |
42 | | - _request = Request.from_values(json = { "url": URL }) |
43 | | - run_http(_request) |
44 | 39 |
|
45 | | -def test_run_pubsub(): |
| 40 | +def test_run_http(test_url): |
| 41 | + run(test_url) |
| 42 | + |
| 43 | + |
| 44 | +def test_run_pubsub(test_url): |
46 | 45 | import base64 |
47 | | - event = { "data": base64.urlsafe_b64encode(URL.encode('utf-8'))} |
| 46 | + event = {"data": base64.urlsafe_b64encode(test_url.encode('utf-8'))} |
48 | 47 | context = None |
49 | 48 | run_pubsub(event, context) |
50 | 49 |
|
51 | | -if __name__ == "__main__": |
52 | | - import httplib2 |
53 | | - HTTP = httplib2.Http(proxy_info = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_SOCKS5, '127.0.0.1', 1086)) |
54 | 50 |
|
55 | | - test_run_http() |
56 | | - test_run_pubsub() |
| 51 | +if __name__ == "__main__": |
| 52 | + PROXIES = { |
| 53 | + "http": "127.0.0.1:1087", |
| 54 | + "https": "127.0.0.1:1087", |
| 55 | + } |
| 56 | + |
| 57 | + _test_url = "https://m.ctrip.com/webapp/flight/schedule/detail.html" |
| 58 | + test_run_http(_test_url) |
| 59 | + test_run_pubsub(_test_url) |
0 commit comments