def get( self, sort: str = "date", dates: bool = True, output: Union[str, None] = None, ) -> str: """ Exposed function for getting data on our Facebook friends. @param sort: the column we want to sort by. Can be either of {date|name}. Default is `dates`. @param dates: boolean flag on do we want the dates column. Default is `True`. @param output: where do we want to write the return value, can be any of: {csv|json|/some/path.{json|csv}}. @return: either the data formatted as csv or json, or a success message about where was the data saved. """ data = self.data if sort == "name": data = data.sort_values(by="name") if not dates: data.reset_index(drop=True, inplace=True) return utils.df_to_file(output, data)
def get( self, kind: str = "private", channels: Union[str, List[str]] = None, cols: List[str] = None, output: str = "csv", ): """ Conversations. @param kind: one of private or group, depending on which messaging do you want to analyze. @param channels: channel names you want to filter for. @param cols: column names you want to include in the output. @param output: where do we want to write the return value, can be any of: {csv|json|/some/path.{json|csv}}. @return: either the data formatted as csv or json, or a success message about where was the data saved. """ data = getattr(self, kind) filtered_channels = (data.keys() if channels is None else list(set(data.keys()) & set(channels))) if not filtered_channels: return (f"Could not filter for these channels: {channels}. " f"Please double-check the channel names.") res = utils.stack_dfs( *[data.get(channel).data for channel in filtered_channels]) filtered_cols = (list(res.columns) if cols is None else [col for col in cols if col in res.columns]) if not filtered_cols: return (f"Could not filter for these columns: {cols}. " f"Please double-check the column names. Hint: if cols " f"is None, all columns will be returned. ") return utils.df_to_file(output, res[filtered_cols])
def test_df_to_file(self, sample_df): path = f"{os.path.dirname(os.path.realpath(__file__))}/test.csv" res = utils.df_to_file(path, sample_df) assert res == f"Data was written to {path}" os.unlink(path) path = f"{os.path.dirname(os.path.realpath(__file__))}/test.json" res = utils.df_to_file(path, sample_df) assert res == f"Data was written to {path}" os.unlink(path) path = "csv" res = utils.df_to_file(path, sample_df) assert isinstance(res, str) path = "json" res = utils.df_to_file(path, sample_df) assert isinstance(res, str) assert isinstance(json.loads(res), dict) res = utils.df_to_file("/home/whatever/a/gibberish", sample_df) assert res == "Directory does not exist: `/home/whatever/a`"
def get(self, output: Union[str, None] = None) -> str: """ Exposed function for getting data on people. @param output: where do we want to write the return value, an be any of: {csv|json|/some/path.{json|csv}}. @return: either the data formatted as csv or json, or a success message about where was the data saved. """ name = [person.name for person in self.data.values()] friend = [person.friend for person in self.data.values()] message_dir = [person.thread_path for person in self.data.values()] media_dir = [person.media_dir for person in self.data.values()] df = pd.DataFrame({ "name": name, "friend": friend, "message_dir": message_dir, "media_dir": media_dir, }) return utils.df_to_file(output, df)
def wrapper(*args, **kwargs: Any): res = func(*args, **kwargs) if not len(res): return return utils.df_to_file(kwargs.get("output"), res)