File size: 1,815 Bytes
1f95777
 
 
 
 
151c2dd
 
 
 
 
 
 
 
1f95777
 
151c2dd
1f95777
151c2dd
 
 
 
1ec143e
151c2dd
 
 
 
 
1ec143e
151c2dd
 
1f95777
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151c2dd
 
 
 
1f95777
 
 
 
 
151c2dd
1f95777
 
 
 
 
 
151c2dd
 
 
 
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

from os import getenv
from os.path import exists
from functools import cache
import json
import streamlit as st
from dotenv import load_dotenv
from googleapiclient.discovery import build
from slugify import slugify

from beautiful_soup.app import extract_content

@cache
def google_search_api_request( query ):
    load_dotenv()
    api_key = getenv('GOOGLE_SEARCH_API_KEY')
    # cx = os.getenv('GOOGLE_SEARCH_ENGINE_ID')
    service = build(
        "customsearch",
        "v1",
        developerKey=api_key,
        cache_discovery=False
    )

    return service.cse().list(
        q=query,
        cx='05048cc2df6134a06',
        num=5,
        ).execute()

def search_results( query ):
    file_path = 'search-results/' + slugify( query ) + '.json'

    results = []
    if exists( file_path ):
        with open( file_path, 'r' ) as results_file:
            results = json.load( results_file )
    else:
        search_result = google_search_api_request( query )
        if (  int( search_result['searchInformation']['totalResults'] ) > 0 ):
            results = search_result['items']
            with open( file_path, 'w' ) as results_file:
                json.dump( results, results_file )

    if ( len( results ) == 0 ) :
        raise Exception('No results found.')
    
    return results

def main():
    st.title('Google Search')
    query = st.text_input('Search query')

    if query :
        try:
            results = search_results( query )
        except Exception as exception:
            st.exception(exception)

        for result in results:
            st.write(result['link'])
            try:
                st.write( extract_content( result['link'] ) )
            except Exception as exception:
                st.exception(exception)


if __name__ == '__main__':
    main()