Skip to content

Commit 6fa42c3

Browse files
committed
feat(python): guides
1 parent be39acd commit 6fa42c3

15 files changed

+267
-11
lines changed
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import asyncio
21
import json
32
{{> snippets/import}}
43

54

6-
async def main():
5+
if __name__ == '__main__':
76
# use the region matching your applicationID
87
_{{> snippets/init}}
98

@@ -14,5 +13,3 @@ async def main():
1413
resp = {{#dynamicSnippet}}pushSetup{{/dynamicSnippet}}
1514

1615
print(resp)
17-
18-
asyncio.run(main())

templates/python/guides/search/deleteMultipleIndices.mustache

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import asyncio
21
{{> snippets/import}}
32

43

5-
async def main():
4+
if __name__ == '__main__':
65
try:
76
# You need an API key with `deleteIndex`
87
_{{> snippets/init}}
@@ -29,4 +28,3 @@ async def main():
2928
except Exception as e:
3029
print(f"Error: {e}")
3130

32-
asyncio.run(main())
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import json
2+
3+
{{> snippets/import}}
4+
5+
6+
if __name__ == '__main__':
7+
try:
8+
_{{> snippets/init}}
9+
10+
with open("records.json", "r", encoding="utf-8") as f:
11+
records = json.load(f)
12+
13+
chunk_size = 10000
14+
15+
for begin_index in range(0, len(records), chunk_size):
16+
chunk = records[begin_index:begin_index + chunk_size]
17+
{{#dynamicSnippet}}saveObjectsChunks{{/dynamicSnippet}}
18+
19+
except Exception as e:
20+
print(f"Error: {e}")
21+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{{> snippets/import}}
2+
3+
def _get_all_app_id_configurations():
4+
return [] # A list of your MCM AppID/ApiKey pairs
5+
6+
if __name__ == '__main__':
7+
playlists = [] # Your records
8+
9+
# Fetch from your own data storage and with your own code
10+
# the list of application IDs and API keys to target each cluster
11+
configurations = _get_all_app_id_configurations()
12+
13+
# Send the records to each cluster
14+
for appID, apiKey in configurations:
15+
try:
16+
_client = SearchClientSync(appID, apiKey)
17+
{{#dynamicSnippet}}saveObjectsPlaylists{{/dynamicSnippet}}
18+
except Exception as e:
19+
print(f"Error: {e}")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import json
2+
3+
{{> snippets/import}}
4+
5+
6+
if __name__ == '__main__':
7+
try:
8+
_{{> snippets/init}}
9+
10+
with open("products.json", "r", encoding="utf-8") as f:
11+
products = json.load(f)
12+
13+
records = []
14+
15+
for product in products:
16+
reference = product['product_reference']
17+
18+
suffixes = []
19+
while len(reference) > 1:
20+
reference = reference[1:]
21+
suffixes.append(reference)
22+
23+
product['product_reference_suffixes'] = suffixes
24+
records.append(product)
25+
26+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
27+
except Exception as e:
28+
print(f"Error: {e}")
29+
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
import asyncio
21
import requests
32
{{> snippets/import}}
43

54

6-
async def main():
5+
if __name__ == '__main__':
76
# Fetch sample dataset
87
url = "https://dashboard.algolia.com/api/1/sample_datasets?type=movie"
98
movies = requests.get(url).json()
@@ -13,5 +12,3 @@ async def main():
1312

1413
# Save records in Algolia index
1514
{{#dynamicSnippet}}saveObjectsMovies{{/dynamicSnippet}}
16-
17-
asyncio.run(main())
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{> snippets/import}}
2+
3+
4+
if __name__ == '__main__':
5+
playlists = [] # Your records
6+
7+
try:
8+
_{{> snippets/init}}
9+
10+
{{#dynamicSnippet}}saveObjectsPlaylistsWithUserIDPublic{{/dynamicSnippet}}
11+
except Exception as e:
12+
print(f"Error: {e}")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{> snippets/import}}
2+
from algoliasearch.search.models.browse_response import BrowseResponse
3+
4+
5+
if __name__ == '__main__':
6+
try:
7+
_{{> snippets/init}}
8+
9+
records = []
10+
def _aggregator(resp: BrowseResponse):
11+
for hit in resp.hits:
12+
hit_dict = hit.to_dict()
13+
records.append({
14+
'twitterHandle': hit_dict['twitterHandle'],
15+
'nbFollowers': hit_dict['nbFollowers'],
16+
'isPopular': hit_dict['nbFollowers'] > 1000000,
17+
})
18+
19+
_client.browse_objects(
20+
index_name="<YOUR_INDEX_NAME>",
21+
aggregator=_aggregator,
22+
)
23+
24+
{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}}
25+
except Exception as e:
26+
print(f"Error: {e}")
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import time
2+
3+
{{> snippets/import}}
4+
5+
6+
if __name__ == '__main__':
7+
try:
8+
_{{> snippets/init}}
9+
10+
date_timestamp = time.time() # Get current timestamp
11+
search_params = {
12+
"query": "<YOUR_SEARCH_QUERY>",
13+
"filters": f"date_timestamp > {date_timestamp}"
14+
}
15+
16+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
17+
except Exception as e:
18+
print(f"Error: {e}")
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{{> snippets/import}}
2+
3+
4+
def _get_google_analytics_user_id_from_browser_cookie(cookie_name: str) -> str:
5+
# Implement your logic here
6+
return ''
7+
8+
if __name__ == '__main__':
9+
try:
10+
_{{> snippets/init}}
11+
12+
user_token = _get_google_analytics_user_id_from_browser_cookie('_ga')
13+
search_params = {
14+
'query': '<YOUR_SEARCH_QUERY>',
15+
'userToken': user_token,
16+
}
17+
18+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
19+
20+
logged_in_user = None
21+
22+
search_params['user_token'] = logged_in_user or user_token
23+
24+
{{#dynamicSnippet}}searchWithSearchParams{{/dynamicSnippet}}
25+
except Exception as e:
26+
print(f"Error: {e}")

0 commit comments

Comments
 (0)