File size: 2,042 Bytes
cd63cf6
 
 
 
 
103de27
cd63cf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103de27
cd63cf6
 
 
 
103de27
cd63cf6
103de27
 
 
 
 
 
 
 
 
 
 
cd63cf6
 
 
 
 
 
 
 
 
 
 
6cbd4a5
 
 
cd63cf6
103de27
cd63cf6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import datetime
import json
import os
from logging import getLogger
import pandas as pd
import time
from dotenv import load_dotenv
from googleapiclient.discovery import build

load_dotenv(".env.dev")
api_key = os.getenv("GOOGLE_CUSTOM_SEARCH_API_KEY")
cx = os.getenv("SEARCH_ENGINE_ID")

gcsearch_client = build(
    "customsearch", "v1", developerKey=api_key
)

temp_csv_file_path = os.getenv("TEMP_CSV_RESULTS_PATH")

logger = getLogger(__name__)


# Function to perform a search
def google_search(query):
    today = datetime.datetime.today().strftime("%Y%m%d")
    datetime.datetime.today().strftime("%Y/%m/%d %H:%M:%S")

    out = []
    results = []
    for i in range(1, 2):
        response = gcsearch_client.cse().list(q=query, cx=cx, start=i).execute()
        if 'items' in response:
            search_result = (
                gcsearch_client.cse().list(q=query, cx=cx, start=i)
                .execute()["items"]
            )[:5]
        else:
            search_result = ()
        for item in search_result:
            results.append((item["title"], item["link"]))
    time.sleep(0.5)

    # query = query.replace("脱炭素", "省エネ")
    # for i in range(1, 2):
    #     if 'items' in response:
    #         search_result = (
    #             gcsearch_client.cse().list(q=query, cx=cx, start=i)
    #             .execute()["items"]
    #         )[:2]
    #     else:
    #         search_result = ()
    #     for item in search_result:
    #         results.append((item["title"], item["link"]))

    output = []
    for res in results:
        temp = {"query": query, "title": res[0], "url": res[1]}
        out.append(temp)
        output.append(res[1])

    with open("output.json", "w") as f:
        json.dump(output, f, indent=4)

    jsonstr = json.dumps(out, ensure_ascii=False)

    # /response_.json → ./response_.jsonに変更した
    with open("./response_.json", mode="w") as response_file:
        response_file.write(jsonstr)
    logger.info(f'Search reuslt: {out}')
    return out, jsonstr