import requests import matplotlib.pyplot as plt from datetime import datetime, timedelta import sys import urllib.parse headers = { 'User-Agent': f'test_script/0.1 (wilr@seerinteractive.com) python/{sys.version_info.major}.{sys.version_info.minor}' } def get_pageviews(article, start_date, end_date): base_url = "https://wikimedia.org/api/rest_v1/metrics/pageviews/per-article/en.wikipedia/all-access/user" url = f"{base_url}/{article}/daily/{start_date}/{end_date}" response = requests.get(url, headers=headers) if response.status_code == 200: return response.json()['items'] print(f"API Error: {response.status_code}") print(f"Response content: {response.text}") return None def aggregate_weekly(daily_data): weekly_data = {} for item in daily_data: date = datetime.strptime(item['timestamp'], "%Y%m%d00") week_start = (date - timedelta(days=date.weekday())).strftime("%Y-%m-%d") if week_start not in weekly_data: weekly_data[week_start] = 0 weekly_data[week_start] += item['views'] return weekly_data def main(): # Prompt for Wikipedia URL wikipedia_url = input("Enter the Wikipedia page URL: ") article_title = urllib.parse.unquote(wikipedia_url.split("/")[-1].replace("_", " ")) # Prompt for number of months months = int(input("Enter the number of months of data you want: ")) # Set the date range end_date = datetime.now() start_date = end_date - timedelta(days=30*months) # Format dates for the API start_str = start_date.strftime("%Y%m%d") end_str = end_date.strftime("%Y%m%d") # Fetch Wikipedia data daily_data = get_pageviews(urllib.parse.quote(article_title.replace(" ", "_")), start_str, end_str) if daily_data: weekly_data = aggregate_weekly(daily_data) # Prepare data for plotting weeks = list(weekly_data.keys()) views = list(weekly_data.values()) # Create the plot plt.figure(figsize=(15, 6)) plt.plot(weeks, views, label='Wikipedia Weekly Views', color='blue') plt.xlabel("Week") plt.ylabel("Wikipedia Views") plt.title(f"Wikipedia Views for {article_title}") plt.xticks(rotation=45) plt.tight_layout() plt.legend() plt.show() else: print("Failed to fetch data.") if __name__ == "__main__": main()