Compare commits
2 Commits
73f6015393
...
b38e2d2614
| Author | SHA1 | Date | |
|---|---|---|---|
| b38e2d2614 | |||
| 6b14fa1bac |
42
script.py
42
script.py
@@ -1,9 +1,16 @@
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import json
|
||||||
|
import logging
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from requests.auth import HTTPBasicAuth
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
BASE_URL = "https://livetse.ir/wp-json/wp/v2/comments"
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s - %(levelname)s - %(message)s"
|
||||||
|
)
|
||||||
|
|
||||||
|
BASE_URL = "https://livetse.ir/wp-json/wp/v2/"
|
||||||
|
|
||||||
USERNAME = "your_username"
|
USERNAME = "your_username"
|
||||||
APP_PASSWORD = "your_application_password"
|
APP_PASSWORD = "your_application_password"
|
||||||
@@ -14,11 +21,12 @@ def fetch_comments(post_id=None, per_page=100):
|
|||||||
page = 1
|
page = 1
|
||||||
total_pages = 1
|
total_pages = 1
|
||||||
|
|
||||||
|
COMMENTS_URL = f"{BASE_URL}comments"
|
||||||
|
|
||||||
while page <= total_pages:
|
while page <= total_pages:
|
||||||
params = {
|
params = {
|
||||||
"per_page": per_page,
|
"per_page": per_page,
|
||||||
"page": page,
|
"page": page
|
||||||
"status": "approve"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if post_id:
|
if post_id:
|
||||||
@@ -26,7 +34,7 @@ def fetch_comments(post_id=None, per_page=100):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
BASE_URL,
|
COMMENTS_URL,
|
||||||
params=params,
|
params=params,
|
||||||
auth=HTTPBasicAuth(USERNAME, APP_PASSWORD),
|
auth=HTTPBasicAuth(USERNAME, APP_PASSWORD),
|
||||||
timeout=10
|
timeout=10
|
||||||
@@ -37,36 +45,36 @@ def fetch_comments(post_id=None, per_page=100):
|
|||||||
|
|
||||||
if page == 1:
|
if page == 1:
|
||||||
total_pages = int(response.headers.get("X-WP-TotalPages", 1))
|
total_pages = int(response.headers.get("X-WP-TotalPages", 1))
|
||||||
print(f"Total pages: {total_pages}")
|
logging.info(f"Total pages: {total_pages}")
|
||||||
|
|
||||||
if not comments:
|
if not comments:
|
||||||
print("No more comments.")
|
logging.warning("No more comments.")
|
||||||
break
|
break
|
||||||
|
|
||||||
all_comments.extend(comments)
|
all_comments.extend(comments)
|
||||||
print(f"Fetched page {page}/{total_pages} ({len(comments)} comments)")
|
logging.info(f"Fetched page {page}/{total_pages} ({len(comments)} comments)")
|
||||||
|
|
||||||
page += 1
|
page += 1
|
||||||
sleep(0.5)
|
sleep(0.5)
|
||||||
|
|
||||||
elif response.status_code == 401:
|
elif response.status_code == 401:
|
||||||
print("Authentication failed ")
|
logging.error("Authentication failed")
|
||||||
break
|
break
|
||||||
|
|
||||||
elif response.status_code == 403:
|
elif response.status_code == 403:
|
||||||
print("Access forbidden ")
|
logging.error("Access forbidden")
|
||||||
break
|
break
|
||||||
|
|
||||||
elif response.status_code == 400:
|
elif response.status_code == 400:
|
||||||
print("Bad request :", response.text)
|
logging.error(f"Bad request: {response.text}")
|
||||||
break
|
break
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print(f"Error: {response.status_code} - {response.text}")
|
logging.error(f"Error: {response.status_code} - {response.text}")
|
||||||
break
|
break
|
||||||
|
|
||||||
except requests.exceptions.RequestException as e:
|
except requests.exceptions.RequestException as e:
|
||||||
print("Request failed:", e)
|
logging.exception(f"Request failed: {e}")
|
||||||
break
|
break
|
||||||
|
|
||||||
return all_comments
|
return all_comments
|
||||||
@@ -82,15 +90,17 @@ def main():
|
|||||||
|
|
||||||
comments = fetch_comments(post_id=post_id)
|
comments = fetch_comments(post_id=post_id)
|
||||||
|
|
||||||
print(f"\nTotal comments fetched: {len(comments)}")
|
logging.info(f"Total comments fetched: {len(comments)}")
|
||||||
|
|
||||||
for c in comments[:3]:
|
for c in comments[:3]:
|
||||||
print("-" * 40)
|
logging.info("-" * 40)
|
||||||
print("Author:", c.get("author_name"))
|
logging.info(f"Author: {c.get('author_name')}")
|
||||||
print("Content:", c.get("content", {}).get("rendered"))
|
logging.info(f"Content: {c.get('content', {}).get('rendered')}")
|
||||||
|
|
||||||
save_to_json(comments)
|
save_to_json(comments)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user