Skip to content

Instantly share code, notes, and snippets.

@ckhung
Last active May 15, 2023 07:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ckhung/e6a372caa693981a61eba706291dc735 to your computer and use it in GitHub Desktop.
Save ckhung/e6a372caa693981a61eba706291dc735 to your computer and use it in GitHub Desktop.
Use selenium to read messages from the "line" messaging app as an extension of chrome.
# This is an experimental piece of code for reading messages
# from the "line" messaging app as an extension of chrome.
# https://chrome.google.com/webstore/detail/line/ophjlpahpchlmihnnnihgmmeilfjmjjc?hl=zh-TW
# My linux is LMDE 5 (elsie), and my chromium is 113.0.5672.63,
# directly installed with apt install.
# I have also done:
# pip3 install selenium webdriver-manager beautifulsoup4
# To use this program:
# 0. Create a line account if you don't have one.
# Note: Line is control-hungry.
# It insists on binding to your one and only phone.
# Every time you create a new selenium session,
# you have to install the line extension yet again,
# and line requires you to authenticate that installation by phone.
# Therefore it's easier to stay in the python interpreter,
# attached to the same selenium session all the time.
# 1. Enter python3 interpreter.
# 2. exec(open('line-sel.py').read()) # a chromium window will be opened
# 3. Install line into the chromium window and log in.
# 4. Come back to the python interpreter and begin from
# init_line_handle()
# towards the end of this program.
import selenium
from time import sleep
from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_extension('tutorial.hello-world.crx')
prefs = {"profile.default_content_setting_values.notifications" : 2}
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome(options=chrome_options)
driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
driver.get("https://www.google.com")
driver.set_window_size(800,600)
def init_line_handle():
global all_tabs, line_handle
all_tabs = {}
for handle in driver.window_handles:
driver.switch_to.window(handle)
all_tabs[driver.title] = handle
line_handle = all_tabs['LINE']
driver.switch_to.window(line_handle)
def print_all_msg(chat):
msg_list = current_chat.find_all(recursive=False)
for msg in msg_list:
time_stamp = msg.find('p', {'class': 'mdRGT07Date'})
time_stamp = '-' if time_stamp is None else time_stamp.get_text().strip()
print('{:<8} '.format(time_stamp), end=' ')
msg_text = msg.find('div', {'class': 'mdRGT07MsgText'})
msg_text = '' if msg_text is None else msg_text.get_text().strip()
msg_class = msg.attrs['class']
if 'MdRGT10Notice'in msg_class:
print('[###]', msg.get_text())
elif 'mdRGT07Own' in msg_class:
print('*me*', msg_text)
elif 'mdRGT07Other' in msg_class:
speaker = msg.find('div', {'class': 'mdRGT07Ttl'})
speaker = '???' if speaker is None else speaker.get_text().strip()
print('[{}] '.format(speaker, msg_text))
else:
print('?????', msg_class)
# init_line_handle()
### This only needs to be done once, after line login and
### before all other following operations
### The following needs to be done every time you switch
### to a different (friend- or group) chat.
# page_soup = BeautifulSoup(driver.page_source, 'html.parser')
# current_chat = page_soup.find(id='_chat_room_msg_list')
### print(current_chat.prettify())
# print_all_msg(current_chat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment