Пример #1
0
async def fetch_cafe_post_published_count(keyword, start_date: datetime.date, end_date: datetime.date):
    """
    네이버 카페 월 발행량 가져오기(기간별)
    """
    headers = {
        'authority': 'apis.naver.com',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="90", "Google Chrome";v="90"',
        'accept': 'application/json, text/plain, */*',
        'x-cafe-product': 'pc',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
        'content-type': 'application/json;charset=UTF-8',
        'origin': 'https://cafe.naver.com',
        'sec-fetch-site': 'same-site',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://cafe.naver.com/ca-fe/home/search/articles?q=%EA%B0%80%EC%9C%84&pr=3',
        'accept-language': 'en',
    }

    data = {
        "query": keyword,
        "page": 1,
        "sortBy": 0,
        "period": [start_date.strftime("%Y%m%d"), end_date.strftime("%Y%m%d")]
    }

    data = json.dumps(data)

    async with aiohttp.ClientSession() as session:
        async with session.post('https://apis.naver.com/cafe-home-web/cafe-home/v1/search/articles', headers=headers, data=data.encode('utf-8')) as response:
            res = await response.json()
    return int(res['message']['result']['totalCount'])
Пример #2
0
def add_new_sheet_element(date: datetime.date, name: str, amount: float,
                          start_range: str):
    month = date.strftime("%B")
    values = [[date.strftime('%d/%m/%Y'), name, amount]]
    updated = SheetService().write_append_sheet(SPREADSHEET_ID,
                                                f'{month}!{start_range}',
                                                values)
    return updated
Пример #3
0
def get_todoist_formatted_string_from_datetime(dt: datetime.date):
    """
    Returns a todoist API accepted string encoding for a given date/date-time
    :param dt:
    :return:
    """
    logging.debug("ALIVE. got:{}".format(dt))
    if isinstance(dt, datetime):
        logging.debug("Formatting {} with {}".format(dt,
                                                     todoist_datetime_format))
        return dt.strftime(todoist_datetime_format)

    if isinstance(dt, date):
        logging.debug("Formatting {} with {}".format(dt, todoist_date_format))
        return dt.strftime(todoist_date_format)
Пример #4
0
    def move_from_archive_to_emission(self, program, date: datetime.date):
        source = settings.ARCHIVE_SERVER_UPLOAD_DIRECTORY + program.normalized_name(
        ) + "/" + program.get_filename_for_date(date.strftime("%Y%m%d"))

        # Replacement is for scp
        destination = "\"\'" + settings.UPLOAD_SERVER_UPLOAD_DIRECTORY + program.get_filename_for_date(
            date.strftime("%Y%m%d")).replace(date.strftime("%Y%m%d"),
                                             "") + "\'\""

        # command may error if there is no upload, but we don't care
        command = "scp,{}@{}:{},{}@{}:{}".format(
            settings.ARCHIVE_SERVER_USERNAME, settings.ARCHIVE_SERVER_IP,
            source, settings.UPLOAD_SERVER_USERNAME, settings.UPLOAD_SERVER_IP,
            destination)
        print("Running:" + command)
        subprocess.run(command.split(","))
 def get_movies(self, session_date: datetime.date):
     request = f"""
         SELECT movie.id, movie.title, movie.duration
         FROM movie
         INNER JOIN session ON movie.id = session.movie_title_id
         WHERE session.date = ?
     """
     return self.data_base.select_all(request, (session_date.strftime('%d.%m.%Y'),))
Пример #6
0
def format_date(date: datetime.date) -> str:
    """
    Convert date object to string with format 'DD-MM-YYYY'.

    :param date: Date as datetime object.
    :return: Date in string type.
    """

    return date.strftime("%d-%m-%Y")
Пример #7
0
def read_weather_for_state_for_date(bucket: Bucket, bucket_raw_base_path: str,
                                    selected_state: str, date: datetime.date):
    yyyymmdd: str = date.strftime("%Y%m%d")
    blob = bucket.blob(
        f"{bucket_raw_base_path.format(date=yyyymmdd)}/{selected_state}.json.gz"
    )
    try:
        return json.loads(gunzip_bytes(blob.download_as_string()))
    except NotFound:
        return None
Пример #8
0
def getting_shutter_open_time_info(
    start_date: datetime.date,
    end_date: datetime.date,
    dome_status_id: int,
    telescope_id: int,
) -> Tuple:
    """This function retrieves information from the dome_shutter_open_time table in the Dashboard database.
     The start date and end date give you a time range, with the start date inclusive and the end date exclusive
     from the query, meaning it doesn't include the final day.
    :param start_date: the starting date(inclusive) for your query
    :param end_date: the ending date(exclusive) for your query
    :param dome_status_id: The dome status id tells us if the dome is open or closed, with 1 representing OPEN and
    2 representing CLOSED
    :param telescope_id: The id of each of the steerable telescopes to identify it based on its name
    :return: A tuple of the results from the database query
    """
    start_time = start_date.strftime("%Y-%m-%d") + " 12:00"
    end_time = end_date.strftime("%Y-%m-%d") + " 12:00"

    with connection.cursor(
            MySQLdb.cursors.DictCursor) as shutter_open_time_cursor:
        mysql_statement = """SELECT 
                                converted_julian_date,
                                telescope_id,
                                dome_status_id 
                        FROM dome_shutter_open_time
                        WHERE converted_julian_date >= %(start_date)s
                        and converted_julian_date < %(end_date)s
                        and dome_status_id= %(dome_status_id)s
                        and telescope_id=%(telescope_id)s"""

        shutter_open_time_cursor.execute(
            mysql_statement,
            dict(
                start_date=start_time,
                end_date=end_time,
                dome_status_id=dome_status_id,
                telescope_id=telescope_id,
            ),
        )
        results = shutter_open_time_cursor.fetchall()
        return results
Пример #9
0
def render_score_page(request, page: str, date: datetime.date, title: str):
    """Render generic score page.
    """
    games = Game.objects.filter(game_date=date.strftime("%b %d, %Y"))

    # Validate date input
    if request.method == 'POST':
        form = DateForm(request.POST)
        date_input = parser.parse(form.data.get('date')).date()

        if form.is_valid():
            return redirect('main:score', date=date_input.strftime("%m-%d-%Y"))

    context = {
        'title': title,
        'date': date.strftime("%b %d, %Y"),
        'games': games,
        'closest_date': Game.get_closest_game_date(date).strftime("%m-%d-%Y")
    }
    return render(request, page, context)
Пример #10
0
 def part_due_date_analysis(self, part: Part,
                            ref_date: datetime.date) -> DueDateAnalysis:
     cache_key = ref_date.strftime("%d%m%y") + str(hash(part))
     cached = self.parts_due_date_cache.get(cache_key, None)
     if cached is None:
         due_date_analysis = DueDateValidator.part_status_on_date(
             part, ref_date)
         self.parts_due_date_cache[cache_key] = due_date_analysis
         return due_date_analysis
     else:
         return cached
Пример #11
0
def sensible_date2str(x: datetime.date) -> str:
    """
    Returns string of the form:
    '15-Dec-2012    [Saturday]'
    """
    #     if x.strftime("%d")[0] == '1': return x.strftime("%A %-dth %B %Y")
    #     elif x.strftime("%d")[-1] == '1': return x.strftime("%A %-dst %B %Y")
    #     elif x.strftime("%d")[-1] == '2': return x.strftime("%A %-dnd %B %Y")
    #     elif x.strftime("%d")[-1] == '3': return x.strftime("%A %-drd %B %Y")
    #     else : return x.strftime("%A %-dth %B %Y")
    return x.strftime("%d-%b-%Y    [%A]")
Пример #12
0
 def component_due_date_analysis(
         self, component: Component,
         ref_date: datetime.date) -> DueDateAnalysis:
     cache_key = ref_date.strftime("%d%m%y") + str(hash(component))
     cached = self.component_due_date_cache.get(cache_key, None)
     if cached is None:
         due_date_analysis = DueDateValidator.component_status_on_date(
             component, ref_date)
         self.component_due_date_cache[cache_key] = due_date_analysis
         return due_date_analysis
     else:
         return cached
Пример #13
0
    def order_cake(
        self,
        contact_id: int,
        address: str,
        delivery_date: datetime.date,
        content: str,
    ) -> int:
        """https://www.amocrm.ru/developers/content/crm_platform/leads-api"""
        product_field_id = 608539
        address_field_id = 608543
        order_date_field_id = 472649

        data = self._send_post(
            "api/v4/leads",
            [{
                "pipeline_id":
                3428428,
                "created_by":
                self.ROBOT_ID,
                "custom_fields_values": [
                    {
                        "field_id": product_field_id,
                        "values": [{
                            "value": content
                        }],
                    },
                    {
                        "field_id": address_field_id,
                        "values": [{
                            "value": address
                        }],
                    },
                    {
                        "field_id":
                        order_date_field_id,
                        "values": [
                            # send as unix timestamp
                            {
                                "value": int(delivery_date.strftime("%s"))
                            }
                        ],
                    },
                ],
                "_embedded": {
                    "contacts": [{
                        "id": contact_id,
                    }]
                },
            }],
        )
        order_id = data["_embedded"]["leads"][0]["id"]
        return order_id
Пример #14
0
 def __init__(self,
              repository: str,
              date: datetime.date = datetime.today().date(),
              ports: Tuple[str] = ('443', ),
              cpu_cores: int = 1):
     self._repository = repository
     self._date = date
     self._ports = (ports, ) if isinstance(ports, str) else ports
     self._cpu_cores = cpu_cores
     self.__date_id = date.strftime('%Y%m%d')
     log.info(
         'RapidDatasetManager initialized with repository=%s, date=%s, ports=%s',
         repository, date, ports)
Пример #15
0
def extract_raw_weather_for_county_for_date(base_url: str, api_key: str,
                                            selected_state: str, county: str,
                                            date: datetime.date):
    query = parse.quote(f"{county}, {selected_state}")
    full_url = base_url.format(key=api_key,
                               q=query,
                               dt=date.strftime('%Y-%m-%d'))
    response = requests.get(full_url)
    result = response.json()
    # Sanity check result
    if "error" in result.keys():
        raise RuntimeError(
            f"Error encountered for {county}, {selected_state} on {date}: {result['error']['message']}"
        )
    return result
Пример #16
0
def build_html_for_crime_type(url: str, crime_type: str,
                              filter_month: datetime.date,
                              html_template_body: str):
    # check its not too big
    # https://developers.google.com/maps/documentation/maps-static/start#url-size-restriction
    print(f'\n\n"{crime_type}"... has string length = {len(url)}')
    if len(url) <= 8192:
        # create the body html
        pagedict = {
            'filter_month': filter_month.strftime('%b-%Y'),
            'crime_type': crime_type,
            'url': url
        }
        return html_template_body.format(**pagedict)
    else:
        return None
Пример #17
0
async def list_appointments(
    first_name: str,
    last_name: str,
    dob: datetime.date,
    last_4_ssn: int,
    **kwargs,
):
    """List existing booked appointments."""
    payload = {
        "FirstName": first_name,
        "LastName": last_name,
        "DateOfBirth": dob.strftime("%m/%d/%Y"),
        "Last4Ssn": last_4_ssn,
    }

    async with aiohttp.ClientSession() as session:
        res = await session.post(f"{BASE_API}/Booking",
                                 json=payload,
                                 headers=HTTP_HEADERS)
        return await res.json(content_type="text/plain")
Пример #18
0
def make_transaction_label(date: datetime.date) -> str:
    month_and_year = date.strftime('%m-%Y')
    period = '1ère' if date.day < 15 else '2nde'
    return 'pass Culture Pro - remboursement %s quinzaine %s' % (
        period, month_and_year)
Пример #19
0
 def datetime_to(self, data: datetime.date) -> str:
     """Convert to Datetime"""
     return "'{}'".format(data.strftime(self.format_datetime))
def get_reportfile_name(file_date: datetime.date, prefix='report') -> str:
    datestr = file_date.strftime('%Y.%m.%d')
    return f"{prefix}-{datestr}.html"
 def date_to_str(self, d: datetime.date) -> str:
     return d.strftime('%A  |  %b, %-d')
Пример #22
0
def build_index_name(index_prefix: str, d: datetime.date) -> str:
    today = d.strftime('%Y-%m-%d')
    index = '{0}-{1}'.format(index_prefix, today)
    print('Index: {0}'.format(index))
    return index
Пример #23
0
 def _fill_context_for_form_week(dt: datetime.date):
     context['week'] = get_lessons_for_week_view(dt)
     context['form'] = LessonsByDayForm({'date': dt.strftime('%d.%m.%Y')})
Пример #24
0
def make_transaction_label(date: datetime.date) -> str:
    month_and_year = date.strftime("%m-%Y")
    period = "1ère" if date.day < 15 else "2nde"
    return "pass Culture Pro - remboursement %s quinzaine %s" % (
        period, month_and_year)
Пример #25
0
 def date_to_string(self, dt: datetime.date, fmt="%Y-%m-%d") -> str:
     return dt.strftime(fmt)
Пример #26
0
async def hold(
    slot_id: int,
    first_name: str,
    last_name: str,
    dob: datetime.date,
    last_4_ssn: int,
    card_number: int,
    email_address: str,
    phone_number: int,
    appt_time: str,
    appt_duration: int,
    site_id: int,
    **kwargs,
):
    """Book an appointment with the TX DPS."""
    common = {
        "FirstName": first_name,
        "LastName": last_name,
        "DateOfBirth": dob.strftime("%m/%d/%Y"),
        "Last4Ssn": last_4_ssn,
    }

    hold_payload = {"SlotId": slot_id, **common}
    # TODO: be less biased in my default payload
    book_payload = {
        "CardNumber": card_number,
        "Email": email_address,
        "CellPhone": format_phone(phone_number),
        "HomePhone": "",
        "ServiceTypeId": DEFAULT_SERVICE_ID,
        "BookingDateTime": appt_time,
        "BookingDuration": appt_duration,
        "SpanishLanguage": "N",
        "SiteId": site_id,
        "SendSms": True,
        "AdaRequired": False,
        **common,
    }

    async with aiohttp.ClientSession() as session:
        # reserve the slot
        res = await session.post(f"{BASE_API}/HoldSlot",
                                 json=hold_payload,
                                 headers=HTTP_HEADERS)
        await res.json(content_type="text/plain")
        logging.debug("Booked appointment.")

        # if you already had an appointment for the same service, you need
        # to cancel the old one
        appts = await list_appointments(
            first_name=first_name,
            last_name=last_name,
            dob=dob,
            last_4_ssn=last_4_ssn,
        )
        collision = next(
            (b for b in appts if b["ServiceTypeId"] == DEFAULT_SERVICE_ID),
            None)
        if collision:
            endpoint = "RescheduleBooking"
        else:
            endpoint = "NewBooking"
        logging.info(f"Using endpoint: {endpoint}")

        # confirm appointment
        res = await session.post(f"{BASE_API}/{endpoint}",
                                 json=book_payload,
                                 headers=HTTP_HEADERS)
        return await res.json(content_type="text/plain")
Пример #27
0
 def datetime_to(self, data: datetime.date) -> str:
     return "'{}'".format(data.strftime(self.format_datetime))
Пример #28
0
 def to_url(self, value: datetime.date):
     return value.strftime('%Y-%m-%d')
Пример #29
0
def get_price(issuer_code: str, dt_left: datetime.date,
              dt_right: datetime.date) -> List[Price]:
    """
    Получить курс за временной отрезок.

    :param issuer_code: Код эмитента
    :param dt_left: Дата конца отрезка
    :param dt_right: Дата начала отрезка

    :raise: NotFoundIssuer

    >>> get_price('XXX', datetime.today().date(), datetime.today().date())
    Traceback (most recent call last):
    ...
    finam.NotFoundIssuer
    """

    if issuer_code.upper() not in ISSUER_LIST:
        raise NotFoundIssuer()

    # Индекс эмитента
    index = ISSUER_LIST.get(issuer_code.upper())[1]

    # Отрезок
    df, mf, yf, from_ = dt_left.day, dt_left.month - 1, dt_left.year, dt_left.strftime(
        '%d.%m.%Y')
    dt, mt, yt, to_ = dt_right.day, dt_right.month - 1, dt_right.year, dt_right.strftime(
        '%d.%m.%Y')

    # Детализация отрезка. Варианты
    # '1' - тики,    '2' - 1 мин., '3' - 5 мин., '4' - 10 мин.,  '5' - 15 мин.,
    # '6' - 30 мин., '7' - 1 час,  '8' - 1 день, '9' - 1 неделя, '10' - 1 месяц
    period = 8

    # Формат результата. Варианты 'txt', 'csv'
    result_ff = 'txt'

    # Формат даты результата. Варианты '1' — ггггммдд, '2' — ггммдд, '3' — ддммгг, '4' — дд/мм/гг, '5' — мм/дд/гг
    result_df = 3

    # Формат времени результата. Варианты '1' — ччммсс, '2' — ччмм, '3' — чч: мм: сс, '4' — чч: мм
    result_tf = 2

    # Московское время результата. Варианты '0' - не московское
    result_tm = 0

    # Время свечи. Варианты '0' — начала свечи, '1' — окончания свечи
    result_tc = 0

    # Разделитель колонок. Варианты
    # '1' — запятая, '2' — точка, '3' — точка с запятой, '4' — табуляция, '5' — пробел
    result_column_sep = 3

    # Разделитель разрядов значений. Варианты '1' — нет, '2' — точка, '3' — запятая, '4' — пробел, '5' — кавычка
    result_value_sep = 1

    # Получаемые данные в результате. Варианты
    # '1' — TICKER, PER, DATE, TIME, OPEN, HIGH, LOW, CLOSE, VOL
    # '2' — TICKER, PER, DATE, TIME, OPEN, HIGH, LOW, CLOSE
    # '3' — TICKER, PER, DATE, TIME, CLOSE, VOL
    # '4' — TICKER, PER, DATE, TIME, CLOSE
    # '5' — DATE, TIME, OPEN, HIGH, LOW, CLOSE, VOL
    # '6' — DATE, TIME, LAST, VOL, ID, OPER
    result_data = 5

    # Наличие заголовка в результате. Варианты '0' - нет, '1' - да
    result_w_head = 0

    url = f'http://export.finam.ru/result.txt?market=1&em={index}&code={issuer_code}&apply=0' \
          f'&df={df}&mf={mf}&yf={yf}&from={from_}' \
          f'&dt={dt}&mt={mt}&yt={yt}&to={to_}' \
          f'&p={period}&f=result&e=.{result_ff}&cn={issuer_code}&dtf={result_df}&tmf={result_tf}' \
          f'&MSOR={result_tc}&mstimever={result_tm}&sep={result_column_sep}&sep2={result_value_sep}' \
          f'&datf={result_data}&at={result_w_head}'

    result = []
    with urllib.request.urlopen(url) as file:
        for result_str in file.readlines():
            result_values = result_str.decode().split(';')
            result.append(
                Price(
                    datetime.strptime(result_values[0], "%d%m%y").date(),
                    Decimal(result_values[2]), Decimal(result_values[5])))

    return result
Пример #30
0
 def to_url(self, value: datetime.date) -> str:
     return value.strftime(self.format)