Skip to content

Python scraper based on AI

refactoring of nodes

VinciGit00a73496df9b55
diff --git a/examples/openai/custom_graph_openai.py b/examples/openai/custom_graph_openai.py
index aa1eea7c..7febda77 100644
--- a/examples/openai/custom_graph_openai.py
+++ b/examples/openai/custom_graph_openai.py
@@ -6,7 +6,7 @@ import os
from dotenv import load_dotenv
from scrapegraphai.models import OpenAI
from scrapegraphai.graphs import BaseGraph
-from scrapegraphai.nodes import FetchNode, ParseNode, RAGNode, GenerateAnswerNode
+from scrapegraphai.nodes import FetchNode, ParseNode, RAGNode, GenerateAnswerNode, RobotsNode
load_dotenv()
# ************************************************
@@ -31,6 +31,12 @@ graph_config = {
llm_model = OpenAI(graph_config["llm"])
# define the nodes for the graph
+robot_node = RobotsNode(
+ input="url",
+ output=["is_scrapable"],
+ node_config={"llm": llm_model}
+)
+
fetch_node = FetchNode(
input="url | local_dir",
output=["doc"],
@@ -57,12 +63,14 @@ generate_answer_node = GenerateAnswerNode(
graph = BaseGraph(
nodes={
+ robot_node,
fetch_node,
parse_node,
rag_node,
generate_answer_node,
},
edges={
+ (robot_node, fetch_node),
(fetch_node, parse_node),
(parse_node, rag_node),
(rag_node, generate_answer_node)
diff --git a/tests/node_test.py b/examples/single_node/robot_node.py
similarity index 100%
rename from tests/node_test.py
rename to examples/single_node/robot_node.py
diff --git a/manual deployment/commit_and_push_with_tests.sh b/manual deployment/commit_and_push_with_tests.sh
new file mode 100755
index 00000000..9cf7c1af
--- /dev/null
+++ b/manual deployment/commit_and_push_with_tests.sh
@@ -0,0 +1,34 @@
+if [ $# -eq 0 ]; then
+ echo "Usage: $0 <commit_message>"
+ exit 1
+fi
+
+cd ..
+
+# Extract the commit message from the argument
+commit_message="$1"
+
+# Run Pylint on the specified Python files
+pylint pylint scrapegraphai/**/*.py scrapegraphai/*.py tests/**/*.py
+
+cd tests
+
+# Run pytest
+if ! pytest; then
+ echo "Pytest failed. Aborting commit and push."
+ exit 1
+fi
+
+cd ..
+
+# Make the pull
+git pull
+
+# Add the modified files to the Git repository
+git add .
+
+# Commit the changes with the provided message
+git commit -m "$commit_message"
+
+# Push the changes to the remote repository
+git push
diff --git a/scrapegraphai/graphs/base_graph.py b/scrapegraphai/graphs/base_graph.py
index 78394e1e..8df92b9a 100644
--- a/scrapegraphai/graphs/base_graph.py
+++ b/scrapegraphai/graphs/base_graph.py
@@ -82,8 +82,6 @@ class BaseGraph:
with get_openai_callback() as cb:
result = current_node.execute(state)
- # ADd the check for the node RObots
-
node_exec_time = time.time() - curr_time
total_exec_time += node_exec_time
diff --git a/scrapegraphai/nodes/robots_node.py b/scrapegraphai/nodes/robots_node.py
index 1820506f..c9235067 100644
--- a/scrapegraphai/nodes/robots_node.py
+++ b/scrapegraphai/nodes/robots_node.py
@@ -137,7 +137,8 @@ class RobotsNode(BaseNode):
if "no" in is_scrapable:
print("\033[33mScraping this website is not allowed\033[0m")
if not self.force_scraping:
- return {"update": "block the scraping phase"}
+ raise ValueError(
+ 'The website you selected is not scrapable')
else:
print("\033[92mThe path is scrapable\033[0m")
diff --git a/tests/Readme.md b/tests/Readme.md
index c6cfff33..747e88e4 100644
--- a/tests/Readme.md
+++ b/tests/Readme.md
@@ -6,5 +6,5 @@ Remember to activating Ollama and having installed the LLM on your pc
For running the tests run the command:
```python
-pytests
+pytest
```
\ No newline at end of file
diff --git a/tests/inputs/books.xml b/tests/graphs/inputs/books.xml
similarity index 100%
rename from tests/inputs/books.xml
rename to tests/graphs/inputs/books.xml
diff --git a/tests/inputs/plain_html_example.txt b/tests/graphs/inputs/plain_html_example.txt
similarity index 100%
rename from tests/inputs/plain_html_example.txt
rename to tests/graphs/inputs/plain_html_example.txt
diff --git a/tests/scrape_plain_text_llama3_test.py b/tests/graphs/scrape_plain_text_llama3_test.py
similarity index 100%
rename from tests/scrape_plain_text_llama3_test.py
rename to tests/graphs/scrape_plain_text_llama3_test.py
diff --git a/tests/scrape_plain_text_mistral_test.py b/tests/graphs/scrape_plain_text_mistral_test.py
similarity index 100%
rename from tests/scrape_plain_text_mistral_test.py
rename to tests/graphs/scrape_plain_text_mistral_test.py
diff --git a/tests/scrape_xml_ollama_test.py b/tests/graphs/scrape_xml_ollama_test.py
similarity index 100%
rename from tests/scrape_xml_ollama_test.py
rename to tests/graphs/scrape_xml_ollama_test.py
diff --git a/tests/script_generator_test.py b/tests/graphs/script_generator_test.py
similarity index 100%
rename from tests/script_generator_test.py
rename to tests/graphs/script_generator_test.py
diff --git a/tests/smart_scraper_ollama_test.py b/tests/graphs/smart_scraper_ollama_test.py
similarity index 100%
rename from tests/smart_scraper_ollama_test.py
rename to tests/graphs/smart_scraper_ollama_test.py
diff --git a/tests/nodes/.env.example b/tests/nodes/.env.example
new file mode 100644
index 00000000..12c1491c
--- /dev/null
+++ b/tests/nodes/.env.example
@@ -0,0 +1 @@
+OPENAI_APIKEY="your openai api key"
\ No newline at end of file
diff --git a/tests/nodes/robot_node_test.py b/tests/nodes/robot_node_test.py
new file mode 100644
index 00000000..3d57e60c
--- /dev/null
+++ b/tests/nodes/robot_node_test.py
@@ -0,0 +1,68 @@
+"""
+Module for testinh robot_node
+"""
+import os
+from dotenv import load_dotenv
+import pytest
+from scrapegraphai.models import OpenAI
+from scrapegraphai.nodes import RobotsNode
+
+# Load environment variables from .env file
+load_dotenv()
+
+
+def setup():
+ """
+ setup
+ """
+ # ************************************************
+ # Define the configuration for the graph
+ # ************************************************
+
+ openai_key = os.getenv("OPENAI_APIKEY")
+
+ graph_config = {
+ "llm": {
+ "api_key": openai_key,
+ "model": "gpt-3.5-turbo",
+ "temperature": 0,
+ "streaming": True
+ },
+ }
+
+ # ************************************************
+ # Define the node
+ # ************************************************
+
+ llm_model = OpenAI(graph_config["llm"])
+
+ robots_node = RobotsNode(
+ input="url",
+ output=["is_scrapable"],
+ node_config={"llm": llm_model}
+ )
+
+ return robots_node
+
+# ************************************************
+# Test the node
+# ************************************************
+
+
+def test_robots_node(setup):
+ """
+ Run the tests
+ """
+ state = {
+ "url": "https://twitter.com/home"
+ }
+
+ result = setup.execute(state)
+
+ assert result is not None
+
+
+# If you need to run this script directly
+if __name__ == "__main__":
+ pytest.main()