Skip to content

Commit 02c2aaa

Browse files
committed
improve README
1 parent cfe86b6 commit 02c2aaa

3 files changed

Lines changed: 340 additions & 54 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ dist=dist/serpapi-$(version).tar.gz
1313

1414
.PHONY: build
1515

16-
all: clean install readme doc lint test build
16+
all: clean install readme doc lint test build oobt check
1717

1818
clean:
1919
find . -name '*.pyc' -delete
@@ -48,7 +48,7 @@ doc: readme
4848
$(MAKE) -C docs/ html
4949

5050
# https://packaging.python.org/tutorials/packaging-projects/
51-
build: doc lint test
51+
build:
5252
python3 setup.py sdist
5353

5454
# out of box testing / user acceptance before delivery

README.md

Lines changed: 170 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
<a href="https://badge.fury.io/py/serpapi-python">![Package](https://badge.fury.io/py/serpapi.svg)</a>
77
<a href="https://pepy.tech/project/serpapi-python">![Downloads](https://static.pepy.tech/personalized-badge/serpapi?period=month&units=international_system&left_color=grey&right_color=brightgreen&left_text=Downloads)</a>
8-
<a href="https://github.com/serpapi/serpapi-python/actions/workflows/python-package.yml">![Buiild](https://github.com/serpapi/serpapi-python/actions/workflows/python-package.yml/badge.svg)</a>
8+
<a href="https://github.com/serpapi/serpapi-python/actions/workflows/ci.yml">![Build](https://github.com/serpapi/serpapi-python/actions/workflows/python-package.yml/badge.svg)</a>
99
</div>
1010

1111

@@ -39,24 +39,109 @@ This example runs a search for "coffee" on Google. It then returns the results a
3939

4040
## Advanced Usage
4141
### Search API
42+
```python
43+
# load pip package
44+
import serpapi
45+
46+
# serpapi client created with default parameters
47+
client = serpapi.Client({'api_key': 'secret_key', 'engine': 'google'})
48+
49+
# We recommend that you keep your keys safe.
50+
# At least, don't commit them in plain text.
51+
# More about configuration via environment variables:
52+
# https://hackernoon.com/all-the-secrets-of-encrypting-api-keys-in-ruby-revealed-5qf3t5l
53+
54+
# search query overview (more fields available depending on search engine)
55+
params = {
56+
# select the search engine (full list: https://serpapi.com/)
57+
'engine': "google",
58+
# actual search query for google
59+
'q': "Coffee",
60+
# then adds search engine specific options.
61+
# for example: google specific parameters: https://serpapi.com/search-api
62+
'google_domain': "Google Domain",
63+
'location': "Location Requested", # example: Portland,Oregon,United States [see: Location API](#Location-API)
64+
'device': "desktop|mobile|tablet",
65+
'hl': "Google UI Language",
66+
'gl': "Google Country",
67+
'safe': "Safe Search Flag",
68+
'num': "Number of Results",
69+
'start': "Pagination Offset",
70+
'tbm': "nws|isch|shop",
71+
'tbs': "custom to be client criteria",
72+
# tweak HTTP client behavior
73+
'async': False, # true when async call enabled.
74+
'timeout': 60, # HTTP timeout in seconds on the client side only.
75+
}
4276

43-
TODO update this specification
77+
# formated search results as a Hash
78+
# serpapi.com converts HTML -> JSON
79+
results = client.search(params)
4480

45-
SerpApi Client uses urllib3 under the hood.
46-
Optionally, rhe HTTP connection can be tuned:
47-
- timeout : connection timeout by default 60s
48-
- retries : attempt to reconnect if the connection failed by default: False.
49-
serpapi is reliable at 99.99% but your company network might not be as stable.
81+
# raw search engine html as a String
82+
# serpapi.com acts a proxy to provive high throughputs, no search limit and more.
83+
raw_html = client.html(params)
84+
```
5085

51-
```python
52-
parameter = {
53-
retries: 5,
54-
timeout: 4.0,
55-
# extra user parameters
56-
}
86+
[Google search documentation](https://serpapi.com/search-api). More hands on examples are available below.
87+
88+
### Documentation
89+
* [API documentation](https://rubydoc.info/github/serpapi/serpapi-ruby/master)
90+
* [Full documentation on SerpApi.com](https://serpapi.com)
91+
* [Library Github page](https://github.com/serpapi/serpapi-ruby)
92+
* [Library GEM page](https://rubygems.org/gems/serpapi/)
93+
* [API health status](https://serpapi.com/status)
94+
95+
### Location API
96+
97+
```python
98+
import serpapi
99+
client = serpapi.Client({'api_key': 'secret_api_key'})
100+
locations = client.location({'q':'Austin', 'limit': 3})
101+
print([loc['canonical_name'] for loc in locations])
102+
```
103+
104+
it prints the first 3 locations matching Austin:
105+
```python
106+
['Austin,TX,Texas,United States', 'Austin,Texas,United States', 'Rochester,MN-Mason City,IA-Austin,MN,United States']
107+
```
108+
109+
NOTE: api_key is not required for this endpoint.
110+
111+
### Search Archive API
112+
113+
This API allows retrieving previous search results.
114+
To fetch earlier results from the search_id.
115+
116+
First, you need to run a search and save the search id.
117+
```python
118+
import serpapi
119+
client = serpapi.Client({'api_key': 'secret_api_key', 'engine': 'google'})
120+
results = client.search({'q': "Coffee"})
121+
search_id = results['search_metadata']['id']
122+
print("search_id: " + search_id)
57123
```
58124

59-
for more details: [URL LIB3 documentation]](https://urllib3.readthedocs.io/en/stable/user-guide.html)
125+
Now let's retrieve the previous search results from the archive.
126+
127+
```python
128+
import serpapi
129+
client = serpapi.Client({'api_key': 'secret_api_key'})
130+
results = client.search_archive('search_id')
131+
print(results)
132+
```
133+
134+
This code prints the search results from the archive. :)
135+
136+
### Account API
137+
138+
```python
139+
import serpapi
140+
client = serpapi.Client({'api_key': 'secret_api_key'})
141+
print(client.account())
142+
```
143+
144+
It prints your account information including plan, credit, montly
60145

61146
## Basic example per search engines
62147

@@ -425,8 +510,8 @@ client = serpapi.Client({
425510
'api_key': os.getenv("API_KEY")
426511
})
427512
data = client.search({
428-
'q': 'Electrician',
429-
'data_cid': 'ChIJOwg_06VPwokRYv534QaPC8g',
513+
'q': 'electrician',
514+
'data_cid': '6745062158417646970',
430515
})
431516
pp = pprint.PrettyPrinter(indent=2)
432517
pp.pprint(data['local_ads'])
@@ -535,27 +620,31 @@ see: [https://serpapi.com/images-results](https://serpapi.com/images-results)
535620
TODO update this section
536621
### Key goals
537622
- High code quality
538-
- KISS principles
623+
- KISS principles (https://en.wikipedia.org/wiki/KISS_principle)
539624
- Brand centric instead of search engine based
540-
- No hard coded logic per search engine
625+
- No hard coded logic per search engine on the client side.
541626
- Simple HTTP client (lightweight, reduced dependency)
542627
- No magic default values
543628
- Thread safe
629+
- Leak free
544630
- Easy to extends
545631
- Defensive code style (raise custom exception)
546-
- TDD
547-
- Best API coding pratice per platform
632+
- TDD - Test driven development (lint, ~100% code coverage)
633+
- Follow best API coding pratice per platform
548634

549635
### Inspiration
550636
The API design was inpired by the most popular Python packages.
551637
- urllib3 - https://github.com/urllib3/urllib3
552638
- Boto3 - https://github.com/boto/boto3
639+
- Numpy -
553640

554641
### Quality expectation
555642
- 0 lint issues using pylint `make lint`
556643
- 99% code coverage running `make test`
557644
- 100% test passing: `make test`
558645

646+
# Developer Guide
647+
## Design : UML diagram
559648
### Client design: Class diagram
560649
```mermaid
561650
classDiagram
@@ -565,8 +654,8 @@ classDiagram
565654
HttpClient *-- ObjectDecoder
566655
567656
class Client {
568-
engine: String
569-
api_key: String
657+
'engine': String
658+
'api_key': String
570659
parameter: Hash
571660
search()
572661
html()
@@ -595,10 +684,64 @@ sequenceDiagram
595684
Client-->>Client: decode JSON into Dict
596685
```
597686

598-
## Build flow
687+
where:
688+
- The end user implements the application.
689+
- Client refers to SerpApi:Client.
690+
- SerpApi.com is the backend HTTP / REST service.
691+
- Engine refers to Google, Baidu, Bing, and more.
692+
693+
The SerpApi.com service (backend)
694+
- executes a scalable search on `'engine': "google"` using the search query: `q: "coffee"`.
695+
- parses the messy HTML responses from Google on the backend.
696+
- returns a standardized JSON response.
697+
The class SerpApi::Client (client side / ruby):
698+
- Format the request to SerpApi.com server.
699+
- Execute HTTP Get request.
700+
- Parse JSON into Ruby Hash using a standard JSON library.
701+
Et voila!
702+
703+
## Continuous integration
704+
We love "true open source" and "continuous integration", and Test Drive Development (TDD).
705+
We are using RSpec to test [our infrastructure around the clock]) using Github Action to achieve the best QoS (Quality Of Service).
706+
707+
The directory spec/ includes specification which serves the dual purposes of examples and functional tests.
708+
709+
Set your secret API key in your shell before running a test.
710+
```bash
711+
export API_KEY="your_secret_key"
712+
```
713+
Install testing dependency
714+
```bash
715+
$ make install
716+
```
717+
718+
Check code quality using Lint.
719+
```bash
720+
$ make lint
721+
```
722+
723+
Run regression.
724+
```bash
725+
$ make test
726+
```
727+
728+
To flush the flow.
729+
```bash
730+
$ make
731+
```
732+
733+
Open coverage report generated by `rake test`
734+
```sh
735+
open coverage/index.html
736+
```
737+
738+
Open ./Rakefile for more information.
739+
740+
Contributions are welcome. Feel to submit a pull request!
741+
742+
## Dependencies
599743

600-
This project is automated using a good old Makefile.
601-
To pipe-clean the project run this:
602-
`make`
744+
HTTP requests are executed using [URL LIB3 documentation](https://urllib3.readthedocs.io/en/stable/user-guide.html).
603745

604-
Open Makefile for more details.
746+
## TODO
747+
- [] Release version 1.0.0

0 commit comments

Comments
 (0)