@@ -21,67 +21,102 @@ export async function fetchPyPIVersion(package_name: string, package_url: string
2121 }
2222}
2323
24+ const CACHE_EXPIRATION_TIME = 3600000 ; // 1 hour in milliseconds
25+
2426interface PypiStats {
25- last_day : number ;
26- last_month : number ;
27- last_week : number ;
27+ last_day : number | string ;
28+ last_week : number | string ;
29+ last_month : number | string ;
2830}
2931
3032interface PypiStatsApiResponse {
31- data : PypiStats ;
32- package : string ;
33- type : string ;
33+ data : PypiStats ;
34+ package : string ;
35+ type : string ;
3436}
3537
38+
39+ // Cache object for PyPI stats
40+ const pypiCache : { [ key : string ] : { data : PypiStatsApiResponse , timestamp : number } } = { } ;
41+
3642export async function fetchPyPIDownloadStats ( package_name : string ) : Promise < PypiStatsApiResponse > {
37- try {
38- const pypistats_url =
39- 'https://pypistats.org/api/packages/' + package_name + '/recent' ;
40- const response = await fetch ( pypistats_url , { mode : 'no-cors' } ) ;
41- const data = await response . json ( ) ;
42- return data ;
43- // return {
44- // data: {
45- // last_day: NaN,
46- // last_month: NaN,
47- // last_week: NaN
48- // },
49- // package: package_name,
50- // type: "recent_downloads"
51- // };
52- } catch ( error ) {
53- console . error ( 'Error fetching ' + package_name + ' download stats:' , error ) ;
54- return {
55- data : {
56- last_day : NaN ,
57- last_month : NaN ,
58- last_week : NaN ,
59- } ,
60- package : package_name ,
61- type : 'recent_downloads' ,
62- } ;
63- }
43+ const cacheKey = `pypi_stats_${ package_name } ` ; // Unique cache key for the package
44+ const currentTime = Date . now ( ) ;
45+
46+ // Check if the data is cached and valid (within expiration time)
47+ if ( pypiCache [ cacheKey ] && ( currentTime - pypiCache [ cacheKey ] . timestamp ) < CACHE_EXPIRATION_TIME ) {
48+ console . log ( 'Returning cached PyPI stats' ) ;
49+ return pypiCache [ cacheKey ] . data ; // Return cached data if valid
50+ }
51+
52+ try {
53+ const pypistats_url = 'https://pypistats.org/api/packages/' + package_name + '/recent' ;
54+ const response = await fetch ( pypistats_url , { mode : 'no-cors' } ) ;
55+ const data = await response . json ( ) ;
56+
57+ // Prepare the response object
58+ const result : PypiStatsApiResponse = {
59+ data : {
60+ last_day : data . data . last_day ?? '-' ,
61+ last_week : data . data . last_week ?? '-' ,
62+ last_month : data . data . last_month ?? '-' ,
63+ } ,
64+ package : package_name ,
65+ type : "recent_downloads"
66+ } ;
67+
68+ // Store the result in the cache with the current timestamp
69+ pypiCache [ cacheKey ] = { data : result , timestamp : currentTime } ;
70+
71+ return result ;
72+ } catch ( error ) {
73+ console . error ( 'Error fetching ' + package_name + ' download stats:' , error ) ;
74+ // In case of an error, return a default fallback object
75+ return {
76+ data : {
77+ last_day : '-' ,
78+ last_month : '-' ,
79+ last_week : '-'
80+ } ,
81+ package : package_name ,
82+ type : "recent_downloads"
83+ } ;
84+ }
6485}
6586
87+
6688interface GitHubStats {
67- n_stars : number ;
68- n_forks : number ;
69- n_watchers : number ;
89+ n_stars : number | string ;
90+ n_forks : number | string ;
91+ n_watchers : number | string ;
7092}
7193
94+ // A simple in-memory cache object
95+ const cache : { [ key : string ] : { data : GitHubStats , timestamp : number } } = { } ;
96+
7297export async function fetchGitHubStats ( github_org : string , github_repo : string ) : Promise < GitHubStats > {
73- const github_api_url = `https://api.github.com/repos/${ github_org } /${ github_repo } ` ;
74- const github_api = await fetch ( github_api_url ) ;
75- const github_data = await github_api . json ( ) ;
76-
77- return {
78- n_stars : github_data . stargazers_count ,
79- n_forks : github_data . forks_count ,
80- n_watchers : github_data . subscribers_count ,
81- } ;
82- // return {
83- // n_stars: NaN,
84- // n_forks: NaN,
85- // n_watchers: NaN
86- // }
98+ const cacheKey = `${ github_org } /${ github_repo } ` ; // Unique cache key for the repository
99+ const currentTime = Date . now ( ) ;
100+
101+ // Check if the data is cached and valid (within expiration time)
102+ if ( cache [ cacheKey ] && ( currentTime - cache [ cacheKey ] . timestamp ) < CACHE_EXPIRATION_TIME ) {
103+ console . log ( 'Returning cached data' ) ;
104+ return cache [ cacheKey ] . data ; // Return the cached data if valid
105+ }
106+
107+ const github_api_url = `https://api.github.com/repos/${ github_org } /${ github_repo } ` ;
108+ const github_api = await fetch ( github_api_url ) ;
109+ const github_data = await github_api . json ( ) ;
110+
111+ const result : GitHubStats = {
112+ n_stars : github_data . stargazers_count ?? '-' ,
113+ n_forks : github_data . forks_count ?? '-' ,
114+ n_watchers : github_data . subscribers_count ?? '-'
115+ } ;
116+
117+ // Store the result in the cache with the current timestamp
118+ cache [ cacheKey ] = { data : result , timestamp : currentTime } ;
119+
120+ return result ;
87121}
122+
0 commit comments