Python scraper based on AI
Python ai-crawlerai-scrapingai-searchcrawlerdata-extractionfirecrawl-alternativelarge-language-modelllmmarkdownragscrapingscraping-pythonweb-crawlerweb-crawlersweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
refactoring of nodes
VinciGit00a73496df9b55
diff --git a/examples/openai/custom_graph_openai.py b/examples/openai/custom_graph_openai.pyindex aa1eea7c..7febda77 100644--- a/examples/openai/custom_graph_openai.py+++ b/examples/openai/custom_graph_openai.py@@ -6,7 +6,7 @@ import osfrom dotenv import load_dotenvfrom scrapegraphai.models import OpenAIfrom scrapegraphai.graphs import BaseGraph-from scrapegraphai.nodes import FetchNode, ParseNode, RAGNode, GenerateAnswerNode+from scrapegraphai.nodes import FetchNode, ParseNode, RAGNode, GenerateAnswerNode, RobotsNodeload_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.pysimilarity index 100%rename from tests/node_test.pyrename to examples/single_node/robot_node.pydiff --git a/manual deployment/commit_and_push_with_tests.sh b/manual deployment/commit_and_push_with_tests.shnew file mode 100755index 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 pushdiff --git a/scrapegraphai/graphs/base_graph.py b/scrapegraphai/graphs/base_graph.pyindex 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_timetotal_exec_time += node_exec_timediff --git a/scrapegraphai/nodes/robots_node.py b/scrapegraphai/nodes/robots_node.pyindex 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.mdindex 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 pcFor running the tests run the command:```python-pytests+pytest```\ No newline at end of filediff --git a/tests/inputs/books.xml b/tests/graphs/inputs/books.xmlsimilarity index 100%rename from tests/inputs/books.xmlrename to tests/graphs/inputs/books.xmldiff --git a/tests/inputs/plain_html_example.txt b/tests/graphs/inputs/plain_html_example.txtsimilarity index 100%rename from tests/inputs/plain_html_example.txtrename to tests/graphs/inputs/plain_html_example.txtdiff --git a/tests/scrape_plain_text_llama3_test.py b/tests/graphs/scrape_plain_text_llama3_test.pysimilarity index 100%rename from tests/scrape_plain_text_llama3_test.pyrename to tests/graphs/scrape_plain_text_llama3_test.pydiff --git a/tests/scrape_plain_text_mistral_test.py b/tests/graphs/scrape_plain_text_mistral_test.pysimilarity index 100%rename from tests/scrape_plain_text_mistral_test.pyrename to tests/graphs/scrape_plain_text_mistral_test.pydiff --git a/tests/scrape_xml_ollama_test.py b/tests/graphs/scrape_xml_ollama_test.pysimilarity index 100%rename from tests/scrape_xml_ollama_test.pyrename to tests/graphs/scrape_xml_ollama_test.pydiff --git a/tests/script_generator_test.py b/tests/graphs/script_generator_test.pysimilarity index 100%rename from tests/script_generator_test.pyrename to tests/graphs/script_generator_test.pydiff --git a/tests/smart_scraper_ollama_test.py b/tests/graphs/smart_scraper_ollama_test.pysimilarity index 100%rename from tests/smart_scraper_ollama_test.pyrename to tests/graphs/smart_scraper_ollama_test.pydiff --git a/tests/nodes/.env.example b/tests/nodes/.env.examplenew file mode 100644index 00000000..12c1491c--- /dev/null+++ b/tests/nodes/.env.example@@ -0,0 +1 @@+OPENAI_APIKEY="your openai api key"\ No newline at end of filediff --git a/tests/nodes/robot_node_test.py b/tests/nodes/robot_node_test.pynew file mode 100644index 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()