Skip to content

Commit

Permalink
add telegram upload server
Browse files Browse the repository at this point in the history
  • Loading branch information
bakatrouble committed Jan 14, 2020
1 parent a48c02a commit 40506ab
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
21 changes: 21 additions & 0 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
bottle==0.12.18
certifi==2019.11.28
cffi==1.13.2
chardet==3.0.4
cryptography==2.8
decorator==4.4.1
future==0.18.2
idna==2.8
imageio==2.6.1
imageio-ffmpeg==0.3.0
moviepy==1.0.1
numpy==1.18.1
Pillow==7.0.0
proglog==0.1.9
pycparser==2.19
python-telegram-bot==12.3.0
requests==2.22.0
six==1.13.0
tornado==6.0.3
tqdm==4.41.1
urllib3==1.25.7
62 changes: 62 additions & 0 deletions server/telegram_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python3

import os
from tempfile import TemporaryDirectory

import telegram.ext
from PIL import Image
from bottle import route, run, request, BaseRequest, HTTPError, default_app
from moviepy.video.io.VideoFileClip import VideoFileClip

BaseRequest.MEMFILE_MAX = 1024 * 1024 * 100

BOT_TOKEN = ''
CHAT_ID = -1

# REQUEST_KWARGS = {
# 'proxy_url': 'socks5h://',
# 'urllib3_proxy_kwargs': {
# 'username': '',
# 'password': '',
# }
# }
REQUEST_KWARGS = None

u = telegram.ext.Updater(BOT_TOKEN, request_kwargs=REQUEST_KWARGS, use_context=True)
bot = u.bot


@route('/', method='POST')
def index():
if 'filename' not in request.params:
raise HTTPError(400, 'filename param is missing')
filename = os.path.basename(request.params['filename'])

with TemporaryDirectory() as d:
fpath = os.path.join(d, filename)
with open(fpath, 'wb') as f:
body = request.body
while True:
chunk = body.read(0xFFFF)
if not chunk:
break
f.write(chunk)
if filename.endswith('.jpg'):
bot.send_photo(CHAT_ID, open(fpath, 'rb'))
else:
thumb = os.path.join(d, 'thumb.jpg')
clip = VideoFileClip(fpath)
frame = clip.get_frame(t=1)
im = Image.fromarray(frame)
im.thumbnail((320, 320), Image.ANTIALIAS)
im.save(thumb)
bot.send_video(CHAT_ID, open(fpath, 'rb'), clip.duration,
width=clip.size[0], height=clip.size[1], supports_streaming=True,
thumb=open(thumb, 'rb'))
return 'OK'


if __name__ == '__main__':
run(host='0.0.0.0', port=8080)

app = default_app()

0 comments on commit 40506ab

Please sign in to comment.