Esempio n. 1
0
def get_project_downloads_in_between_dates(project_name: str,
                                           start_date: pendulum.DateTime,
                                           end_date: pendulum.DateTime) -> int:
    query_string = PROJECT_DOWNLOADS_BETWEEN_DATES.format(
        project_name=project_name,
        start_date=start_date.format("YYYYMMDD"),
        end_date=end_date.format("YYYYMMDD"),
        table=settings.BQ_PYPI_DOWNLOADS_TABLE,
    )
    result = _do_downloads_query(query_string)
    if not result:
        return -1
    return result[project_name]
Esempio n. 2
0
def format_datetime(dt: pendulum.DateTime) -> str:
    """Format date and time by pattern.

    Args:
        dt: DateTime instance to format.
    """
    return dt.format("ddd, MMM D, YYYY HH:mm:ss zz", locale="en")
Esempio n. 3
0
def query_pushshift(
    limit: int,
    after: pendulum.DateTime,
    before: pendulum.DateTime,
    subreddit: str,
    query: str = "",
    comments_num: str = ">0",
) -> typ.Any:
    """Given search parameters, query pushshift and return JSON."""

    # https://github.com/pushshift/api

    # no need to pass different limit params beyond 100 (Pushshift's limit)
    # as it creates unnecessary keys in get_JSON cache
    if limit >= PUSHSHIFT_LIMIT:
        limit_param = f"limit={PUSHSHIFT_LIMIT}&"
    else:
        limit_param = f"limit={limit}&"

    after_human = after.format("YYYY-MM-DD HH:mm:ss")
    before_human = before.format("YYYY-MM-DD HH:mm:ss")
    critical(f"******* between {after_human} and {before_human}")
    after_timestamp = after.int_timestamp
    before_timestamp = before.int_timestamp
    debug(f"******* between {after_timestamp} and {before_timestamp}")

    optional_params = ""
    if after:
        optional_params += f"&after={after_timestamp}"
    if before:
        optional_params += f"&before={before_timestamp}"
    if comments_num:
        # I prefer `comments_num`, but Reddit uses poorly
        # named `num_comments`
        optional_params += f"&num_comments={comments_num}"
    # this can be use to remove typ.any message with "removed"
    # see earlier commits for full functionality
    # optional_params += f"&selftext:not=[removed]"

    pushshift_url = (f"https://api.pushshift.io/reddit/submission/search/"
                     f"?{limit_param}subreddit={subreddit}{optional_params}")
    print(f"{pushshift_url=}")
    pushshift_data = web_utils.get_JSON(pushshift_url)["data"]
    if len(pushshift_data) != 100:
        print(f"short on some entries {len(pushshift_data)}")
        # breakpoint()
    return pushshift_data
Esempio n. 4
0
 async def stats_request(self, day: pendulum.DateTime, send_command):
     logger.info(f'Getting stats info for {day}')
     try:
         markdown_result = await self.local_stats_handler(day)
     except Exception:
         logger.exception('Error during stats request')
         await send_command('Error during request stats')
         return
     day = day.format('DD_MM_YYYY')
     markup = Markup([[
         InlineKeyboardButton(text='check', callback_data=f'check_cb {day}')
     ], [
         InlineKeyboardButton(text='clear', callback_data=f'clear_cb {day}')
     ]])
     await send_command('\n'.join(markdown_result),
                        parse_mode='Markdown',
                        reply_markup=markup.to_json())
Esempio n. 5
0
    def get_wellness(self, date: pendulum.DateTime) -> requests.Response:
        """Get wellness data for a given date

        Parameters
        ----------
        date
            Date for which you want to fetch wellness data

        Returns
        -------
        requests.Response
            Response content of the request to Garmin Connect
        """

        response = self.get(
            url=config["wellness"]["endpoint"].format(
                date=date.format("YYYY-MM-DD")),
            err_message=f"Failed to fetch wellness data for date {date!r}.",
            tolerate=tuple(config["wellness"]["tolerate"]),
        )
        return response
Esempio n. 6
0
def dtformat(datetime: pendulum.DateTime, format_str: str) -> str:
    """Return the given datetime as a formatted string."""
    return datetime.format(format_str)