自分の過去のツイートから特定の舞台のハッシュタグだけ登録して観劇感想ツイート検索できないか
イメージ画像 (身バレ防止であえて個人アカウント名表示しないようにしています)
chat GPTの助けを借りてpythonでシステムを作ってみる
好きなハッシュタグのボタンを作って、押したら自分の投稿からリンクに飛べる段階まではできた
日本語入力ができない → 日本語入力可にする必要がある
- import kivy
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- import webbrowser
- kivy.require('1.11.1')
- class LinkOpenerApp(App):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.links = []
- def build(self):
- self.layout = BoxLayout(orientation='vertical')
- self.input_text1 = TextInput(
- hint_text='enter your account name without "@"')
- self.input_text2 = TextInput(
- hint_text='enter a hashtag without "#"')
- self.add_button = Button(text='create')
- self.add_button.bind(on_press=self.add_link)
- self.remove_button = Button(text='remove')
- self.remove_button.bind(on_press=self.remove_link)
- self.layout.add_widget(self.input_text1)
- self.layout.add_widget(self.input_text2)
- self.layout.add_widget(self.add_button)
- self.layout.add_widget(self.remove_button)
- return self.layout
- def add_link(self, instance):
- hashtag = self.input_text2.text.strip()
- if hashtag:
- link_button = Button(text=f'#{hashtag}')
- link_button.bind(on_press=lambda x, link=hashtag: self.open_link(link))
- self.links.append(link_button)
- self.layout.add_widget(link_button)
- self.input_text2.text = ''
- def remove_link(self, instance):
- if self.links:
- last_link_button = self.links.pop()
- self.layout.remove_widget(last_link_button)
- def open_link(self, hashtag):
- acn = self.input_text1.text.strip()
- if hashtag:
- search_url = f'https://twitter.com/search?q=from%3A{acn}%20%23{hashtag}&src=typed_query&f=top'
- webbrowser.open(search_url)
- if __name__ == '__main__':
- LinkOpenerApp().run()
次に取り組むこと
- import japanize_kivy
- import urllib.parse
- import kivy
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- import webbrowser
- kivy.require('1.11.1')
- class LinkOpenerApp(App):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.links = []
- def build(self):
- self.layout = BoxLayout(orientation='vertical')
- self.input_text1 = TextInput(
- hint_text='enter your account name without "@"')
- self.input_text2 = TextInput(
- hint_text='enter a hashtag without "#"')
- self.add_button = Button(text='create')
- self.add_button.bind(on_press=self.add_link)
- self.remove_button = Button(text='remove')
- self.remove_button.bind(on_press=self.remove_link)
- self.layout.add_widget(self.input_text1)
- self.layout.add_widget(self.input_text2)
- self.layout.add_widget(self.add_button)
- self.layout.add_widget(self.remove_button)
- return self.layout
- def add_link(self, instance):
- hashtag = self.input_text2.text.strip()
- if hashtag:
- link_button = Button(text=f'#{hashtag}')
- link_button.bind(on_press=lambda x, link=hashtag: self.open_link(link))
- self.links.append(link_button)
- self.layout.add_widget(link_button)
- self.input_text2.text = ''
- def remove_link(self, instance):
- if self.links:
- last_link_button = self.links.pop()
- self.layout.remove_widget(last_link_button)
- def open_link(self, hashtag):
- hashtag = urllib.parse.quote(hashtag) # キーワードをエンコード
- acn = self.input_text1.text.strip()
- if hashtag and acn:
- # エンコードされたキーワードとアカウント名を使用してURLを生成
- search_url = f'https://twitter.com/search?q=from%3A{acn}%20%23{hashtag}&src=typed_query&f=top'
- webbrowser.open(search_url)
- if __name__ == '__main__':
- LinkOpenerApp().run()
閉じる△
- import japanize_kivy
- import urllib.parse
- import kivy
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- from kivy.core.window import Window
- import webbrowser
- kivy.require('1.11.1')
- class LinkOpenerApp(App):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.links = []
- def build(self):
- # ウィンドウの背景色を白に設定
- #Window.clearcolor = (1, 1, 1, 1)
- self.layout = BoxLayout(orientation='vertical')
- self.input_text1 = TextInput(
- hint_text='"@"なしでアカウントIDを入力してください',
- size_hint_y=None,
- height=400
- )
- self.input_text2 = TextInput(
- hint_text='"#"なしでハッシュタグ名を入力してください',
- size_hint_y=None,
- height=300
- )
- self.add_button = Button(
- text='タグを追加',
- background_color=(0, 0, 1, 1), # オレンジの背景色 rgbaで表示、ボタンはデフォルトでグレー設定
- color=(1, 1, 1, 1), # 白の文字色
- height = 300,
- )
- self.add_button.bind(on_press=self.add_link)
- self.remove_button = Button(
- text='タグを削除',
- background_color=(1, 0.5, 0, 1), # オレンジの背景色
- color=(1, 1, 1, 1), # 白の文字色
- height = 400
- )
- self.remove_button.bind(on_press=self.remove_link)
- # createボタンとremoveボタンを横に並べる
- button_layout = BoxLayout(orientation='horizontal', size_hint_y=None, height=40)
- button_layout.add_widget(self.add_button)
- button_layout.add_widget(self.remove_button)
- self.layout.add_widget(self.input_text1)
- self.layout.add_widget(self.input_text2)
- self.layout.add_widget(button_layout) # ボタンを追加
- return self.layout
- def add_link(self, instance):
- hashtag = self.input_text2.text.strip()
- if hashtag:
- link_button = Button(
- text=f'#{hashtag}',
- background_color=(1, 0.5, 0, 1), # オレンジの背景色
- color=(1, 1, 1, 1), # 白の文字色
- height = 200
- )
- link_button.bind(on_press=lambda x, link=hashtag: self.open_link(link))
- self.links.append(link_button)
- self.layout.add_widget(link_button)
- self.input_text2.text = ''
- def remove_link(self, instance):
- if self.links:
- last_link_button = self.links.pop()
- self.layout.remove_widget(last_link_button)
- def open_link(self, hashtag):
- hashtag = urllib.parse.quote(hashtag) # キーワードをエンコード
- acn = self.input_text1.text.strip()
- if hashtag and acn:
- # エンコードされたキーワードとアカウント名を使用してURLを生成
- search_url = f'https://twitter.com/search?q=from%3A{acn}%20%23{hashtag}&src=typed_query&f=top'
- webbrowser.open(search_url)
- if __name__ == '__main__':
- LinkOpenerApp().run()
閉じる△
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- from kivy.uix.screenmanager import ScreenManager, Screen
- import webbrowser
- import japanize_kivy
- import urllib.parse
- class MyApp(App):
- def build(self):
- return MyBoxLayout()
- class MyBoxLayout(BoxLayout):
- def __init__(self, **kwargs):
- super(MyBoxLayout, self).__init__(**kwargs)
- self.orientation = 'vertical'
- self.button_create = Button(text='create', on_press=self.show_page_1)
- self.button_showlist = Button(text='showlist', on_press=self.show_page_2)
- self.button_close = Button(text='close', on_press=self.close_app)
- self.add_widget(self.button_create)
- self.add_widget(self.button_showlist)
- self.add_widget(self.button_close)
- self.screen_manager = ScreenManager()
- self.page_1 = Page1(name='page_1')
- self.page_2 = Page2(name='page_2')
- self.screen_manager.add_widget(self.page_1)
- self.screen_manager.add_widget(self.page_2)
- self.add_widget(self.screen_manager)
- def show_page_1(self, instance):
- self.screen_manager.current = 'page_1'
- def show_page_2(self, instance):
- self.screen_manager.current = 'page_2'
- def close_app(self, instance):
- App.get_running_app().stop()
- class Page1(Screen):
- def __init__(self, **kwargs):
- super(Page1, self).__init__(**kwargs)
- self.layout = BoxLayout(orientation='vertical')
- self.text_input1 = TextInput(hint_text='enter your account name without "@"')
- self.text_input2 = TextInput(hint_text='enter a hashtag without "#"')
- self.button_add = Button(text='add', on_press=self.add_yellow_button)
- self.layout.add_widget(self.text_input1)
- self.layout.add_widget(self.text_input2)
- self.layout.add_widget(self.button_add)
- self.add_widget(self.layout)
- def add_yellow_button(self, instance):
- page_2 = self.manager.get_screen('page_2')
- hashtag = self.text_input2.text
- yellow_button = Button(text=f'{hashtag}', on_press=self.search_google)
- page_2.layout.add_widget(yellow_button)
- def go_to_page_1(self, instance):
- self.manager.current = 'page_1'
- def search_google(self, instance):
- acn = self.text_input1.text
- hashtag = self.text_input2.text
- hst = urllib.parse.quote(hashtag) # キーワードをエンコード
- webbrowser.open(f'https://twitter.com/search?q=from%3A{acn}%20%23{hst}&src=typed_query&f=top')
- class Page2(Screen):
- def __init__(self, **kwargs):
- super(Page2, self).__init__(**kwargs)
- self.layout = BoxLayout(orientation='vertical')
- self.add_widget(self.layout)
- if __name__ == '__main__':
- MyApp().run()
- <MyBoxLayout>:
- orientation: 'vertical'
- Button:
- text: 'red'
- on_press: root.show_page_1(self)
- Button:
- text: 'green'
- on_press: root.show_page_2(self)
- Button:
- text: 'blue'
- on_press: root.close_app(self)
- ScreenManager:
- id: screen_manager
- Screen:
- name: 'page_1'
- BoxLayout:
- orientation: 'vertical'
- TextInput:
- hint_text: 'Enter memo here'
- Button:
- text: 'black'
- on_press: root.page_1.add_yellow_button(self)
- Button:
- text: 'white'
- on_press: root.page_1.go_to_page_1(self)
- Screen:
- name: 'page_2'
- BoxLayout:
- orientation: 'vertical'
閉じる△サンプルアプリ(上記サイトから拝借)をPC上で表示
ビルドしてapkファイルにした後、スマホで上記アプリを表示
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- from kivy.uix.screenmanager import ScreenManager, Screen
- import csv
- import webbrowser
- import japanize_kivy
- import urllib.parse
- class MyApp(App):
- def build(self):
- return MyBoxLayout()
- class MyBoxLayout(BoxLayout):
- def __init__(self, **kwargs):
- super(MyBoxLayout, self).__init__(**kwargs)
- self.orientation = 'vertical'
- self.button_create = Button(text='create', on_press=self.show_page_1)
- self.button_showlist = Button(text='showlist', on_press=self.show_page_2)
- self.button_close = Button(text='close', on_press=self.close_app)
- self.add_widget(self.button_create)
- self.add_widget(self.button_showlist)
- self.add_widget(self.button_close)
- self.screen_manager = ScreenManager()
- self.page_1 = Page1(name='page_1')
- self.page_2 = Page2(name='page_2')
- self.screen_manager.add_widget(self.page_1)
- self.screen_manager.add_widget(self.page_2)
- self.add_widget(self.screen_manager)
- def show_page_1(self, instance):
- self.screen_manager.current = 'page_1'
- def show_page_2(self, instance):
- self.screen_manager.current = 'page_2'
- def close_app(self, instance):
- App.get_running_app().stop()
- class Page1(Screen):
- def __init__(self, **kwargs):
- super(Page1, self).__init__(**kwargs)
- self.layout = BoxLayout(orientation='vertical')
- self.text_input1 = TextInput(hint_text='enter your account name without "@"')
- self.button_add_acn = Button(text='add', on_press=self.add_acn)
- self.text_input2 = TextInput(hint_text='enter a hashtag without "#"')
- self.button_add_ht = Button(text='green', on_press=self.add_green_button)
- self.layout.add_widget(self.text_input1)
- self.layout.add_widget(self.button_add_acn)
- self.layout.add_widget(self.text_input2)
- self.layout.add_widget(self.button_add_ht)
- self.add_widget(self.layout)
- def add_acn(self, instance):
- acn = self.text_input1.text
- # CSVファイルの読み込み
- existing_data = []
- try:
- with open('list.csv', 'r') as csvfile:
- csv_reader = csv.reader(csvfile)
- existing_data = list(csv_reader)
- except FileNotFoundError:
- pass
- # CSVファイルへの書き込み
- with open('list.csv', 'w', newline='') as csvfile:
- csv_writer = csv.writer(csvfile)
- csv_writer.writerow([acn])
- # 既存のデータを書き込む
- for row in existing_data[1:]:
- csv_writer.writerow(row)
- def add_green_button(self, instance):
- page_2 = self.manager.get_screen('page_2')
- hashtag = self.text_input2.text.strip()
- # CSVファイルの読み込み
- existing_data = []
- try:
- with open('list.csv', 'r') as csvfile:
- csv_reader = csv.reader(csvfile)
- existing_data = list(csv_reader)
- except FileNotFoundError:
- pass
- # CSVファイルへの書き込み(追記)
- with open('list.csv', 'a', newline='') as csvfile:
- csv_writer = csv.writer(csvfile)
- csv_writer.writerow(['', hashtag])
- green_button = Button(text=f'{hashtag}', on_press=lambda instance: self.search_tweet(hashtag))
- page_2.layout.add_widget(green_button)
- def go_to_page_1(self, instance):
- self.manager.current = 'page_1'
- def search_tweet(self, hashtag):
- try:
- with open('list.csv', 'r') as csvfile:
- csv_reader = csv.reader(csvfile)
- existing_data = list(csv_reader)
- #アカウント名はcsvの1行め
- account_name = existing_data[0][0]
- except FileNotFoundError:
- account_name = ''
- hst = urllib.parse.quote(hashtag) # キーワードをエンコード
- webbrowser.open(f'https://twitter.com/search?q=from%3A{account_name}%20%23{hst}&src=typed_query&f=top')
- class Page2(Screen):
- def __init__(self, **kwargs):
- super(Page2, self).__init__(**kwargs)
- self.layout = BoxLayout(orientation='vertical')
- self.add_widget(self.layout)
- # Load data from list.csv and create buttons
- try:
- with open('list.csv', 'r') as csvfile:
- csv_reader = csv.reader(csvfile)
- existing_data = list(csv_reader)
- # Assuming hashtags are in the second column (index 1) of the CSV file
- hashtags = [row[1] for row in existing_data[1:]]
- self.create_buttons(hashtags)
- except FileNotFoundError:
- pass # File not found, no buttons to create
- def create_buttons(self, hashtags):
- for hashtag in hashtags:
- green_button = Button(text=f'{hashtag}', on_press=lambda instance, h=hashtag: self.search_tweet(h))
- self.layout.add_widget(green_button)
- def search_tweet(self, hashtag):
- try:
- with open('list.csv', 'r') as csvfile:
- csv_reader = csv.reader(csvfile)
- existing_data = list(csv_reader)
- account_name = existing_data[0][0] #アカウント名はcsvの1行目
- except FileNotFoundError:
- account_name = ''
- hst = urllib.parse.quote(hashtag) # キーワードをエンコード
- webbrowser.open(f'https://twitter.com/search?q=from%3A{account_name}%20%23{hst}&src=typed_query&f=top')
- if __name__ == '__main__':
- MyApp().run()
- from kivy.app import App
- from kivy.uix.boxlayout import BoxLayout
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- from kivy.uix.label import Label
- from kivy.uix.screenmanager import ScreenManager, Screen
- import webbrowser
- import japanize_kivy
- import urllib.parse
- import json
- from functools import partial
- class MyApp(App):
- def build(self):
- return MyBoxLayout()
- class MyBoxLayout(BoxLayout):
- def __init__(self, **kwargs):
- #選択画面
- super(MyBoxLayout, self).__init__(**kwargs)
- self.orientation = 'vertical'
- # page1のラベル、動作
- self.button_create = Button(text='create', on_press=self.show_page_1)
- # page2のラベル、動作
- self.button_showlist = Button(text='showlist', on_press=self.show_page_2)
- # closeボタンのラベル、動作
- self.button_close = Button(text='close', on_press=self.close_app)
- # ボタンを配置
- self.add_widget(self.button_create)
- self.add_widget(self.button_showlist)
- self.add_widget(self.button_close)
- # スライドするレイアウト
- self.screen_manager = ScreenManager()
- self.page_1 = Page1(name='page_1')
- self.page_2 = Page2(name='page_2')
- self.screen_manager.add_widget(self.page_1)
- self.screen_manager.add_widget(self.page_2)
- self.add_widget(self.screen_manager)
- # プログラム開始時に、過去に保存されたボタンを読み込む
- self.load_buttons_from_file()
- def show_page_1(self, instance):
- self.screen_manager.current = 'page_1'
- def show_page_2(self, instance):
- self.screen_manager.current = 'page_2'
- def close_app(self, instance):
- self.save_buttons_to_file() # アプリを閉じる前にボタンを保存
- App.get_running_app().stop()
- def save_buttons_to_file(self):
- # ボタンの情報をファイルに保存
- buttons_data = []
- for child in self.page_2.layout.children:
- if isinstance(child, Button):
- buttons_data.append(child.text)
- with open('buttons_data.json', 'w') as file:
- json.dump(buttons_data, file)
- def load_buttons_from_file(self):
- # ファイルからボタンの情報を読み込んでPage2に表示
- try:
- with open('buttons_data.json', 'r') as file:
- buttons_data = json.load(file)
- for button_text in buttons_data:
- yellow_button = Button(text=button_text, on_press=self.search_twitter)
- self.page_2.layout.add_widget(yellow_button)
- except FileNotFoundError:
- pass # ファイルが存在しない場合は何もしない
- def search_twitter(self, instance):
- acn = self.page_1.text_input1.text
- hashtag = instance.text
- hst = urllib.parse.quote(hashtag) # キーワードをエンコード
- webbrowser.open(f'https://twitter.com/search?q=from%3A{acn}%20%23{hst}&src=typed_query&f=top')
- class Page1(Screen):
- def __init__(self, **kwargs):
- super(Page1, self).__init__(**kwargs)
- # レイアウト
- self.layout = BoxLayout(orientation='vertical')
- self.text_input1 = TextInput(hint_text='enter your account name without "@"')
- self.button_add_acn = Button(text='add your account', on_press=self.add_acn_to_p2)
- self.text_input2 = TextInput(hint_text='enter a hashtag without "#"')
- self.button_add_hashtag = Button(text='add hashtag', on_press=self.add_hashtag_to_p2)
- self.layout.add_widget(self.text_input1)
- self.layout.add_widget(self.button_add_acn)
- self.layout.add_widget(self.text_input2)
- self.layout.add_widget(self.button_add_hashtag)
- self.add_widget(self.layout)
- def add_acn_to_p2(self, instance):
- page_2 = self.manager.get_screen('page_2')
- acn = self.text_input1.text
- acn_label = Label(text=f'{acn}') # Label を使用するように修正
- page_2.layout.add_widget(acn_label)
- def add_hashtag_to_p2(self, instance):
- page_2 = self.manager.get_screen('page_2')
- hashtag = self.text_input2.text
- hst_button = Button(text=f'{hashtag}')
- hst_button.bind(on_press=partial(self.search_twitter, page_2))
- page_2.layout.add_widget(hst_button)
- def search_twitter(self, page_2, instance):
- acn = self.text_input1.text
- hashtag = instance.text
- hst = urllib.parse.quote(hashtag) # キーワードをエンコード
- webbrowser.open(f'https://twitter.com/search?q=from%3A{acn}%20%23{hst}&src=typed_query&f=top')
- class Page2(Screen):
- def __init__(self, **kwargs):
- super(Page2, self).__init__(**kwargs)
- self.layout = BoxLayout(orientation='vertical')
- self.add_widget(self.layout)
- if __name__ == '__main__':
- MyApp().run()
閉じる△