Archived
102 lines
3.0 KiB
Python
102 lines
3.0 KiB
Python
import requests
|
|
import datetime
|
|
import sys
|
|
|
|
from dateutil import parser
|
|
import datetime
|
|
|
|
def get_plugin_info(slug):
|
|
"""Fetch plugin info from the WordPress API."""
|
|
url = f"https://api.wordpress.org/plugins/info/1.2/?action=plugin_information&request[slug]={slug}"
|
|
response = requests.get(url)
|
|
if response.status_code != 200:
|
|
print(f"Error: Unable to fetch data for plugin '{slug}'.")
|
|
return None
|
|
return response.json()
|
|
|
|
def calculate_security_score(plugin):
|
|
"""Calculate a security score (0-100) for a WordPress plugin."""
|
|
score = 0
|
|
|
|
# last updated
|
|
last_updated_str = plugin.get("last_updated")
|
|
if last_updated_str:
|
|
try:
|
|
last_updated = parser.parse(last_updated_str)
|
|
now = datetime.datetime.now(datetime.timezone.utc)
|
|
if last_updated.tzinfo is None:
|
|
last_updated = last_updated.replace(tzinfo=datetime.timezone.utc)
|
|
days_since_update = (now - last_updated).days
|
|
|
|
if days_since_update <= 90:
|
|
score += 40
|
|
elif days_since_update <= 180:
|
|
score += 30
|
|
elif days_since_update <= 365:
|
|
score += 20
|
|
else:
|
|
score += 10
|
|
except Exception as e:
|
|
print(f"Warning: Could not parse date '{last_updated_str}': {e}")
|
|
score += 0
|
|
else:
|
|
score += 0
|
|
|
|
# user rating
|
|
rating = plugin.get("rating")
|
|
if isinstance(rating, (int, float)):
|
|
score += (rating / 100) * 30
|
|
else:
|
|
score += 0
|
|
|
|
# active installs
|
|
installs = plugin.get("active_installs")
|
|
if isinstance(installs, int):
|
|
if installs >= 1_000_000:
|
|
score += 20
|
|
elif installs >= 100_000:
|
|
score += 15
|
|
elif installs >= 10_000:
|
|
score += 10
|
|
elif installs > 0:
|
|
score += 5
|
|
else:
|
|
score += 0
|
|
|
|
return round(score, 1)
|
|
|
|
|
|
def rate_plugin(slug):
|
|
plugin = get_plugin_info(slug)
|
|
if not plugin:
|
|
return
|
|
|
|
score = calculate_security_score(plugin)
|
|
name = plugin.get("name", slug)
|
|
last_updated = plugin.get("last_updated", "Unknown")
|
|
rating = plugin.get("rating", "Unknown")
|
|
installs = plugin.get("active_installs", "Unknown")
|
|
|
|
print(f"\n🔍 Security Rating for '{name}'")
|
|
print("=" * (25 + len(name)))
|
|
print(f"Last Updated: {last_updated}")
|
|
print(f"User Rating: {rating}/100")
|
|
print(f"Active Installs: {installs}")
|
|
print(f"→ Security Score: {score}/100")
|
|
|
|
if score >= 85:
|
|
print("Excellent — Actively maintained and trusted")
|
|
elif score >= 65:
|
|
print("Good — Generally safe but monitor updates")
|
|
elif score >= 45:
|
|
print("Moderate — May be outdated or less popular")
|
|
else:
|
|
print("Poor — High risk of security issues")
|
|
|
|
if __name__ == "__main__":
|
|
if len(sys.argv) < 2:
|
|
print("Usage: python plugin_security_rating.py <plugin-slug>")
|
|
sys.exit(1)
|
|
slug = sys.argv[1]
|
|
rate_plugin(slug)
|