Skip to content

Commit e3637db

Browse files
Add files via upload
1 parent 8078578 commit e3637db

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

views.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
# number of views and unique visitors for all public repos.
33
# Any public repo can be excluded by adding its name to the
44
# 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
66
# 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
88
# scheduling code is located in .github/workflows/views.yml
99

1010
import os
1111
import requests
1212
import csv
13-
from datetime import datetime
13+
from datetime import datetime, timedelta
1414
from github import Github
1515

1616
def fetch_traffic_stats(repo_url, headers):
@@ -62,28 +62,26 @@ def main():
6262

6363
try:
6464
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)
8785
except Exception as e:
8886
print(f"Failed to fetch traffic stats for repository '{repo_name}': {str(e)}")
8987

0 commit comments

Comments
 (0)