def test_subcommand_with_cache(self): # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall" mocked_response = """{ "data": [ {"category": "without_mirrors", "date": "2018-11-01", "downloads": 2295765} ], "package": "pip", "type": "overall_downloads" }""" expected_output = """ | category | downloads | |-----------------|----------:| | without_mirrors | 2,295,765 | Date range: 2018-11-01 - 2018-11-01 """ # Act with requests_mock.Mocker() as m: m.get(mocked_url, text=mocked_response) # First time to save to cache pypistats.overall(package) # Second time to read from cache output = pypistats.overall(package) # Assert assert output.strip() == expected_output.strip()
def test_overall_tabular_start_date(self): # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall" mocked_response = """{ "data": [ {"category": "without_mirrors", "date": "2018-11-01", "downloads": 2295765}, {"category": "without_mirrors", "date": "2018-11-02", "downloads": 2297591} ], "package": "pip", "type": "overall_downloads" }""" expected_output = """ | category | downloads | |-----------------|----------:| | without_mirrors | 2,297,591 | """ # Act with requests_mock.Mocker() as m: m.get(mocked_url, text=mocked_response) output = pypistats.overall(package, mirrors=False, start_date="2018-11-02") # Assert self.assertEqual(output.strip(), expected_output.strip())
def get_weekly_downloads(package): try: data = pypistats.overall(package, total=False, format="pandas") results = data[data.category != 'Total'] results['Date'] = pd.to_datetime(results['date']) results = pd.DataFrame( results.groupby(pd.Grouper( key='Date', freq='W-SUN'))['downloads'].sum().reset_index()) results['Package'] = package return results except Exception as foo: st.write("Invalid package", package, foo) return pd.DataFrame()
def update(self, repo=None): """Update repo download file(s). Description ----------- Update the local stored file with daily downloads for the specified repos. Parameters ---------- repo : list of Strings, (Default: None) None : Take all available pypi repos for the username. Returns ------- None. """ if (repo is not None) and ('str' in str(type(repo))): repo = [repo] # Extract all repos repos = self._get_repo_names_from_git() if (repo is not None): repos = repo if not np.any(np.isin(repos, repo)): raise ValueError('[pypiplot] >Error: repos [%s] does not exists or is private.' %(repo)) if self.verbose>=3: print('[pypiplot] >Start updating..') for repo in repos: try: if self.verbose>=3: print('[pypiplot] >[%s]' %(repo)) status = True df = pypistats.overall(repo, total=True, format="pandas") df.dropna(inplace=True) df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d') df = df.sort_values("date") df.reset_index(drop=True, inplace=True) del df['percent'] # Merge with any on disk pathname = os.path.join(self.savepath, repo + '.csv') if os.path.isfile(pathname): # Read repo from disk df_disk = read_repo_counts_from_disk(pathname, self.sep) # Merge with latest counts df, status = add_new_counts_to_repo(df, df_disk, repo, verbose=self.verbose) # Write to disk if status: if self.verbose>=3: print('[pypiplot] >Write to disk: [%s]' %(pathname)) df.to_csv(pathname, index=False, sep=self.sep) except: if self.verbose>=1: print('[pypiplot] >Skip [%s] because could not retrieve statistics from Pypi.' %(repo))
def overall(args): # pragma: no cover if args.mirrors in ["with", "without"]: args.mirrors = args.mirrors == "with" print( pypistats.overall( args.package, mirrors=args.mirrors, start_date=args.start_date, end_date=args.end_date, format=args.format, total="daily" if args.daily else ("monthly" if args.monthly else "all"), ))
def test_format_numpy(self): # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall" mocked_response = SAMPLE_RESPONSE_OVERALL expected_output = "[['without_mirrors' 4593356]]" # Act with requests_mock.Mocker() as m: m.get(mocked_url, text=mocked_response) output = pypistats.overall(package, format="numpy") # Assert assert isinstance(output, numpy.ndarray) assert str(output) == expected_output
def overall(args: argparse.Namespace) -> None: # pragma: no cover if args.mirrors in ["with", "without"]: args.mirrors = args.mirrors == "with" print( pypistats.overall( args.package, mirrors=args.mirrors, start_date=args.start_date, end_date=args.end_date, format=args.format, total="daily" if args.daily else ("monthly" if args.monthly else "all"), color="no", # Coloured percentages not really helpful here verbose=args.verbose, ))
def fetch_and_plot(name): """A little helper method to do it all and make it pretty.""" data = pypistats.overall(name, total=True, format="pandas") data = data.groupby("category").get_group("without_mirrors").sort_values( "date") data.plot(x="date", y="downloads", figsize=(15, 5), marker="o") plt.xticks( rotation=45, horizontalalignment="right", fontweight="light", fontsize="medium", ) plt.title("Downloads for {}".format(name)) plt.tight_layout() return plt.show()
def test_format_numpy(self) -> None: # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall" mocked_response = SAMPLE_RESPONSE_OVERALL expected_output = """ [['with_mirrors' '100.00%' 3587357] ['without_mirrors' '99.22%' 3559451] ['Total' None 3587357]] """ # Act respx.get(mocked_url).respond(content=mocked_response) output = pypistats.overall(package, format="numpy") # Assert assert isinstance(output, numpy.ndarray) assert str(output).strip() == expected_output.strip()
def test_format_pandas(self): # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall" mocked_response = SAMPLE_RESPONSE_OVERALL expected_output = """ category downloads 0 without_mirrors 4593356 """ # Act with requests_mock.Mocker() as m: m.get(mocked_url, text=mocked_response) output = pypistats.overall(package, format="pandas") # Assert assert isinstance(output, pandas.DataFrame) assert str(output).strip() == expected_output.strip()
def test_format_pandas(self) -> None: # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall" mocked_response = SAMPLE_RESPONSE_OVERALL expected_output = """ category percent downloads 0 with_mirrors 100.00% 3587357 1 without_mirrors 99.22% 3559451 2 Total None 3587357 """ # Act respx.get(mocked_url).respond(content=mocked_response) output = pypistats.overall(package, format="pandas") # Assert assert isinstance(output, pandas.DataFrame) assert str(output).strip() == expected_output.strip()
def pypi_downloads(package="name_of_the_package_on_pypi"): """Access the downloads history from pypi Examples --------- >>> import popularipy >>> data = popularipy.pypi_downloads("neurokit2") >>> data.plot(x="Date") """ data = pypistats.overall(package, total=True, format="pandas") # process data = data.groupby("date").sum().sort_values("date").reset_index() data = data.rename(columns={"downloads": "Downloads"}) data["Trend"] = nk.fit_loess(data["Downloads"]) data["Date"] = pd.to_datetime(data["date"]).dt.strftime('%d %b %Y') data = data.drop("date", axis=1) return data
def main(): parser = argparse.ArgumentParser() parser.add_argument('--packages', nargs='+', required=True) parsed = parser.parse_args() metrics = [] for package in parsed.packages: pypi_data = json.loads(pypistats.recent(package, format='json'))['data'] metrics.append(make_metric(package, 'last_day', pypi_data['last_day'])) metrics.append( make_metric(package, 'last_week', pypi_data['last_week'])) metrics.append( make_metric(package, 'last_month', pypi_data['last_month'])) overall_data = json.loads(pypistats.overall(package, format='json'))['data'] total_downloads = sum(val['downloads'] for val in overall_data) metrics.append(make_metric(package, 'all_time', total_downloads)) print(json.dumps(metrics))
def test_overall_tabular_end_date(self): # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall" mocked_response = SAMPLE_RESPONSE_OVERALL expected_output = """ | category | downloads | |-----------------|----------:| | without_mirrors | 2,295,765 | Date range: 2018-11-01 - 2018-11-01 """ # Act with requests_mock.Mocker() as m: m.get(mocked_url, text=mocked_response) output = pypistats.overall(package, mirrors=False, end_date="2018-11-01") # Assert assert output.strip() == expected_output.strip()
def test_overall_tabular_end_date(self) -> None: # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall?&mirrors=false" mocked_response = SAMPLE_RESPONSE_OVERALL expected_output = """ | category | percent | downloads | |:----------------|--------:|----------:| | with_mirrors | 100.00% | 2,100,139 | | without_mirrors | 99.21% | 2,083,472 | | Total | | 2,100,139 | Date range: 2020-05-01 - 2020-05-01 """ # Act respx.get(mocked_url).respond(content=mocked_response) output = pypistats.overall(package, mirrors=False, end_date="2020-05-01") # Assert assert output.strip() == expected_output.strip()
def test_overall_tabular_start_date(self): # Arrange package = "pip" mocked_url = "https://pypistats.org/api/packages/pip/overall?&mirrors=false" mocked_response = SAMPLE_RESPONSE_OVERALL expected_output = """ | category | percent | downloads | | --------------- | ------: | --------: | | with_mirrors | 100.00% | 1,487,218 | | without_mirrors | 99.24% | 1,475,979 | | Total | | 1,487,218 | Date range: 2020-05-02 - 2020-05-02 """ # Act respx.get(mocked_url).respond(content=mocked_response) output = pypistats.overall(package, mirrors=False, start_date="2020-05-02") # Assert assert output.strip() == expected_output.strip()
database = 'prody' mirror = False base_date = "2011-10-01" base_number = 2135029 # set up time elapse from yesterday to today today = date.today() str_today = today.strftime('%Y-%m-%d') yesterday = date.today() - timedelta(days=1) str_yesterday = yesterday.strftime('%Y-%m-%d') # retrieve download counts for yesterday ret = pis.overall(package, mirrors=mirror, start_date=str_yesterday, end_date=str_today, format='numpy') n_downloads = ret[0][-1] # connecting to the database connection = sql.connect(host=host, user=user, passwd=passwd, database=database) crsr = connection.cursor() # create the main table (if not already exists) in the database sql_cmd = 'CREATE TABLE IF NOT EXISTS downloads (date DATE PRIMARY KEY, number INTEGER);' crsr.execute(sql_cmd)
def get_stats(package): return loads(pypistats.overall(package, mirrors=True, format="json"))
import os import pypistats import pandas as pd cwd = os.getcwd() trackpath = f'{cwd}/doc/source/tracking/pypistats/' downloadfn = "downloads_data.csv" sysdownloadfn = "sys_downloads_data.csv" downloads = pypistats.overall("icepyx", format="pandas").drop(columns=['percent']) downloads = downloads[downloads.category != "Total"] # try: exist_downloads = pd.read_csv(trackpath + downloadfn) # .drop(columns=['percent']) # exist_downloads = exist_downloads[exist_downloads.category != "Total"] dl_data = downloads.merge(exist_downloads, how='outer', on=['category', 'date', 'downloads']).reindex() # except: # dl_data = downloads dl_data.to_csv(trackpath + downloadfn, index=False) sysdownloads = pypistats.system("icepyx", format="pandas").drop(columns=['percent']) sysdownloads = sysdownloads[sysdownloads.category != "Total"] # try: exist_sysdownloads = pd.read_csv(trackpath +