ダウンロード機能の修正とモジュール化

ダウンロード機能の修正とモジュール化

import fitz  # PyMuPDF
from playsound import playsound
from gmail_utils import gmail_init, gmail_get_latest_unread_message_body
from pdf_downloader import find_preview_link, download_pdf

def save_email_body_to_text(body, filename="email_body.txt"):
    with open(filename, "w", encoding="utf-8") as file:
        file.write(body)

def count_pdf_characters(file_path):
    doc = fitz.open(file_path)
    text = ""
    for page in doc:
        text += page.get_text()
    return len(text)

def main():
    # Gmail API サービスを初期化
    service = gmail_init()
    
    # ラベル ID を指定して最新の未読メール本文とURLを取得
    body, urls = gmail_get_latest_unread_message_body(service, "ラベルID")
    
    # 未読メールがある場合は音声ファイルを再生
    if body:
        playsound('notice.wav')

        if urls:
            for url in urls:
                print(f"プレビューリンクを検索するURL: {url}")
                # プレビューリンクを取得
                preview_url = find_preview_link(url)
                if preview_url:
                    print(f"プレビューリンク: {preview_url}")
                    # プレビューリンクからPDFファイルのダウンロードを試みる
                    download_pdf(preview_url, file_path="downloaded_file.pdf")
                    
                    # PDFファイルから文字数をカウント
                    char_count = count_pdf_characters("downloaded_file.pdf")
                    print(f"PDF内の文字数: {char_count}")
                    
                    # 文字数が100文字以上の場合は別の音声ファイルを再生
                    if char_count >= 100:
                        playsound('notice_pdf.wav')

                else:
                    print("プレビューリンクが見つかりませんでした。")
        else:
            print("メールにURLが見つかりませんでした。")

        # メール本文をテキストファイルに保存
        save_email_body_to_text(body)
    else:
        print("未読メールはありません。")

if __name__ == "__main__":
    main()

としたらリンクがエラーになる

from gmail_utils import gmail_init, gmail_get_latest_unread_message_body
from pdf_downloader import find_preview_link, download_pdf

def save_email_body_to_text(body, filename="email_body.txt"):
    with open(filename, "w", encoding="utf-8") as file:
        file.write(body)

def main():
    # Gmail API サービスを初期化
    service = gmail_init()
    
    # ラベル ID を指定して最新の未読メール本文とURLを取得
    body, urls = gmail_get_latest_unread_message_body(service, "ラベルID")
    if urls:
        for url in urls:
            print(f"プレビューリンクを検索するURL: {url}")
            # プレビューリンクを取得
            preview_url = find_preview_link(url)
            if preview_url:
                print(f"プレビューリンク: {preview_url}")
                # プレビューリンクからPDFファイルのダウンロードを試みる
                download_pdf(preview_url, file_path="downloaded_file.pdf")
            else:
                print("プレビューリンクが見つかりませんでした。")
    else:
        print("メールにURLが見つかりませんでした。")

    # メール本文をテキストファイルに保存
    save_email_body_to_text(body)

if __name__ == "__main__":
    main()

だと問題ない

とりあえず

import fitz  # PyMuPDF
from playsound import playsound
from gmail_utils import gmail_init, gmail_get_latest_unread_message_body
from pdf_downloader import find_preview_link, download_pdf
import subprocess
import pygame
import time

def generate_and_play_audio_from_text(text):
    # JSONファイルを作成するためのcurlコマンド
    command_json = [
        "curl", "-s", "-X", "POST",
        "192.168.1.69:50021/audio_query?speaker=1",
        "--get", "--data-urlencode", f"text={text}"
    ]

    # 音声ファイルを作成するためのcurlコマンド
    command_audio = [
        "curl", "-s", "-H", "Content-Type: application/json", "-X", "POST",
        "-d", "@query.json", "192.168.1.69:50021/synthesis?speaker=1"
    ]

    # JSONファイルと音声ファイルを作成
    with open('query.json', 'w') as file:
        subprocess.run(command_json, stdout=file)
    with open('audio_output.wav', 'wb') as file:
        subprocess.run(command_audio, stdout=file)

    # Pygameで音声ファイルを再生
    pygame.init()
    pygame.mixer.init()
    sound = pygame.mixer.Sound("audio_output.wav")
    sound.play()
    while pygame.mixer.get_busy():
        time.sleep(0.1)

def count_pdf_characters(file_path):
    doc = fitz.open(file_path)
    text = ""
    for page in doc:
        text += page.get_text()
    return len(text)

def main():
    # Gmail API サービスを初期化
    service = gmail_init()
    
    # ラベル ID を指定して最新の未読メール本文とURLを取得
    body, urls = gmail_get_latest_unread_message_body(service, "ラベルID")
    
    # 未読メールがある場合は音声ファイルを再生
    if body:
        playsound('notice.wav')
        generate_and_play_audio_from_text(body)
        

        if urls:
            for url in urls:
                print(f"プレビューリンクを検索するURL: {url}")
                # プレビューリンクを取得
                preview_url = find_preview_link(url)
                if preview_url:
                    print(f"プレビューリンク: {preview_url}")
                    # プレビューリンクからPDFファイルのダウンロードを試みる
                    download_pdf(preview_url, file_path="downloaded_file.pdf")
                    
                    # PDFファイルから文字数をカウント
                    char_count = count_pdf_characters("downloaded_file.pdf")
                    print(f"PDF内の文字数: {char_count}")
                    
                    # 文字数が100文字以上の場合は別の音声ファイルを再生
                    if char_count >= 100:
                        playsound('notice_pdf.wav')

                else:
                    print("プレビューリンクが見つかりませんでした。")
        else:
            print("メールにURLが見つかりませんでした。")
    else:
        print("未読メールはありません。")

if __name__ == "__main__":
    main()

とすることで
メールの読み上げと
PDFのダウンロードができた
また長文のPDFに関してはPDFを見るように促すようにした

あとは顔を認識したら起動するようにする

まずはこれをモジュールにする

import fitz
from playsound import playsound
import subprocess
import pygame
import time

# テキストから音声を生成して再生する関数
def generate_and_play_audio_from_text(text):
    command_json = [
        "curl", "-s", "-X", "POST",
        "192.168.1.69:50021/audio_query?speaker=1",
        "--get", "--data-urlencode", f"text={text}"
    ]
    command_audio = [
        "curl", "-s", "-H", "Content-Type: application/json", "-X", "POST",
        "-d", "@query.json", "192.168.1.69:50021/synthesis?speaker=1"
    ]
    with open('query.json', 'w') as file:
        subprocess.run(command_json, stdout=file)
    with open('audio_output.wav', 'wb') as file:
        subprocess.run(command_audio, stdout=file)
    pygame.init()
    pygame.mixer.init()
    sound = pygame.mixer.Sound("audio_output.wav")
    sound.play()
    while pygame.mixer.get_busy():
        time.sleep(0.1)

# PDFファイルから文字数をカウントする関数
def count_pdf_characters(file_path):
    doc = fitz.open(file_path)
    text = ""
    for page in doc:
        text += page.get_text()
    return len(text)

# メールの処理を行う関数
def process_email(service, label_id):
    from gmail_utils import gmail_get_latest_unread_message_body
    from pdf_downloader import find_preview_link, download_pdf

    body, urls = gmail_get_latest_unread_message_body(service, label_id)
    if body:
        playsound('notice.wav')
        generate_and_play_audio_from_text(body)
        if urls:
            for url in urls:
                preview_url = find_preview_link(url)
                if preview_url:
                    download_pdf(preview_url, "downloaded_file.pdf")
                    char_count = count_pdf_characters("downloaded_file.pdf")
                    if char_count >= 100:
                        playsound('notice_pdf.wav')
                else:
                    print("プレビューリンクが見つかりませんでした。")
        else:
            print("メールにURLが見つかりませんでした。")
    else:
        print("未読メールはありません。")

として

email_processor.py

として保存

次に顔を検出したらこれを呼び出すようにする

Gmail読み上げとメールのお知らせを作る

Gmail読み上げとメールのお知らせを作る

from gmail_utils import gmail_init, gmail_get_latest_unread_message_body
from pdf_downloader import find_preview_link, download_pdf

def save_email_body_to_text(body, filename="email_body.txt"):
    with open(filename, "w", encoding="utf-8") as file:
        file.write(body)

def main():
    # Gmail API サービスを初期化
    service = gmail_init()
    
    # ラベル ID を指定して最新の未読メール本文とURLを取得
    body, urls = gmail_get_latest_unread_message_body(service, "ラベルID")
    if urls:
        for url in urls:
            print(f"プレビューリンクを検索するURL: {url}")
            # プレビューリンクを取得
            preview_url = find_preview_link(url)
            if preview_url:
                print(f"プレビューリンク: {preview_url}")
                # プレビューリンクからPDFファイルのダウンロードを試みる
                download_pdf(preview_url, file_path="downloaded_file.pdf")
            else:
                print("プレビューリンクが見つかりませんでした。")
    else:
        print("メールにURLが見つかりませんでした。")

    # メール本文をテキストファイルに保存
    save_email_body_to_text(body)

if __name__ == "__main__":
    main()

でテキストファイルの作成とPDF取得ができているので
お知らせメッセージの
notice.wavを再生する

GPTによれば

pip install playsound

でインストールし
コードを変更する

from playsound import playsound
from gmail_utils import gmail_init, gmail_get_latest_unread_message_body
from pdf_downloader import find_preview_link, download_pdf

def save_email_body_to_text(body, filename="email_body.txt"):
    with open(filename, "w", encoding="utf-8") as file:
        file.write(body)

def main():
    # Gmail API サービスを初期化
    service = gmail_init()
    
    # ラベル ID を指定して最新の未読メール本文とURLを取得
    body, urls = gmail_get_latest_unread_message_body(service, "ラベルID")
    
    # 未読メールがある場合は音声ファイルを再生
    if body:
        playsound('notice.wav')

        if urls:
            for url in urls:
                print(f"プレビューリンクを検索するURL: {url}")
                # プレビューリンクを取得
                preview_url = find_preview_link(url)
                if preview_url:
                    print(f"プレビューリンク: {preview_url}")
                    # プレビューリンクからPDFファイルのダウンロードを試みる
                    download_pdf(preview_url, file_path="downloaded_file.pdf")
                else:
                    print("プレビューリンクが見つかりませんでした。")
        else:
            print("メールにURLが見つかりませんでした。")

        # メール本文をテキストファイルに保存
        save_email_body_to_text(body)
    else:
        print("未読メールはありません。")

if __name__ == "__main__":
    main()

変更点は

def save_email_body_to_text(body, filename="email_body.txt"):
    with open(filename, "w", encoding="utf-8") as file:
        file.write(body)

で音声ファイルを読み込んでいること

Main.pyの中で

    # 未読メールがある場合は音声ファイルを再生
    if body:
        playsound('notice.wav')

を追加して音声を出している

次にPDFの文字数を調べて100文字以上なら
音声でPDFの内容を確認するように促す

あとはメール本文の読み上げ処理だけ

PyMuPDFを使い
文字数をカウント、文字数が100文字以上ならPDFを確認する音声を再生するようにコード変更

import fitz  # PyMuPDF
from playsound import playsound
from gmail_utils import gmail_init, gmail_get_latest_unread_message_body
from pdf_downloader import find_preview_link, download_pdf

def save_email_body_to_text(body, filename="email_body.txt"):
    with open(filename, "w", encoding="utf-8") as file:
        file.write(body)

def count_pdf_characters(file_path):
    doc = fitz.open(file_path)
    text = ""
    for page in doc:
        text += page.get_text()
    return len(text)

def main():
    # Gmail API サービスを初期化
    service = gmail_init()
    
    # ラベル ID を指定して最新の未読メール本文とURLを取得
    body, urls = gmail_get_latest_unread_message_body(service, "ラベルID")
    
    # 未読メールがある場合は音声ファイルを再生
    if body:
        playsound('notice.wav')

        if urls:
            for url in urls:
                print(f"プレビューリンクを検索するURL: {url}")
                # プレビューリンクを取得
                preview_url = find_preview_link(url)
                if preview_url:
                    print(f"プレビューリンク: {preview_url}")
                    # プレビューリンクからPDFファイルのダウンロードを試みる
                    download_pdf(preview_url, file_path="downloaded_file.pdf")
                    
                    # PDFファイルから文字数をカウント
                    char_count = count_pdf_characters("downloaded_file.pdf")
                    print(f"PDF内の文字数: {char_count}")
                    
                    # 文字数が100文字以上の場合は別の音声ファイルを再生
                    if char_count >= 100:
                        playsound('notice_pdf.wav')

                else:
                    print("プレビューリンクが見つかりませんでした。")
        else:
            print("メールにURLが見つかりませんでした。")

        # メール本文をテキストファイルに保存
        save_email_body_to_text(body)
    else:
        print("未読メールはありません。")

if __name__ == "__main__":
    main()

あとはvoicevoxの処理のみ

とりあえずdockerを起動させる

docker run -d -p '192.168.1.69:50021:50021' voicevox/voicevox_engine:cpu-ubuntu20.04-latest

でやったら普通に音声が再生できたので
単純に負荷でdockerが落ちてたみたい

文章を音声で読み上げしたが
どうやら最初の文章に日付と宛先の名前がくっついている
毎回main3.pyを実行するたびに他のメールでも同じなので
最初の文章を削除する

Gmail未読メールの読み上げ

Gmail未読メールの読み上げ

cp mail_message.py unread_mail_message.py


ファイルをコピーしてから改造
未読の最新の1件のみ取得するようにする

ChatGPTでの出力
最新の未読メール1件のみを取得するようにコードを修正します。
既にmessages.list API呼び出しでmaxResults=1を指定しているため、最新の1件だけを取得しています。
しかし、一番新しいメールが未読でない可能性があるため、
念のためにメッセージのリストをループする部分を削除し、最初のメッセージのみを処理するようする

from __future__ import print_function
import os.path
import re
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
import base64
import email
import dateutil.parser

# スコープの設定
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']

# トークンとクレデンシャルのパス
tokenPath = "token.json"
credentialsPath = "credentials.json"

# メール本文のデコード関数
def decode(encoded):
    decoded = base64.urlsafe_b64decode(encoded).decode()
    return decoded

# URLを削除する関数
def remove_urls(text):
    # URLにマッチする正規表現パターン
    url_pattern = r'https?://\S+|www\.\S+'
    return re.sub(url_pattern, '', text)

# 最新の未読メール本文を取得する関数
def gmail_get_latest_unread_message_body(service, labelIdsValue):
    messages = service.users().messages()
    msg_list = messages.list(userId='me', labelIds=labelIdsValue, q="is:unread", maxResults=1).execute()

    if 'messages' not in msg_list:
        return "No unread messages found."

    # 最新のメッセージ(最新の未読メール)を選択
    msg = msg_list['messages'][0]
    date = gmail_get_messages_body_date(messages, msg)
    topid = msg['id']
    msg = messages.get(userId='me', id=topid).execute()

    # メールの本文を取得
    body = ""
    if msg["payload"]["body"]["size"] != 0:
        body = decode(msg["payload"]["body"]["data"])
    elif 'parts' in msg["payload"]:
        # メール本文が parts 属性にある場合
        for part in msg["payload"]["parts"]:
            if part["body"]["size"] != 0:
                body = decode(part["body"]["data"])
                break

    if not body:
        return date + "<br> No body content"

    # URLを削除
    body_no_urls = remove_urls(body)
    return date + "<br>" + body_no_urls

# メールの受信日時を取得する関数(変更なし)
def gmail_get_messages_body_date(messages, msg):
    msg_id = msg['id']
    m = messages.get(userId='me', id=msg_id, format='raw').execute()
    raw = base64.urlsafe_b64decode(m['raw'])
    eml = email.message_from_bytes(raw)
    date = dateutil.parser.parse(eml.get('Date')).strftime("%Y-%m-%d_%H-%M-%S")
    return date

# Gmail API 初期化関数(変更なし)
def gmail_init():
    creds = None
    if os.path.exists(tokenPath):
        creds = Credentials.from_authorized_user_file(tokenPath, SCOPES)
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                credentialsPath, SCOPES)
            creds = flow.run_local_server(port=0)
        with open(tokenPath, 'w') as token:
            token.write(creds.to_json())
    service = build('gmail', 'v1', credentials=creds)
    return service

# メイン処理
service = gmail_init()

# ラベル ID を指定して最新の未読メール本文を取得
latest_unread_message_body = gmail_get_latest_unread_message_body(service, "Label_4")
print(latest_unread_message_body)

これで最新の1件のみ取得できるようになった
さらに差し出し元などもなく本文のみ取得している

URLの削除もできているが
メルマガによっては
——
などで区切っているところがある
これは読み上げるとどうなるのか?

もしダメなら、削除項目を増やすこと

とりあえず本文を読み上げる

まず支援学校のメールのIDになるように

latest_unread_message_body = gmail_get_latest_unread_message_body(service, "Label_4")

Label_4の部分を変える

次に
本文が

特別支援学校よりメッセージをお預かりしています。
下記URLをクリックして内容を確認してください。



※学校からのお知らせの保存期間は1年間です。
重要なお知らせは、スクリーンショットなどでお手元に保存してください。
※お手元で保存された画像等データのお取り扱いにつきましては、個人情報保護に十分ご配慮ください。

となって取得できているので
記号がどうなるか試すため
Docker を起動して実験

docker run -d  -p '192.168.1.69:50021:50021' voicevox/voicevox_engine:cpu-ubuntu20.04-latest

あとは
取得したメールの本文が
mail.txt
にあるので中のテキストをもとにして音声を作成する

curl -s -X POST "192.168.1.69:50021/audio_query?speaker=1" --get --data-urlencode text@mail.txt > query.json

curl -s -H "Content-Type: application/json" -X POST -d @query.json "192.168.1.69:50021/synthesis?speaker=1" > mail_audio.wav

普通に問題なく記号や句読点などは読み上げることなく成功した

支援学校は問題なさそうなので
メルマガで実験する

サーチマン佐藤さんのメルマガを使い実験

mail_searchman.txt

として保存し

curl -s -X POST "192.168.1.69:50021/audio_query?speaker=1" --get --data-urlencode text@mail_searchman.txt > query.json

curl -s -H "Content-Type: application/json" -X POST -d @query.json "192.168.1.69:50021/synthesis?speaker=1" > mail_searchman_audio.wav

で作成し音声を作成

しかし途中でdocker が過負荷で落ちる
このためおそらくこの方法ではメルマガなどの長文を再生することは出来なさそう

現在地の天気情報の取得その2

現在地の天気情報の取得その2

import requests
import json
from pprint import pprint
url = "https://api.openweathermap.org/data/2.5/weather?zip={zip_place}&units=metric&appid={API_key}"
# xxxxx
url = url.format(zip_place = "郵便番号,JP", API_key = "APIキー")

jsondata = requests.get(url).json()
pprint(jsondata)

print("天気:",jsondata["weather"][0]["main"])
print("天気詳細:",jsondata["weather"][0]["description"])

print("都市名:",jsondata["name"])
print("気温:",jsondata["main"]["temp"])
print("体感気温:",jsondata["main"]["feels_like"])
print("最低気温:",jsondata["main"]["temp_min"])
print("最高気温:",jsondata["main"]["temp_max"])
print("気圧:",jsondata["main"]["pressure"])
print("湿度:",jsondata["main"]["humidity"])

print("風速:",jsondata["wind"]["speed"])
print("風の方角:",jsondata["wind"]["deg"])

で実行すると

{'base': 'stations',
 'clouds': {'all': 0},
 'cod': 200,
 'coord': {'lat': 緯度, 'lon': 経度},
 'dt': 1701631236,
 'id': 0,
 'main': {'feels_like': 4.12,
          'humidity': 60,
          'pressure': 1021,
          'temp': 6.65,
          'temp_max': 6.65,
          'temp_min': 6.65},
 'name': 'Kawai',
 'sys': {'country': 'JP',
         'id': 2008260,
         'sunrise': 1701639567,
         'sunset': 1701675443,
         'type': 2},
 'timezone': 32400,
 'visibility': 10000,
 'weather': [{'description': 'clear sky',
              'icon': '01n',
              'id': 800,
              'main': 'Clear'}],
 'wind': {'deg': 288, 'gust': 5.36, 'speed': 3.58}}
天気: Clear
天気詳細: clear sky
都市名: 番地
気温: 6.65
体感気温: 4.12
最低気温: 6.65
最高気温: 6.65
気圧: 1021
湿度: 60
風速: 3.58
風の方角: 288

となる

デフォルトがだと気温が華氏で取得されます。摂氏を基準としたい場合はクエリに units=metric を含めます

これだと英語なので日本語で天気を取得したい

https://zenn.dev/masaru21/articles/d26519ec888f01
を元に

<!DOCTYPE html>
<html lang="ja">
  <head>
    <meta charset="UTF-8" />
    <title>日本語のタイトル</title>
    <!--
    <link rel="stylesheet" href="/css/style.css" type="text/css">
    <script type="text/javascript" src="/js/main.js"></script>

    -->

  </head>
  <body>
  <script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>
  <script>

  $(function() {
    var API_KEY = 'APIキー'
    var item_id = 'lat=35.2932718&lon=139.97671'
    
    var url = 'https://api.openweathermap.org/data/2.5/forecast?' + item_id + '&lang=ja&units=metric&appid=' + API_KEY

    $.ajax({
      url: url,
      dataType: "json",
      type: 'GET',
    })
    .done(function(data) {
      var insertHTML = "";
      var cityName = '<h2>' + data.city.name + '</h2>';
      $('#city-name').html(cityName);
      for (var i = 0; i <= 39; i = i + 8) {
        insertHTML += buildHTML(data, i);
      }
      $('#weather').html(insertHTML);
    })
    .fail(function(data) {
      console.log("失敗しました");
    });
  });

  function buildHTML(data, i) {
    var Week = new Array("(日)","(月)","(火)","(水)","(木)","(金)","(土)");
    var date = new Date (data.list[i].dt_txt);
    date.setHours(date.getHours() + 9);
    var month = date.getMonth()+1;
    var day = month + "月" + date.getDate() + "日" + Week[date.getDay()] + date.getHours() + ":00";
    var icon = data.list[i].weather[0].icon;
    var html =
    '<div class="weather-report">' +
      '<div id="cityname">' + data.city.name + '</div>' +
      '<img src="https://openweathermap.org/img/w/' + icon + '.png">' +
      '<div class="weather-date">' + day + '</div>' +
      '<div class="weather-main">'+ data.list[i].weather[0].description + '</div>' +
      '<div class="weather-temp">' + Math.round(data.list[i].main.temp) + '℃</div>' +
    '</div>';
    return html
  }

  </script>
  <style>
    .content {
        text-align: center;
        margin-left: auto;
        margin-right: auto;
    }

    .weather-report {
        margin-right: 20px;
        margin-left: 20px;
        float: left;
    }

  </style>   
    <div class="content">
      <div id="weather"></div>

    </div>
 

  </body>
</html>

をvscode の golive で実行してみた

これで5日間の天気が表示される
ただし、場所が

http://api.openweathermap.org/geo/1.0/direct?q=fukuokashi&limit=5&appid={API KEY}

で福岡に指定している

ソースの中で

 var item_id = 'lat=35.2932718&lon=139.97671'

で緯度経度を指定している

とりあえずchat GPT で日本語で天気を取得する方法を調べたら

Pythonの辞書を使って英語の天気の説明を日本語にマッピングする
英語から日本語への基本的な翻訳の辞書を作り
APIから取得した英語の天気説明を日本語に変換

書に含まれていない天気の条件には「不明」と表示されますが、必要に応じて辞書を拡張する

とのこと

# 天気の英語から日本語への翻訳辞書
weather_translation = {
    "Clear": "晴れ",
    "Clouds": "曇り",
    "Rain": "雨",
    "Snow": "雪",
    "Thunderstorm": "雷雨",
    "Drizzle": "霧雨",
    "Mist": "霧",
    "Fog": "濃霧"
}

# APIから取得した天気を翻訳する
weather_english = jsondata["weather"][0]["main"]
weather_japanese = weather_translation.get(weather_english, "不明")  # 辞書にない場合は「不明」と表示

print("天気(日本語):", weather_japanese)

この処理を加え

import requests
import json
from pprint import pprint
url = "https://api.openweathermap.org/data/2.5/weather?zip={zip_place}&units=metric&appid={API_key}"
# xxxxx
url = url.format(zip_place = "郵便番号,JP", API_key = "APIキー")

# 天気の英語から日本語への翻訳辞書
weather_translation = {
    "Clear": "晴れ",
    "Clouds": "曇り",
    "Rain": "雨",
    "Snow": "雪",
    "Thunderstorm": "雷雨",
    "Drizzle": "霧雨",
    "Mist": "霧",
    "Fog": "濃霧"
}


jsondata = requests.get(url).json()

# APIから取得した天気を翻訳する
weather_english = jsondata["weather"][0]["main"]
weather_japanese = weather_translation.get(weather_english, "不明")  # 辞書にない場合は「不明」と表示

pprint(jsondata)

# print("天気:",jsondata["weather"][0]["main"])
print("天気:",weather_japanese)

print("天気詳細:",jsondata["weather"][0]["description"])

print("都市名:",jsondata["name"])
print("気温:",jsondata["main"]["temp"])
print("体感気温:",jsondata["main"]["feels_like"])
print("最低気温:",jsondata["main"]["temp_min"])
print("最高気温:",jsondata["main"]["temp_max"])
print("気圧:",jsondata["main"]["pressure"])
print("湿度:",jsondata["main"]["humidity"])

print("風速:",jsondata["wind"]["speed"])
print("風の方角:",jsondata["wind"]["deg"])

とすることで天気が日本語になった

つまり自分で辞書を作成しないとだめ

とりあえず
* Clear sky(晴天)
* Few clouds(少し曇り)
* Scattered clouds(散らばった雲)
* Broken clouds(雲が多い)
* Overcast clouds(曇天)
* Light rain(小雨)
* Moderate rain(中雨)
* Heavy intensity rain(大雨)
* Very heavy rain(非常に激しい雨)
* Extreme rain(極端な雨)
* Freezing rain(凍雨)
* Light intensity shower rain(弱いにわか雨)
* Shower rain(にわか雨)
* Heavy intensity shower rain(激しいにわか雨)
* Ragged shower rain(不規則なにわか雨)
* Light snow(小雪)
* Snow(雪)
* Heavy snow(大雪)
* Sleet(みぞれ)
* Light shower sleet(軽いにわかみぞれ)
* Shower sleet(にわかみぞれ)
* Light rain and snow(小雨と雪)
* Rain and snow(雨と雪)
* Light shower snow(軽いにわか雪)
* Shower snow(にわか雪)
* Heavy shower snow(激しいにわか雪)
* Mist(靄)
* Smoke(煙)
* Haze(霞)
* Sand/ dust whirls(砂塵旋風)
* Fog(霧)
* Sand(砂)
* Dust(ほこり)
* Volcanic ash(火山灰)
* Squalls(スコール)
* Tornado(竜巻)
* Thunderstorm with light rain(雷雨と小雨)
* Thunderstorm with rain(雷雨)
* Thunderstorm with heavy rain(雷雨と大雨)
* Light thunderstorm(軽い雷雨)
* Thunderstorm(雷雨)
* Heavy thunderstorm(激しい雷雨)
* Ragged thunderstorm(不規則な雷雨)
* Thunderstorm with light drizzle(雷雨と弱い霧雨)
* Thunderstorm with drizzle(雷雨と霧雨)
* Thunderstorm with heavy drizzle(雷雨と激しい霧雨)

となるらしい

面倒なので

url = "https://api.openweathermap.org/data/2.5/weather?zip={zip_place}&units=metric&lang=ja&appid={API_key}"

としたら詳細天気は英語から日本語になったけど
天気は英語のままなのでやっぱり辞書は必要かも

とりあえず一日の最高気温と最低気温を取得したいのなら
もう少し処理が必要

import requests
import json
from datetime import datetime, timedelta

# OpenWeather APIのURLとパラメータを設定
url = "https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude=hourly,minutely&units=metric&appid={API_key}"
latitude = "緯度"  # 例としての緯度
longitude = "経度"  # 例としての経度
API_key = "YOUR_API_KEY"  # あなたのAPIキーをここに入力

# APIリクエスト
url = url.format(lat=latitude, lon=longitude, API_key=API_key)
response = requests.get(url)
jsondata = response.json()

# 今日の日付を取得
today = datetime.now().date()

# 今日の最高気温と最低気温を探す
for daily in jsondata["daily"]:
    date = datetime.fromtimestamp(daily["dt"]).date()
    if date == today:
        min_temp = daily["temp"]["min"]
        max_temp = daily["temp"]["max"]
        print("今日の最高気温:", max_temp, "度")
        print("今日の最低気温:", min_temp, "度")
        break

というように緯度経度で取得する

とりあえず日本語で天気取得できたので
これを音声にする

YouTube live カメラ動画をopencvで表示

YouTube live カメラ動画をopencvで表示

静岡県の薩埵峠のyoutubeライブ画像をopencv で表示する

当初は

YouTube動画をOpenCVでキャプチャするスクリプト
を参考に実行したがエラーとなる

YouTubeのライブ配信をOpenCVで再生する
も同様にエラーとなる

ChatGpt で
opencv でYouTubeライブカメラの画像を表示するPythonコード
を表示し実行したが
これもエラー

ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.
Traceback (most recent call last):
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 815, in wrapper
    return func(self, *args, **kwargs)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 836, in __extract_info
    ie_result = ie.extract(url)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/extractor/common.py", line 534, in extract
    ie_result = self._real_extract(url)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/extractor/youtube.py", line 1794, in _real_extract
    'uploader_id': self._search_regex(r'/(?:channel|user)/([^/?&#]+)', owner_profile_url, 'uploader id') if owner_profile_url else None,
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/extractor/common.py", line 1012, in _search_regex
    raise RegexNotFoundError('Unable to extract %s' % _name)
youtube_dl.utils.RegexNotFoundError: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/snowpool/aw10s/youtuvelive.py", line 36, in <module>
    display_youtube_stream(youtube_url)
  File "/Users/snowpool/aw10s/youtuvelive.py", line 11, in display_youtube_stream
    info_dict = ydl.extract_info(url, download=False)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 808, in extract_info
    return self.__extract_info(url, ie, download, extra_info, process)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 824, in wrapper
    self.report_error(compat_str(e), e.format_traceback())
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 628, in report_error
    self.trouble(error_message, tb)
  File "/Users/snowpool/.pyenv/versions/3.10.6/lib/python3.10/site-packages/youtube_dl/YoutubeDL.py", line 598, in trouble
    raise DownloadError(message, exc_info)
youtube_dl.utils.DownloadError: ERROR: Unable to extract uploader id; please report this issue on https://yt-dl.org/bug . Make sure you are using the latest version; see  https://yt-dl.org/update  on how to update. Be sure to call youtube-dl with the --verbose flag and include its complete output.

となる

youtube_dlライブラリがYouTubeのビデオからアップローダーIDを抽出することができない
で検索

Pythonのパッケージyoutube_dlで、DownloadErrorが発生する。

を参考に

pip install yt-dlp

でインストールし

import youtube_dl

の代わりに

from yt_dlp import YoutubeDL

でインポート

import cv2
import youtube_dl
from yt_dlp import YoutubeDL


def display_youtube_stream(url):
    ydl_opts = {
        'format': 'best[ext=mp4]',  # mp4 format, you can change this to other formats
        'quiet': True,
    }

    with YoutubeDL() as ydl:
        info_dict = ydl.extract_info(url, download=False)
        video_url = info_dict['url']

    cap = cv2.VideoCapture(video_url)

    if not cap.isOpened():
        print("Error: Could not open stream.")
        exit()

    while True:
        ret, frame = cap.read()
        if not ret:
            print("Failed to grab frame.")
            break

        cv2.imshow('YouTube Live Stream', frame)

        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    cap.release()
    cv2.destroyAllWindows()

# Replace with your YouTube live stream URL
youtube_url = 'https://www.youtube.com/watch?v=6S4qvf97cbQ'
display_youtube_stream(youtube_url)

として保存

これで実行すると
YouTube live画像が表示されます

なお表示しているのは
LIVE】静岡市さった峠 交通の要衝

Mac Jupyter notebook メモ

Mac Jupyter notebook メモ

Cmd + ←
で行頭へ移動後

Shift + cmd + →
で行を選択できる

単語ごとの選択は
Option + ←

単語の削除は
Option + backspace

これで
効率的にコードを書ける

[/python]
search_indicate[‘actual’] =search_indicate[‘actual’].str.replace(r”\D”, “”,regex=True)

search_indicate[‘forecast’] =search_indicate[‘forecast’].str.replace(r”\D”, “”,regex=True)

search_indicate[‘previous’] =search_indicate[‘previous’].str.replace(r”\D”, “”,regex=True)

search_indicate
[/python]

これで変換して

date2 =[]

for i in search_indicate['date']:

    new_date = datetime.datetime.strptime(i,"%d/%m/%Y").strftime("%Y-%m-%d")

    date2.append(new_date)

search_indicate['date']=search_indicate['date'].str.replace('/','-')

search_indicate['date'] = date2

search_indicate.fillna(0)

で日付を修正

あとは

actual =(

    alt.Chart(search_indicate)

    .mark_line(opacity=0.8,clip=True)

    .encode(

        x="date:T",

        y=alt.Y("actual:Q",stack=None)

    )

)



forecast =(

    alt.Chart(search_indicate)

    .mark_line(opacity=0.8,clip=True,color='red')

    .encode(

        x="date:T",

        y=alt.Y("forecast:Q",stack=None),      

    )

)



previous =(

    alt.Chart(search_indicate)

    .mark_line(opacity=0.8,clip=True,color='green')

    .encode(

        x="date:T",

        y=alt.Y("previous:Q",stack=None),      

    )

)

alt.layer(actual,forecast,previous).resolve_scale(

    y = 'independent'

)

それぞれのチャートを表示

次に
indicatorの ( 以降を
削除する

まずは

# s = 'abcxyz-xyz('



# print(my_removesuffix(s, '-xyz'))

txt ="ANZ Job Advertisements (MoM)"

print(txt)

として ( 以降を削除

Pythonで文字列の先頭と末尾から空白や文字列を削除する:strip()

を参考にしたが
strip ではできなかった

Replace と正規表現で行う

0回以上の繰り返し
*

任意の英数字は
[a-zA-Z0-9_]

\w

txt.replace("(","")

だとできた

エスケープは不要

year_economic_data2['event']=year_economic_data2['event'].replace("\([a-zA-Z0-9_]+\)","",regex=True)

year_economic_data2

とすることで解決

あとは経済指標カレンダーも同様に処理

# dealing_calender['event']= dealing_calender['event'].str.replace('(','\(').str.replace(')','\)')

dealing_calender['event']=dealing_calender['event'].replace("\([a-zA-Z0-9_]+\)","",regex=True)

とすることで解決

ただし

search_indicate['actual'] =search_indicate['actual'].str.replace(r"\D", "",regex=True)

search_indicate['forecast'] =search_indicate['forecast'].str.replace(r"\D", "",regex=True)

search_indicate['previous'] =search_indicate['previous'].str.replace(r"\D", "",regex=True)

search_indicate

だと

小数点のものまで書き換えてしまい
マイナス、小数点まで削除してしまう

不等号などはそのままにして
%
英数字のみ
削除すればいけるはず

Python: 正規表現ライブラリを使って、グループ化による置換

を参考に

import re

print(re.sub(r'([A-Z][a-z]?)(\d{1,3})', r"\\Nuc{\1}{\2}{}", "He4"))

# \Nuc{He}{4}{}

print(re.sub(r'([A-Z][a-z]?)(\d{1,3})L', r"\\Nuc{\1}{\2}{\\Lambda}", "He4L"))

# \Nuc{He}{4}{\Lambda}

みたいに

search_indicate['actual'] =search_indicate['actual'].str.replace(r"[a-zA-Z%]", "",regex=True)

search_indicate['forecast'] =search_indicate['forecast'].str.replace(r"[a-zA-Z%]", "",regex=True)

search_indicate['previous'] =search_indicate['previous'].str.replace(r"[a-zA-Z%]", "",regex=True)

search_indicate

とすればできた

とりあえず問題はあるけど
経済指標の取得後の処理はできてきた

次はIMM ポジションの取得

https://www.gaitame.com/market/imm/imm_currency.xls

からダウンロードしたいが

Mac だとそのままだとできないらしい

と思ったが Jupiter notebook からだとファイルを開けないみたい

Finder からだと普通にひらけた

Wgetは Mac にはないけど
Curl で代用できる

curl コマンド 使い方メモ

を参考に

-O オプションを付けることでファイル取得と保存ができる

!curl  "https://www.gaitame.com/market/imm/imm_currency.xls" -O

これで取得はコマンドでできるけど
Excel ファイル読み込みでエラー

No module xlrd

のエラーが出たので

【Python エラー】No module xlrd の対処法。権限がなくてxlrdモジュールをインストールできないとき

を参考に

!pip3 install xlrd

でインストール

あと excel の操作もできるように

!pip3 install openpyxl

で追加

あとは notebook を再起動すると
普通にExcelファイルの取り扱いできた

とりあえず
データ取得関連はほぼできた

Pandas 特定の文字列取得

Pandas 特定の文字列取得

lst = ["近隣10分20000円","近隣5分5000円","敷地内25000円",""]
df = pd.DataFrame(lst, columns = ['駐車場'])
import re

def fee(x):
    m = re.search(r'\d+円', x)
    if m:
        return m.group()
    else:
        return ""

df['駐車料金'] = df['駐車場'].apply(fee)
print(df)

これは apply を使うとできる

「駐車場」列から、金額のみを抽出して、新たな列「駐車料金」

として追加

これだと円が入っているので
これを金額のみにする

re.search() で正規表現で探れる

[解決!Python]re.search/re.match関数と正規表現を使って文字列から部分文字列を抽出するには
を参考に

s = 'id: deep, mail: deep@foo.com, tel: 03-0123-4567'

# re.search関数は文字列にパターンとマッチする部分があるかを調べる
m = re.search('tel: [-\d]+', s)
print(m)  # <re.Match object; span=(30, 47), match='tel: 03-0123-4567'>
r = m.group()  # re.Matchオブジェクトのgroupメソッドでマッチ全体を抽出
print(r)  # tel: 03-0123-4567

つまり

def fee(x):

    m = re.search(r'\d+円', x)

の部分で

xとして渡された文字列から

'\d+円'

で検索している

re.search(r’

で検索したら
正規表現の先頭につく`r`は何ですか?エスケープシーケンスやrow文字列を解説します
とのこと

正規表現を使う時に
R をつけることを推奨しているためらしい

次にわからんのが
m.group()

マッチした文字列を取得: group()
とあった

Pythonの正規表現マッチオブジェクトでマッチした文字列や位置を取得
を参考に

つまり

    m = re.search(r'\d+円', x)

    if m:
        return m.group()

の部分で

\d+円

にマッチする部分を格納している

次に

df['駐車料金'] = df['駐車場'].apply(fee)

の部分

pandasのapply関数の代表的な使い方
が参考になる

df = pd.DataFrame({
        "col_A": [100, 300, 200, 400, 200, 700, 200],
        "col_B": ["A", "B", "C", "D", "E", "D", "C"],
        "col_C": [2, 3, 5, 1, 2, 3, 8]
        })
df.head(10)


# 受け取ったレコードに対して、やりたい操作を記述する
def func1(row):
   return row["col_A"] * row["col_C"]



# dfをレコード単位でfunc1に渡して何らかの操作をしてもらう。戻り値は新たに作成する"col_D"に保存する。
df["col_D"] = df.apply(func1, axis=1)
df.head(10)



# 受け取った値に対して、やりたい操作を記述する
def func2(x):
    return x * 100



# dfの"col_D"の値をfunc2に渡して何らかの操作をしてもらう。戻り値は新たに作成する"col_E"に保存する。
df["col_E"] = df["col_D"].apply(func2)

df

でみるとわかるけど

Def で定義した自作メソッドを

apply() で実行している

そして新しいカラムを追加して

そこへ処理した結果を格納している

とりあえず

まずは apply などで

円の文字を削除してみよう

これができればツイートから経済指標部分を「取り出し

数値以外を削除して数値を文字列に変換すれば処理できる

Pandasで不要な文字を取り除いたり置換する方法まとめ
https://deepage.net/features/pandas-str-replace.html#%E7%89%B9%E5%AE%9A%E3%81%AE%E6%96%87%E5%AD%97%E3%82%92%E6%8C%87%E5%AE%9A%E3%81%97%E3%81%A6%E6%B6%88%E5%8E%BB

を参考に

str.strip()

で削除できる

今回は円を削除したい

df['駐車料金'].str.strip('円.')

で削除はできる

あとは反映

df['駐車料金'] =df['駐車料金'].str.strip('円.')

df

と経済指標のときみたいに代入すればok

これで文字列の編集はできるので

次に pandas で特定の文字を含む行の抽出をする

まずはツイートの取得と変数への格納

その前に経済指標カレンダーの取得

これがあっているか調べないとだめ

import pandas as pd

url = 'https://fx.minkabu.jp/indicators' #みんかぶFXの経済指標URLを取得
dfs = pd.read_html(url)[0] #テーブルのオブジェクトを生成

#本日のカレンダーを取得

dfs

でも十分に使える

余分なものをなくしてみやすくするなら

import pandas as pd


url = 'https://fx.minkabu.jp/indicators' #みんかぶFXの経済指標URLを取得
dfs = pd.read_html(url) #テーブルのオブジェクトを生成



#前処理開始
dfs1 = dfs[0].dropna(subset = [4]) #4番にNaNが入っている行はバグなので、削除
dfs2 = dfs1.drop(2,axis =1) #2番目の列を削除。axis = 1は列を削除するオプション
dfs2.columns = ["発表時間", "経済指標", "前回変動幅(USD/JPY)","前回","予想","結果"]#列名を手動で追加。



#前処理終了
dfs2    #テーブルを表示

となる

よくみたら配列で0〜6で

0が月曜

つまりdatetime などで曜日を取得してしていする必要がある

引数に曜日を数値で指定する必要ですかあるが

https://itips.krsw.biz/python-get-week-number/#st-toc-h-4

にあるように

Pythonで曜日を数字で取得する方法 まとめ

Pythonで曜日を取得するには weekday() を使う。

weekday() は今日(today)以外にもdatetime型の変数にも適用可能。

戻り値は0から6までの数値で月曜日が0に当たる。

とのこと

つまり今日の日付を取得して

これを使えば曜日指定できるはず

import datetime



# 曜日番号を取得(0は月曜日)

week_num = datetime.date.today().weekday()

print(datetime.date.today())

print(type(datetime.date.today()))

print(week_num)

このweek_num を使えば指定できるはず

これは成功

import pandas as pd

import datetime



week_num = datetime.date.today().weekday()



url = 'https://fx.minkabu.jp/indicators' #みんかぶFXの経済指標URLを取得

dfs = pd.read_html(url) #テーブルのオブジェクトを生成

print(week_num)

で曜日の数値が出る

あとは

dfs[week_num]

で本日のカレンダーを取得できる

import pandas as pd



url = 'https://fx.minkabu.jp/indicators' #みんかぶFXの経済指標URLを取得

dfs = pd.read_html(url) #テーブルのオブジェクトを生成



#前処理開始



dfs1 = dfs[0].dropna(subset = [week_num]) #4番にNaNが入っている行はバグなので、削除

dfs2 = dfs1.drop(2,axis =1) #2番目の列を削除。axis = 1は列を削除するオプション

dfs2.columns = ["発表時間", "経済指標", "前回変動幅(USD/JPY)","前回","予想","結果"]#列名を手動で追加。



#前処理終了



dfs2    #テーブルを表示

をそのあとで実行するとよりわかりやすく表示できる

そのまま経済指標のカレンダーで

Twitter で検索しても出ないが

予測値などを取得することはできるはず

次は

Pandas で時刻の比較とかトリガーとして使う方法を探す

これは

Pandas】DataFrameの文字列日付データを比較して、抽出したりする

https://www.tcom242242.net/entry/python-basic/dataframe-date-compare-extract/#toc7

を参考にまず日時の部分をdatetime へ変換する

#Dfs2 の発表時間をdatetime へ変換



def convert_date(x):

     date = datetime.datetime.strptime(x,"%Y-%m-%d %H:%M:%S")

     return date



dfs2["発表時間"] = dfs2["発表時間"].apply(convert_date)

dfs2

だとエラー
https://qiita.com/over_di/items/1270d3737e0a81719e56

Applyだとエラーになる

Pandas の to_datetime()

だとできるけど

年数の指定がないため1900年1月1日になってしまう

これを解決しないとだめ

これを解決するには

https://qiita.com/over_di/items/1270d3737e0a81719e56
Pandasのpd.to_datetimeで時刻のみ抜き出す方法

を参考に
https://qiita.com/over_di/items/1270d3737e0a81719e56
https://qiita.com/over_di/items/1270d3737e0a81719e56

.dt.time

をつければOK

dfs2["発表時間"] = pd.to_datetime(dfs2["発表時間"],format="%H:%M",errors='coerce').dt.time

dfs2

とすることで無事に変換できた

次は時刻に応じてカレンダーの列を取り出すようにする

オブジェクトを調べたら

to_datetime()

では型変換されていない

元の状態を

dfs2.dtypes
/
で調べたら


発表時間              object

経済指標              object

前回変動幅(USD/JPY)    object

前回                object

予想                object

結果                object

dtype: object

となっていた

ロイターRSS取得

ロイターRSS取得

ニュース取得にはRSSという方法もある

https://www.google.com/amp/www.algo-fx-blog.com/python-news-headlines-rss/amp/
によれば
feedparserライブラリでできるらしい

https://qiita.com/hann-solo/items/46d2bd25667618c36a5d
を参考に

ロイターRSSのURLを変更したらできたが

# ニュースのヘッドライン
print(reuters['entries'][0]['title'])

# 公開日時
print(reuters['entries'][0]['published'])
 
# URL
print(reuters['entries'][0]['link'])

でえらーになる

entries がうまく行かない

# 取得したデータを確認
reuters

で内容を確認

# 公開日時
#print(reuters['entries'][0]['published'])
print(reuters['entries'][0]['updated'])

としたら表示できた

[/python]
# ニュースの取得件数の確認
len(reuters[‘entries’])
[/python]
で確認したら現在は10が限界

# マーケットウォッチのデータを取得
market = feedparser.parse('http://feeds.marketwatch.com/marketwatch/realtimeheadlines/')
market_df = pd.DataFrame(market['entries'])
market_df.head(2)

としたが見にくい

# データを綺麗にする
market_df = market_df[['published', 'title']]
market_df.head()

で見やすくなったが title が省略される

market_df[['title']]

としても同じ

【Python】Pandasのデータフレームを省略せずに表示する方法を紹介!
を参考に省略せずに表示する

多分表示されなくてもデータの格納はされていると思う

https://qiita.com/kiddayo/items/e5ce519a234aff88af12

を参考に

pd.set_option('display.max_rows', 1500)
pd.set_option('display.max_columns', 4096)

としたが
Summary 表示は変わらない
とりあえずは保留

またRSSだと
ニュース更新頻度に問題あるため
Twitter から取得する

すでに tweepy はインストール済

あとはツイート検索と取得
これもできたので
次はテキストのみ抽出する

Streamline でエラーになったが

https://teratail.com/questions/97557
を参考に

重複したdatetime を修正し

new_date = datetime.strptime(i,"%d/%m/%Y").strftime("%Y-%m-%d")

としたら解決した

修正したので commit しようとしたら

Author identity unknown


*** Please tell me who you are.


Run


  git config --global user.email "you@example.com"

  git config --global user.name "Your Name"


to set your account's default identity.

Omit --global to set the identity only in this repository.


fatal: unable to auto-detect email address
 

となったので

git config --global user.email "メルアド"

git config --global user.name "snowpooll"
 

を実行後

git commit -m "Correction of datetime part"
 

これで

Git push origin main
 

でOK

for tweet in tweepy.Cursor(api.search_tweets, q='"#雇用統計"').items(10):

    print(tweet.text)

とすれば

#雇用統計

を含むツイートが10件取得できる

Tweet.text

でツイート情報を取得できる

Tweet.user

ツイートしたユーザ情報にアクセス

Tweet.user.name

ユーザ名の取得

次に特定ユーザのツイート

#if文にてRTとリプライを除外

[tweet.text for tweet in tweepy.Cursor(api.user_timeline, id="Qiita").items(10) if (list(tweet.text)[:2]!=['R', 'T']) & (list(tweet.text)[0]!='@')]

としたら

Unexpected parameter id

となるので id ではないかもしれない

https://toxublog.com/blog/get_tweet_tweepy/
を参考にDFにしてみた

必要なのは

ユーザ名
時刻
ツイート内容

これで一回経済指標で実験してみる

#検索条件の設定

searchkey = '#雇用統計'

item_num = 10

#検索条件を元にツイートを抽出

#tweets = tweepy.Cursor(api.search,q=searchkey,lang='ja').items(item_num)

tweets = tweepy.Cursor(api.search_tweets,q=searchkey,lang='ja').items(item_num)

tweet_data = []

for tweet in tweets:

        #tweet_dataの配列に取得したい情報を入れていく

    tweet_data.append([

        tweet.text,

        tweet.user.screen_name,

        tweet.user.name

                       ])

    #取り出したデータをpandasのDataFrameに変換

#CSVファイルに出力するときの列の名前を定義

labels=[

    'ツイート内容',

    'ユーザID',

    'アカウント名',

    ]

#tweet_dataのリストをpandasのDataFrameに変換

tweet_df = pd.DataFrame(tweet_data,columns=labels)

で大体近い感じ

あとは省略されている部分を調べる

Cvs では省略されていないので

そのままでも使えるはず

もしくはそのままでも使えるか調べる

経済指標アラートアカウント見たけど

アラートのみで実際の値はなし

Reutersとかのアカウントを使った方が正解かもしれない

とりあえず

アカウント名

ツイート内容が取得できればok

あとはここから絞り込み

python で PDF 取り扱いライブラリのインストール

python で PDF 取り扱いライブラリのインストール

ReportLab・wkhtmltopdf / pdfkit・PyPDF2・pdfminer.six・tabula-py
を使えるようにする

python でPDF作成編集、テキスト、表の抽出をすることができるようになる

reportlab のインストールは
Pythonのreportlabの使い方まとめ

を参考に

pip install reportlab

を実行

WARNING: You are using pip version 21.2.4; however, version 21.3 is available.
You should consider upgrading via the '/home/snowpool/anaconda3/bin/python -m pip install --upgrade pip' command.

という警告がでたら
pip のアップデートが必要なので

python -m pip install --upgrade pip

で pip をアップデートする

pdfkit は

HTMLをPDFに変換するライブラリ「pdfkit」をインストールする

を参考に

pip install pdfkit

でインストール

次に PyPDF2

【Python】「PyPDF2ライブラリ」によるPDFファイルの結合・分割と画像の抽出

を参考にインストール

pip だと画像の抽出に一部欠陥があるため
Github のインストーラーがおすすめらしい

https://github.com/mstamy2/PyPDF2
へアクセス

Code をクリックすると
Download ZIP がでるので
これでダウンロード

unzip PyPDF2-master.zip

で解凍して

cd PyPDF2-master/
python setup.py install

でインストール

pdfminer.six は
【Python】pdfminer.six:PDFからテキストを取得・抽出する

を参考に

pip install pdfminer.six

でインストール

【自動化】PDFの表をExcelへ(Python)

を参考に

pip install tabula-py

次に
pyinstaller

pyinstallerインストール ( Python3, Ubuntu 20.04 )

を参考に

pip3 install pyinstaller

でインストール

python でIMMポジショングラフ表示

python でIMMポジショングラフ表示

【第35回PYTHON講座】plotlyで複合グラフの作成


IMMポジションをグラフ表示するのがあったので実践

plotly を使うので

!pip install plotly

でインストール

使用するIMMポジションデータについては
外為どっとこむからダウンロード可能

https://www.gaitame.com/markets/imm/

xls ファイルをダウンロード

https://www.gaitame.com/market/imm/imm_currency.xls
がリンク先なので
wget を cron で毎週火曜日に取得するように設定すると楽かもしれない

内容は毎週火曜日に更新されている

ubuntu なので
Excel ではなく LibreOffice で代用

使用するシートは
IMMyen になる

この中で使用するセルは
日付
IMMポジション数差引
となる

まずはライブラリをインポート

import pandas as pd
import plotly

次にファイルを jupyter notebook で
immcurrency.xls
をアップロードし

xlsfile = "imm_currency.xls"

でファイルを読み込む

df_a = pd.read_excel(xlsfile, sheet_name="IMMyen", usecols=[0, 8])
df_b = pd.read_excel(xlsfile, sheet_name="IMMyen", usecols=[0, 1])

で usecols を使い抽出する列を指定
df_a には差引
df_b にはドル円レート
を格納している

0がA列になり8がI列になっている

1はB列になる

数えるときには0からなので注意

df1 = df_a[50:len(df_a)]
df2 = df_b[50:len(df_b)]

で50行目から終わりまでを指定

次に棒グラフの設定

graph1 = plotly.graph_objs.Bar(
x=df1.iloc[:, 0],
y=df1.iloc[:, 1],
yaxis="y1",
name="差引数"
)

graph_objs.Bar が棒グラフの設定になる

x=df1.iloc[:, 0],
y=df1.iloc[:, 1],

でどの列を対象にするのか視程している
0が日付
1が差引の数になる

yaxis="y1",

でy軸の名称をつけて識別できるようにする

次に折れ線グラフ

graph2 = plotly.graph_objs.Scatter(
x=df2.iloc[:, 0],
y=df2.iloc[:, 1],
yaxis="y2",
name="ドル円"
)

次に余白設定

adjust1 = (df1.iloc[:, 1].max() - df1.iloc[:, 1].min())/10 / 2
adjust2 = (df2.iloc[:, 1].max() - df2.iloc[:, 1].min())/10 / 2

adjust1、adjust2はそれぞれのY軸の最大値、最小値の差の20分の1を定義

これでグラフ表示のときにY軸上下に余白をつけることができる

次に複号グラフの位置の設定

layout = plotly.graph_objs.Layout(

    title="複合グラフ",

    xaxis=dict(title="日付"),

    yaxis=dict(
        title="IMMポジション差引数",
        range=[df1.iloc[:, 1].min() - adjust1, df1.iloc[:, 1].max() + adjust1]),

    yaxis2=dict(
        title="ドル円レート",
        range=[df2.iloc[:, 1].min() - adjust2, df2.iloc[:, 1].max() + adjust2],
        overlaying="y1", side="right", showgrid=False)

)

これは
graph_objs.Layout で設定する

range でy軸の高さ

overlaying は上に表示するときに使う

今回は

overlaying="y1"

を折れ線グラフで設定しているので
棒グラフの上に折れ線グラフがでる

side="right"

とすると
右側にy軸の値がでる

こうすることで2つのy軸表示がでる
デフォルトは左にでる

showgrid=False

を指定すると
グリッドが表示されなくなる

これがあるとグリッド線が表示されてわかりにくくなるので
今回は false にしている

あとは複号グラフの表示

data = [graph1, graph2]
fig = plotly.graph_objs.Figure(data=data, layout=layout)
fig.show()

data に表示するものを指定[/python]
graph_objs.Figure() [/python]
でデータとレイアウトをセット

show() 

で表示する

次に別の通貨で応用
ユーロドルで実践

imm_euro =  pd.read_excel(xlsfile, sheet_name="IMMeuro", usecols=[0, 8])
euro_rate = pd.read_excel(xlsfile, sheet_name="IMMeuro", usecols=[0, 1])

でユーロドルのシートからデータ取得

imm_euro_data = imm_euro[50:len(df_a)]
euro_rate_data = euro_rate[50:len(df_b)]

でデータを50行目からに絞り込む

imm_graph = plotly.graph_objs.Bar(
x=imm_euro_data.iloc[:, 0],
y=imm_euro_data.iloc[:, 1],
yaxis="y1",
name="差引数"
)
euro_graph = plotly.graph_objs.Scatter(
x=euro_rate_data.iloc[:, 0],
y=euro_rate_data.iloc[:, 1],
yaxis="y2",
name="ドル円"
)

でそれぞれのデータをグラフにセット

adjust1 = (imm_euro_data.iloc[:, 1].max() - imm_euro_data.iloc[:, 1].min())/10 / 2
adjust2 = (euro_rate_data.iloc[:, 1].max() - euro_rate_data.iloc[:, 1].min())/10 / 2

で余白を設定してグラフ表示を見やすくする

layout = plotly.graph_objs.Layout(

    title="複合グラフ",

    xaxis=dict(title="日付"),

    yaxis=dict(
        title="IMMポジション差引数",
        range=[imm_euro_data.iloc[:, 1].min() - adjust1, imm_euro_data.iloc[:, 1].max() + adjust1]),

    yaxis2=dict(
        title="ユーロ/ドルレート",
        range=[euro_rate_data.iloc[:, 1].min() - adjust2, euro_rate_data.iloc[:, 1].max() + adjust2],
        overlaying="y1", side="right", showgrid=False)

)


複号グラフの設定

data = [imm_graph, euro_graph]
fig = plotly.graph_objs.Figure(data=data, layout=layout)
fig.show()


データ表示

これでユーロドルでの
IMMポジションの比率
ユーロドルの現状がわかりやすくなる