Skip to content

Commit 9fc8e10

Browse files
committed
1st commit
0 parents  commit 9fc8e10

21 files changed

Lines changed: 157701 additions & 0 deletions

.github/workflows/build.yml

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
name: Deploy Scraper to Prefect
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
services:
12+
elasticsearch:
13+
image: docker.elastic.co/elasticsearch/elasticsearch:8.17.3
14+
ports:
15+
- 9200:9200
16+
env:
17+
discovery.type: single-node
18+
ELASTIC_PASSWORD: ${{ secrets.ELASTIC_PASSWORD }}
19+
xpack.security.enabled: "false"
20+
options: >-
21+
--name es01
22+
--health-cmd="curl --silent --fail localhost:9200/_cluster/health || exit 1"
23+
--health-interval=10s
24+
--health-timeout=5s
25+
--health-retries=10
26+
27+
steps:
28+
- uses: actions/checkout@v3
29+
30+
- name: Set up Python
31+
uses: actions/setup-python@v4
32+
with:
33+
python-version: '3.10'
34+
35+
- name: Install dependencies
36+
run: |
37+
pip install --upgrade pip
38+
pip install -r requirements.txt
39+
40+
- name: Wait for Elasticsearch to be ready
41+
run: |
42+
echo "Waiting for Elasticsearch..."
43+
until curl -s -u elastic:${{ secrets.ELASTIC_PASSWORD }} http://localhost:9200 >/dev/null; do
44+
sleep 5
45+
done
46+
echo "Elasticsearch is up!"
47+
48+
- name: Log in to Docker Hub
49+
uses: docker/login-action@v2
50+
with:
51+
username: ${{ secrets.DOCKER_USERNAME }}
52+
password: ${{ secrets.DOCKER_PASSWORD }}
53+
54+
- name: Build Docker image
55+
run: docker build -t ${{ secrets.DOCKER_USERNAME }}/scraping-image:latest .
56+
57+
- name: Push Docker image
58+
run: docker push ${{ secrets.DOCKER_USERNAME }}/scraping-image:latest
59+
60+
- name: Deploy flow to Prefect
61+
env:
62+
PREFECT_API_KEY: ${{ secrets.PREFECT_API_KEY }}
63+
PREFECT_API_URL: https://api.prefect.cloud/api/accounts/${{ secrets.PREFECT_ACCOUNT }}/workspaces/${{ secrets.PREFECT_WORKSPACE }}
64+
run: prefect deploy --prefect-file deployment.yaml
65+
66+
- name: Elastic indexing
67+
run: |
68+
python elastic_script.py
69+
echo "Events indexed to Elasticsearch successfully."
70+
- name: Wait for indexing
71+
run: |
72+
echo "Waiting for events to be indexed..."
73+
sleep 100 # Adjust the sleep time as necessary
74+
75+
- name: All indexed
76+
run: |
77+
echo "All events have been indexed to Elasticsearch."
78+
curl -X GET "localhost:9200/events_index/_search?pretty" -u elastic:${{ secrets.ELASTIC_PASSWORD }} -H 'Content-Type: application/json' -d'
79+
{
80+
"query": {
81+
"match_all": {}
82+
},
83+
"size": 10
84+
}'
85+
- name: Run FastAPI
86+
run: |
87+
uvicorn main:app --host 0.0.0.0 --port 8000 &
88+
sleep 5 # wait a few seconds for server to start
89+
90+
- name: Test streaming endpoint
91+
run: |
92+
curl -v http://localhost:8000/events
93+
echo "Streaming endpoint tested successfully."
94+

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
__pycache__/
2+
.venv
3+
.env
4+
text.json

Dockerfile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Use official Python 3.12 slim image
2+
FROM python:3.12-slim
3+
4+
# Set working directory
5+
WORKDIR /app
6+
7+
# Copy requirements and install dependencies
8+
COPY requirements.txt .
9+
RUN pip install --no-cache-dir -r requirements.txt
10+
11+
# Copy all source code
12+
COPY . .
13+
14+
# Expose FastAPI port
15+
EXPOSE 8000
16+
17+
# Run the FastAPI app with uvicorn
18+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Events_Project

deployment.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: 1
2+
pull:
3+
- prefect.deployments.steps.git_clone:
4+
repository: https://github.com/Chaimaekit/Events_Project.git
5+
branch: main
6+
deployments:
7+
- name: print-events
8+
entrypoint: workflow.py:print_events
9+
work_pool:
10+
name: my-process-pool
11+
12+
infrastructure:
13+
image: chaimaaeljerrar/scraping-image:latest
14+
schedules:
15+
- cron: "*/2 * * * *"
16+
timezone: UTC

docker-compose.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: '3.9'
2+
3+
services:
4+
elasticsearch:
5+
image: docker.elastic.co/elasticsearch/elasticsearch:8.12.1
6+
container_name: elasticsearch
7+
environment:
8+
- discovery.type=single-node
9+
- ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
10+
- xpack.security.enabled=false
11+
ports:
12+
- "9200:9200"
13+
networks:
14+
- backend
15+
16+
fastapi:
17+
build: .
18+
container_name: fastapi_app
19+
ports:
20+
- "8000:8000"
21+
depends_on:
22+
- elasticsearch
23+
networks:
24+
- backend
25+
environment:
26+
- ELASTICSEARCH_HOST=http://elasticsearch:9200
27+
28+
networks:
29+
backend:
30+
driver: bridge
31+

elastic_script.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from elasticsearch import Elasticsearch
2+
from into_db.connection import get_db_events
3+
from scrape.casaevents import get_casa_events
4+
from scrape.eventbrit import get_event_brit
5+
from scrape.eventsma import get_events_ma
6+
from scrape.guichet import get_guichet
7+
from dotenv import load_dotenv
8+
import os
9+
import time
10+
11+
12+
13+
load_dotenv()
14+
elastic_password = os.getenv("ELASTIC_PASSWORD")
15+
16+
17+
18+
def indexing():
19+
es = Elasticsearch("http://localhost:9200", basic_auth=("elastic", f"{elastic_password}"))
20+
events = []
21+
for _ in range(20):
22+
if es.ping():
23+
print("Connected to Elasticsearch.")
24+
break
25+
print("Waiting for Elasticsearch to be ready...")
26+
time.sleep(1)
27+
else:
28+
raise ValueError("Elasticsearch not available after waiting 20 seconds.")
29+
30+
31+
es.indices.create(index="events_index", ignore=400)
32+
casa_events = get_casa_events()
33+
event_brit = get_event_brit()
34+
events_ma = get_events_ma()
35+
guichet = get_guichet()
36+
events.extend(casa_events)
37+
events.extend(event_brit)
38+
events.extend(events_ma)
39+
events.extend(guichet)
40+
for event in events:
41+
doc = {
42+
"name": event.get("name", ""),
43+
"date": event.get("date", ""),
44+
"location": event.get("city", "")+" "+event.get("place", ""),
45+
"description": event.get("description", ""),
46+
"url": event.get("url", "")
47+
}
48+
es.index(index="events_index", document=doc)
49+
print("Indexing succeded !")
50+
51+
def check_doc(index_name, event):
52+
es = Elasticsearch("http://localhost:9200", basic_auth=("elastic", f"{elastic_password}"))
53+
result = es.search(index=index_name, body={
54+
"query": {
55+
"match":{
56+
"name":event
57+
}
58+
},
59+
"size": 20
60+
})
61+
if result:
62+
return result["hits"]["hits"]
63+
64+
if __name__ == "__main__":
65+
indexing()

0 commit comments

Comments
 (0)