|
2 | 2 | # number of views and unique visitors for all public repos. |
3 | 3 | # Any public repo can be excluded by adding its name to the |
4 | 4 | # exclude_repos list in the script. Once the data has been |
5 | | -# extracted it will be appened to the views.csv file that is |
| 5 | +# extracted it will append to the views.csv file that is |
6 | 6 | # located in the data directory of the main branch. This file |
7 | | -# is scheduled to run once a day at midnight using cron. The |
| 7 | +# is scheduled to run once a day at 12:30am using cron. The |
8 | 8 | # scheduling code is located in .github/workflows/views.yml |
9 | 9 |
|
10 | 10 | import os |
11 | 11 | import requests |
12 | 12 | import csv |
13 | | -from datetime import datetime |
| 13 | +from datetime import datetime, timedelta |
14 | 14 | from github import Github |
15 | 15 |
|
16 | 16 | def fetch_traffic_stats(repo_url, headers): |
@@ -62,28 +62,26 @@ def main(): |
62 | 62 |
|
63 | 63 | try: |
64 | 64 | traffic_stats = fetch_traffic_stats(repo_url, headers) |
65 | | - if traffic_stats: |
66 | | - latest_views = traffic_stats[-1:] |
67 | | - |
68 | | - print(repo_name, latest_views) |
69 | | - |
70 | | - if not latest_views: |
71 | | - current_date = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') |
72 | | - latest_views = [{'timestamp': current_date, 'count': 0, 'uniques': 0}] |
73 | | - |
74 | | - datetime_object = datetime.strptime(latest_views[0]['timestamp'], '%Y-%m-%dT%H:%M:%SZ') |
75 | | - date_only = datetime_object.date() |
76 | | - |
77 | | - if date_only != datetime.now().date(): |
78 | | - current_date = datetime.utcnow().strftime('%Y-%m-%d') |
79 | | - latest_views = [{'timestamp': current_date, 'count': 0, 'uniques': 0}] |
80 | | - date_only = current_date |
81 | | - |
82 | | - views = latest_views[0]['count'] |
83 | | - unique_visitors = latest_views[0]['uniques'] |
84 | | - |
85 | | - result = f"{date_only},{repo_name},{views},{unique_visitors}" |
86 | | - data.append(result) |
| 65 | + |
| 66 | + check_dates_list = [] |
| 67 | + today = datetime.now().date() |
| 68 | + yesterday = today - timedelta(days=1) |
| 69 | + |
| 70 | + for views_stats in traffic_stats: |
| 71 | + date_object = datetime.strptime(views_stats['timestamp'], '%Y-%m-%dT%H:%M:%SZ').date() |
| 72 | + check_dates_list.append(date_object) |
| 73 | + if date_object == yesterday: |
| 74 | + date_only = yesterday |
| 75 | + views = views_stats['count'] |
| 76 | + unique_visitors = views_stats['uniques'] |
| 77 | + |
| 78 | + if yesterday not in check_dates_list: |
| 79 | + date_only = yesterday |
| 80 | + views = 0 |
| 81 | + unique_visitors = 0 |
| 82 | + |
| 83 | + result = f"{date_only},{repo_name},{views},{unique_visitors}" |
| 84 | + data.append(result) |
87 | 85 | except Exception as e: |
88 | 86 | print(f"Failed to fetch traffic stats for repository '{repo_name}': {str(e)}") |
89 | 87 |
|
|
0 commit comments