├── README-zh.md ├── README.md ├── aria2.conf ├── aria2.service ├── quick-install.sh └── wechat_remote.py /README-zh.md: -------------------------------------------------------------------------------- 1 | # wechat-remote 2 | 通过微信远程控制电脑 (比如树莓派) 3 | 4 | - 利用网页版微信通信协议向树莓派发送指令,类似一个非实时的终端交互 5 | - 相比其它微信远程而言,这个脚本可以适用于普通微信号 6 | - 适用于无公网IP的情况下远程发送命令 7 | 8 | 测试可用系统为: `RASPBIAN JESSIE`, `Arch Linux Arm` 9 | 10 | 测试硬件为: `树莓派3` 11 | 12 | 基于 [ItChat](https://github.com/littlecodersh/ItChat) 框架;另外这个框架有一个 [robot](https://github.com/littlecodersh/ItChat/tree/robot) 分支,可做聊天机器人 13 | 14 | 在这个[demo](https://github.com/littlecodersh/ItChat/issues/24#issuecomment-228583833)的基础上修改而成 15 | 16 | ## 功能 17 | 18 | - 远程下载(aria2 + diana) 19 | - 远程下载在线视频(youtube-dl) 20 | - 播放在线视频(mpv) 21 | - 更多 22 | 23 | ## Quick Install (Debian/Raspbian) 24 | 25 | (将会安装配置 `aria2`, `diana`, `youtube-dl`, 如你已安装 `aria2` 请对照下面的 `安装`部分安装本脚本) 26 | 27 | ```bash 28 | /bin/bash -c "$(curl -sL https://git.io/vXy3m)" 29 | ``` 30 | 31 | ## 安装 32 | 33 | 安装 `subprocess32` 模块 34 | 35 | ```bash 36 | sudo apt-get install python-dev python-pillow 37 | ``` 38 | 39 | ```bash 40 | sudo pip install subprocess32 41 | ``` 42 | 43 | 下载 `itchat` 框架 44 | 45 | ```bash 46 | sudo pip install itchat``` 47 | 48 | ```bash 49 | wget https://raw.githubusercontent.com/yangxuan8282/wechat-remote/master/wechat_remote.py 50 | ``` 51 | 52 | 运行脚本: 53 | 54 | ```bash 55 | python wechat_remote.py 56 | ``` 57 | 58 | 建议在 `screen` 内运行: 59 | 60 | ```bash 61 | sudo apt-get install screen 62 | ``` 63 | 64 | ```bash 65 | screen bash 66 | ``` 67 | 68 | ``` 69 | python wechat_remote.py 70 | ``` 71 | 72 | 扫码登陆后可以操作了(有些微信账号无法给自己发送消息,需用其它账号发送) 73 | 74 | 下面具体说一下需要不同功能该如何安装配置 75 | 76 | 77 | 1. 远程下载 ([aria2](https://aria2.github.io/) + diana) 78 | 79 | 先安装 `aria2`: 80 | 81 | ```bash 82 | sudo apt-get update && sudo apt-get upgrade 83 | ``` 84 | 85 | ```bash 86 | sudo apt-get install aria2 87 | ``` 88 | 89 | 安装 `diana`: 90 | 91 | ```bash 92 | cd ~ 93 | ``` 94 | 95 | ```bash 96 | git clone https://github.com/baskerville/diana 97 | ``` 98 | 99 | 启动: 100 | 101 | ```bash 102 | dad start 103 | ``` 104 | 105 | 通过微信添加下载: 106 | 107 | ```bash 108 | diana add url 109 | ``` 110 | 111 | 通过微信查看下载进度: 112 | 113 | ```bash 114 | diana list 115 | ``` 116 | 117 | 百度云的话可以将手机浏览器的浏览器标识改为 `Symbian`,进入文件下载页面点击 `下载` 按钮获取url 118 | 然后发送给登陆的微信账号,格式如下 119 | 120 | ```bash 121 | diana add "url" 122 | ``` 123 | 124 | 加双引号是为了防止地址被断开 125 | 126 | 127 | 2. 下载视频 (youtube-dl) 128 | 129 | 先安装 `youtube-dl` 130 | 131 | ```bash 132 | pip install youtube-dl 133 | ``` 134 | 135 | 然后给自己的账号发送指令就可以下载了 136 | 137 | ```bash 138 | youtube-dl url 139 | ``` 140 | 141 | 查看可选的清晰度 142 | 143 | ```bash 144 | youtube-dl -F url 145 | ``` 146 | 147 | 下载选定的清晰度(返回的信息可能较长) 148 | 149 | ```bash 150 | youtube-dl -f args url 151 | ``` 152 | 153 | 另一个视频下载的选择是 [you-get](https://github.com/soimort/you-get) 154 | 155 | 156 | 3. 播放在线视频 157 | 158 | - `omxplayer` + `you-get` 159 | 160 | 测试可用站点:Youtube 161 | 162 | 先安装 `you-get` 163 | 164 | ```bash 165 | sudo pip3 install you-get 166 | ``` 167 | 168 | 播放指令: 169 | 170 | ```bash 171 | you-get -p omxplayer url 172 | ``` 173 | 174 | 相较 `mpv` 而言,`omxplayer` 效率更高,但是由于有些指令可能无法像在终端内一样执行,比如调用 `youtube-dl` 获取视频地址,然后让 `omxplayer` 播放的指令: 175 | 176 | ```bash 177 | omxplayer $(youtube-dl -g url) 178 | ``` 179 | 180 | 就没法成功播放,只能通过 `you-get` 调用播放器;国内的站点能成功播放的较少 181 | 182 | `you-get`获取视频地址的速度比 `youtube-dl` 慢一些,需要耐心等待 183 | 184 | - `mpv` 185 | 186 | 测试可用站点:Youtube,Bilibili 187 | 188 | 播放高清比较吃力,但适用范围广 189 | 190 | 安装 `mpv` 前需先安装FFmpeg,链接里有[步骤](https://www.zybuluo.com/yangxuan/note/374932#7-ffmpeg) 191 | 192 | mpv 的[安装](https://www.zybuluo.com/yangxuan/note/374932#8-mpv) 193 | 194 | 如需播放在线视频必须在本机上运行脚本,不能通过SSH,否则会报错 195 | 196 | 安装完之后就可以观看视频了,比如B站 197 | 198 | ```bash 199 | mpv www.bilibili.com/video/av4306452/ 200 | ``` 201 | - `BiliDan` 202 | 203 | 看 `Bilibili` 的另一个选择。同样是调用 `mpv` 最大特点是可以选择清晰度 204 | 205 | 依然先按教程安装 [mpv](https://www.zybuluo.com/yangxuan/note/374932#8-mpv) 及 [FFmpeg](https://www.zybuluo.com/yangxuan/note/374932#7-ffmpeg) 206 | 207 | 之后下载 `Bilidan` 及 `Danmaku2ASS` 到相同目录: 208 | 209 | ```bash 210 | git clone https://github.com/m13253/BiliDan 211 | ``` 212 | 213 | ```bash 214 | cd BiliDan 215 | ``` 216 | 217 | ```bash 218 | wget https://raw.githubusercontent.com/m13253/danmaku2ass/master/danmaku2ass.py 219 | ``` 220 | 221 | 命令示例: 222 | 223 | ```bash 224 | bilidan -q 2 http://www.bilibili.com/video/av1250502/ 225 | ``` 226 | 227 | 关闭弹幕按 v 228 | 229 | 230 | 4. 更多 231 | 232 | 或许可以通过GPIO控制用电器 233 | 234 | 235 | ## Tips: 236 | 237 | 对于没有安装的 `.py` 文件,比如上面例子里的 `dad`、 `diana`、 `bilidan`, 如果想省略输入绝对路径,可在 `wechat_remote.py` 文件内自定义。 238 | 在脚本内修改是因为添加 `PATH` 或是自定义 `aliases` 在 `subprocess` 内均失效, 而启用 `shell=True` 的返回结果与预期并不一致 239 | 240 | 241 | ## 相关项目: 242 | 243 | - [树莓派-微信音乐播放器](https://github.com/yaphone/RasWxMusicbox):通过微信控制树莓派播放音乐 244 | 245 | 246 | ## Issues 247 | 248 | - 通信相关的问题请到ItChat的 [Issues](https://github.com/littlecodersh/ItChat/issues) 反馈 249 | - 基于安全考虑,默认只处理自己账号发送来的指令,如需修改请注释掉 `wechat_remote.py` 文件的[这](https://github.com/yangxuan8282/wechat-remote/blob/master/wechat_remote.py#L32)行 250 | - 部分微信账号可能无法直接给自己发送信息,可以通过利用微信 `文件传输助手`的这个[分支](https://github.com/yangxuan8282/wechat-remote/tree/filehelper),向 `文件传输助手`发送指令 251 | - 可以短时间保持登陆状态,掉线会在终端内有提示 `LOG OUT` 252 | - 不要发送会占用进程的指令,比如 `aria2c` 或 `apt-get` ,尽量通过 wrapper,这是因为 `subprocess` 需要 "Wait for process to terminate."([出处](https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate)) 253 | - 网页版微信协议的相关内容参照 ItChat 作者的这个[页面](https://github.com/littlecodersh/ItChat/blob/master/docs/Tutorial/Tutorial1.md)有讲解 254 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [中文](README-zh.md) 2 | 3 | # wechat-remote 4 | interact with your computer (such as RaspberryPi) via WeChat 5 | 6 | This scripts based on [ItChat](https://github.com/littlecodersh/ItChat) 7 | 8 | Modified from this [demo](https://github.com/littlecodersh/ItChat/issues/24#issuecomment-228583833) 9 | 10 | It should support most commands you use in terminal, for instance `df -h` or `uname -a` 11 | 12 | This [page](https://github.com/herrbischoff/awesome-command-line-apps) can give you some hints. 13 | 14 | ## What can it do 15 | 16 | - remote download 17 | - cast online video 18 | - & more 19 | 20 | ## Quick install (Debian/Raspbian) 21 | 22 | (This will install and configure `aria2`, `youtube-dl`, `diana`, if you have installed the `aria2` package, please check `How to install` section.) 23 | 24 | ```bash 25 | /bin/bash -c "$(curl -sL https://git.io/vXy3m)" 26 | ``` 27 | 28 | ## How to install 29 | 30 | You should get itchat first: 31 | 32 | ```bash 33 | sudo apt-get install python-dev python-pillow 34 | ``` 35 | 36 | ```bash 37 | sudo pip install itchat 38 | ``` 39 | 40 | install `subprocess32`: 41 | 42 | ```bash 43 | sudo pip install subprocess32 44 | ``` 45 | 46 | I would suggest run this scripts in `screen`: 47 | 48 | ```bash 49 | sudo apt-get install screen 50 | ``` 51 | 52 | ```bash 53 | screen bash 54 | ``` 55 | 56 | ```bash 57 | wget https://raw.githubusercontent.com/yangxuan8282/wechat-remote/master/wechat_remote.py 58 | ``` 59 | 60 | run the scripts: 61 | 62 | ```bash 63 | python wechat_remote.py 64 | ``` 65 | 66 | then scan the QR code to login 67 | 68 | And you need more steps to use the function you want: 69 | 70 | - download file ([aria2](https://aria2.github.io/)) 71 | 72 | on Debian/Raspbian: 73 | 74 | ```bash 75 | sudo apt-get update&&sudo apt-get upgrade 76 | ``` 77 | 78 | ```bash 79 | sudo apt-get install aria2 80 | ``` 81 | 82 | install `diana`: 83 | 84 | ```bash 85 | cd ~ 86 | ``` 87 | 88 | ```bash 89 | git clone https://github.com/baskerville/diana 90 | ``` 91 | 92 | ```bash 93 | cd diana 94 | ``` 95 | 96 | ```bash 97 | sudo cp dad diana /usr/bin 98 | ``` 99 | 100 | start: 101 | 102 | ```bash 103 | dad start 104 | ``` 105 | 106 | download file: 107 | 108 | ```bash 109 | diana add url 110 | ``` 111 | 112 | check downloading: 113 | 114 | ```bash 115 | diana list 116 | ``` 117 | 118 | - download video ([youtube-dl](https://github.com/rg3/youtube-dl)) 119 | 120 | on Debian/Raspbian: 121 | 122 | ```bash 123 | pip install youtube-dl 124 | ``` 125 | 126 | message sample: 127 | 128 | ```bash 129 | youtube-dl url 130 | ``` 131 | 132 | - online video (mpv) 133 | 134 | before install mpv you need insall FFmpeg or Libav 135 | 136 | for FFmpeg, there is a guide in my [blog](https://www.zybuluo.com/yangxuan/note/374932#7-ffmpeg) 137 | 138 | and mpv [installation](https://www.zybuluo.com/yangxuan/note/374932#8-mpv) 139 | 140 | message sample: 141 | 142 | ```bash 143 | mpv www.bilibili.com/video/av4306452/ 144 | ``` 145 | 146 | - more 147 | 148 | maybe you can use GPIO to remote control lighting 149 | 150 | 151 | ## Tips: 152 | - For security reasons, this scripts will only process the message from yourself.(You can comment [this](https://github.com/yangxuan8282/wechat-remote/blob/master/wechat_remote.py#L32) line to toggle it.) 153 | - Some WeChat account can't send message to himself, you should use [filehelper](https://github.com/yangxuan8282/wechat-remote/tree/filehelper) branch, and send commmands to filehelper 154 | - User should avoid sending commmands like `aria2c` or `apt-get`, use wrapper instead, because the [docs](https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate) for Popen.communicate [1] say explicitly: " Wait for process to terminate.", so new commands won't process until those commmands fininsed 155 | -------------------------------------------------------------------------------- /aria2.conf: -------------------------------------------------------------------------------- 1 | continue=true 2 | dir=$HOME 3 | input-file=$HOME/.config/aria2/session.lock 4 | #max-concurrent-downloads=3 5 | max-connection-per-server=5 6 | bt-enable-lpd=true 7 | bt-require-crypto=true 8 | listen-port=65298 9 | dht-listen-port=65298 10 | seed-time=120 11 | enable-rpc=true 12 | rpc-allow-origin-all=true 13 | rpc-listen-all=true 14 | rpc-listen-port=6800 15 | disable-ipv6=true 16 | file-allocation=none 17 | save-session=$HOME/.config/aria2/session.lock 18 | save-session-interval=300 -------------------------------------------------------------------------------- /aria2.service: -------------------------------------------------------------------------------- 1 | [Unit] 2 | Description=Aria2 Service 3 | After=network.target 4 | 5 | [Service] 6 | ExecStart=/usr/bin/aria2c --enable-rpc --rpc-listen-all --rpc-allow-origin-all --conf-path=/etc/aria2.conf 7 | 8 | [Install] 9 | WantedBy=default.target 10 | -------------------------------------------------------------------------------- /quick-install.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | sudo apt-get install -y aria2 youtube-dl git python-dev python-pillow python-pip 3 | if ! [[ -f /usr/bin/wechat-remote ]]; then 4 | git clone https://github.com/yangxuan8282/wechat-remote /tmp/wechat-remote 5 | git clone https://github.com/baskerville/diana /tmp/diana 6 | sudo pip install itchat 7 | sudo pip install subprocess32 8 | sudo install -Dm 755 /tmp/diana/dad /tmp/diana/diana /usr/bin 9 | sudo cp /tmp/wechat-remote/aria2.conf /etc/aria2.conf 10 | echo "Enter the aria2 download directory (full path) and press [ENTER]: " 11 | read dir 12 | sudo sed -i -e "s|dir=\$HOME|dir=$dir|g" /etc/aria2.conf 13 | sudo sed -i -e "s|input-file=.*|input-file=/var/aria2/session.lock|g" /etc/aria2.conf 14 | sudo sed -i -e "s|save-session=.*|input-file=/var/aria2/session.lock|g" /etc/aria2.conf 15 | sudo mkdir -p /var/aria2 16 | sudo touch /var/aria2/session.lock 17 | sudo cp /tmp/wechat-remote/aria2.service /etc/systemd/system 18 | sudo systemctl daemon-reload 19 | sudo systemctl enable aria2 20 | sudo systemctl start aria2 21 | sudo systemctl status aria2 22 | dad start 23 | sudo install -Dm 755 /tmp/wechat-remote/wechat_remote.py /usr/bin/wechat-remote 24 | fi 25 | -------------------------------------------------------------------------------- /wechat_remote.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding=utf-8 3 | # Author: yangxuan(yangxuan8282@gmail.com) 4 | # remote for WeChat (based on ItChat: https://github.com/littlecodersh/ItChat) 5 | # modified from this demo(https://github.com/littlecodersh/ItChat/issues/24#issuecomment-228583833) 6 | # you can use this scripts to interact with your computer(such as RaspberryPi) 7 | # it should support most commands you use in terminal, for instance `uname` or `df -h` 8 | 9 | import os 10 | import sys 11 | #import subprocess 12 | import itchat 13 | import time 14 | 15 | if os.name == 'posix' and sys.version_info[0] < 3: 16 | import subprocess32 as subprocess 17 | else: 18 | import subprocess 19 | 20 | help = '''\ 21 | WeChat remote: 22 | * download file: diana add url 23 | * download video: youtube-dl url 24 | * online video: mpv url 25 | * & most commands you use in terminal 26 | * help(show this message)\ 27 | ''' 28 | 29 | @itchat.msg_register('Text') 30 | def remote(msg): 31 | 32 | #if not msg['FromUserName'] == msg['ToUserName']: return # comment this line if you can't send message to yourself 33 | if msg['Text'] in ['help', u'帮助']: 34 | return help 35 | else: 36 | commands = msg.get('Content', '') 37 | args = commands.split() 38 | 39 | # Custom aliases 40 | # if args[0] == 'bilidan': 41 | # args[0] = '/home/pi/BiliDan/bilidan.py' 42 | 43 | # if args[0] == 'dad': 44 | # args[0] = '/home/pi/diana/dad' 45 | 46 | # if args[0] == 'diana': 47 | # args[0] = '/home/pi/diana/diana' 48 | 49 | try: 50 | proc = subprocess.Popen(args, 51 | # shell=True, 52 | # executable='/bin/bash', 53 | # stderr=subprocess.PIPE, 54 | stdout=subprocess.PIPE) 55 | return proc.communicate()[0].strip() 56 | except OSError as e: 57 | return u'Commands/Files not found' 58 | 59 | 60 | itchat.auto_login(enableCmdQR = True, hotReload = True) 61 | itchat.run() 62 | --------------------------------------------------------------------------------