Compare commits
5 Commits
main
...
WordpessAP
| Author | SHA1 | Date | |
|---|---|---|---|
| b38e2d2614 | |||
| 6b14fa1bac | |||
| 73f6015393 | |||
| 5bdc2e1b04 | |||
| a366c0619d |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
venv/
|
||||||
94112
comments.json
Normal file
94112
comments.json
Normal file
File diff suppressed because one or more lines are too long
23
requirements.txt
Normal file
23
requirements.txt
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
attrs==25.3.0
|
||||||
|
certifi==2025.1.31
|
||||||
|
cffi==2.0.0
|
||||||
|
charset-normalizer==3.4.7
|
||||||
|
cryptography==46.0.7
|
||||||
|
h11==0.14.0
|
||||||
|
h2==4.3.0
|
||||||
|
hpack==4.1.0
|
||||||
|
hyperframe==6.1.0
|
||||||
|
idna==3.10
|
||||||
|
outcome==1.3.0.post0
|
||||||
|
pycparser==2.22
|
||||||
|
PySocks==1.7.1
|
||||||
|
requests==2.33.1
|
||||||
|
selenium==4.31.0
|
||||||
|
sniffio==1.3.1
|
||||||
|
sortedcontainers==2.4.0
|
||||||
|
trio==0.29.0
|
||||||
|
trio-websocket==0.12.2
|
||||||
|
typing_extensions==4.13.2
|
||||||
|
urllib3==2.4.0
|
||||||
|
websocket-client==1.8.0
|
||||||
|
wsproto==1.2.0
|
||||||
106
script.py
Normal file
106
script.py
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
from time import sleep
|
||||||
|
from requests.auth import HTTPBasicAuth
|
||||||
|
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s - %(levelname)s - %(message)s"
|
||||||
|
)
|
||||||
|
|
||||||
|
BASE_URL = "https://livetse.ir/wp-json/wp/v2/"
|
||||||
|
|
||||||
|
USERNAME = "your_username"
|
||||||
|
APP_PASSWORD = "your_application_password"
|
||||||
|
|
||||||
|
|
||||||
|
def fetch_comments(post_id=None, per_page=100):
|
||||||
|
all_comments = []
|
||||||
|
page = 1
|
||||||
|
total_pages = 1
|
||||||
|
|
||||||
|
COMMENTS_URL = f"{BASE_URL}comments"
|
||||||
|
|
||||||
|
while page <= total_pages:
|
||||||
|
params = {
|
||||||
|
"per_page": per_page,
|
||||||
|
"page": page
|
||||||
|
}
|
||||||
|
|
||||||
|
if post_id:
|
||||||
|
params["post"] = post_id
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = requests.get(
|
||||||
|
COMMENTS_URL,
|
||||||
|
params=params,
|
||||||
|
auth=HTTPBasicAuth(USERNAME, APP_PASSWORD),
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
comments = response.json()
|
||||||
|
|
||||||
|
if page == 1:
|
||||||
|
total_pages = int(response.headers.get("X-WP-TotalPages", 1))
|
||||||
|
logging.info(f"Total pages: {total_pages}")
|
||||||
|
|
||||||
|
if not comments:
|
||||||
|
logging.warning("No more comments.")
|
||||||
|
break
|
||||||
|
|
||||||
|
all_comments.extend(comments)
|
||||||
|
logging.info(f"Fetched page {page}/{total_pages} ({len(comments)} comments)")
|
||||||
|
|
||||||
|
page += 1
|
||||||
|
sleep(0.5)
|
||||||
|
|
||||||
|
elif response.status_code == 401:
|
||||||
|
logging.error("Authentication failed")
|
||||||
|
break
|
||||||
|
|
||||||
|
elif response.status_code == 403:
|
||||||
|
logging.error("Access forbidden")
|
||||||
|
break
|
||||||
|
|
||||||
|
elif response.status_code == 400:
|
||||||
|
logging.error(f"Bad request: {response.text}")
|
||||||
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
logging.error(f"Error: {response.status_code} - {response.text}")
|
||||||
|
break
|
||||||
|
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
logging.exception(f"Request failed: {e}")
|
||||||
|
break
|
||||||
|
|
||||||
|
return all_comments
|
||||||
|
|
||||||
|
|
||||||
|
def save_to_json(data, filename="comments.json"):
|
||||||
|
with open(filename, "w", encoding="utf-8") as f:
|
||||||
|
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
post_id = None
|
||||||
|
|
||||||
|
comments = fetch_comments(post_id=post_id)
|
||||||
|
|
||||||
|
logging.info(f"Total comments fetched: {len(comments)}")
|
||||||
|
|
||||||
|
for c in comments[:3]:
|
||||||
|
logging.info("-" * 40)
|
||||||
|
logging.info(f"Author: {c.get('author_name')}")
|
||||||
|
logging.info(f"Content: {c.get('content', {}).get('rendered')}")
|
||||||
|
|
||||||
|
save_to_json(comments)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user