1
0
mirror of https://gitlab.com/MisterBiggs/telegram-video-download-bot.git synced 2025-06-15 14:46:41 +00:00

Initial commit

This commit is contained in:
Anson 2021-08-22 17:19:58 -07:00
commit 49777095ad
5 changed files with 183 additions and 0 deletions

View File

@ -0,0 +1,25 @@
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/docker-existing-dockerfile
{
"name": "Existing Dockerfile",
// Sets the run context to one level up instead of the .devcontainer folder.
"context": "..",
// Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
"dockerFile": "../Dockerfile",
// Set *default* container specific settings.json values on container create.
"settings": {},
// Add the IDs of extensions you want installed when the container is created.
"extensions": [
"ms-python.python"
]
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Uncomment the next line to run commands after the container is created - for example installing curl.
// "postCreateCommand": "apt-get update && apt-get install -y curl",
// Uncomment when using a ptrace-based debugger like C++, Go, and Rust
// "runArgs": [ "--cap-add=SYS_PTRACE", "--security-opt", "seccomp=unconfined" ],
// Uncomment to use the Docker CLI from inside the container. See https://aka.ms/vscode-remote/samples/docker-from-docker.
// "mounts": [ "source=/var/run/docker.sock,target=/var/run/docker.sock,type=bind" ],
// Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root.
// "remoteUser": "vscode"
}

96
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,96 @@
#Following instructions (as of 2020-04-01): https://docs.gitlab.com/ee/ci/docker/using_kaniko.html
#Kaniko docs are here: https://github.com/GoogleContainerTools/kaniko
#While this example shows building to multiple registries for all branches, with a few modifications
# it can be used to build non-master branches to a "dev" container registry and only build master to
# a production container registry
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
variables:
#More Information on Kaniko Caching: https://cloud.google.com/build/docs/kaniko-cache
KANIKO_CACHE_ARGS: "--cache=true --cache-copy-layers=true --cache-ttl=24h"
VERSIONLABELMETHOD: "OnlyIfThisCommitHasVersion" # options: "OnlyIfThisCommitHasVersion","LastVersionTagInGit"
IMAGE_LABELS: >
--label org.opencontainers.image.vendor=$CI_SERVER_URL/$GITLAB_USER_LOGIN
--label org.opencontainers.image.authors=$CI_SERVER_URL/$GITLAB_USER_LOGIN
--label org.opencontainers.image.revision=$CI_COMMIT_SHA
--label org.opencontainers.image.source=$CI_PROJECT_URL
--label org.opencontainers.image.documentation=$CI_PROJECT_URL
--label org.opencontainers.image.licenses=$CI_PROJECT_URL
--label org.opencontainers.image.url=$CI_PROJECT_URL
--label vcs-url=$CI_PROJECT_URL
--label com.gitlab.ci.user=$CI_SERVER_URL/$GITLAB_USER_LOGIN
--label com.gitlab.ci.email=$GITLAB_USER_EMAIL
--label com.gitlab.ci.tagorbranch=$CI_COMMIT_REF_NAME
--label com.gitlab.ci.pipelineurl=$CI_PIPELINE_URL
--label com.gitlab.ci.commiturl=$CI_PROJECT_URL/commit/$CI_COMMIT_SHA
--label com.gitlab.ci.cijoburl=$CI_JOB_URL
--label com.gitlab.ci.mrurl=$CI_PROJECT_URL/-/merge_requests/$CI_MERGE_REQUEST_ID
get-latest-git-version:
stage: .pre
image:
name: alpine/git
entrypoint: [""]
rules:
- if: '$VERSIONLABELMETHOD == "LastVersionTagInGit"'
script:
- |
echo "the google kaniko container does not have git and does not have a packge manager to install it"
git clone https://github.com/GoogleContainerTools/kaniko.git
cd kaniko
echo "$(git describe --abbrev=0 --tags)" > ../VERSIONTAG.txt
echo "VERSIONTAG.txt contains $(cat ../VERSIONTAG.txt)"
artifacts:
paths:
- VERSIONTAG.txt
.build_with_kaniko:
#Hidden job to use as an "extends" template
stage: build
script:
- |
echo "Building and shipping image to $CI_REGISTRY_IMAGE"
#Build date for opencontainers
BUILDDATE="'$(date '+%FT%T%z' | sed -E -n 's/(\+[0-9]{2})([0-9]{2})$/\1:\2/p')'" #rfc 3339 date
IMAGE_LABELS="$IMAGE_LABELS --label org.opencontainers.image.created=$BUILDDATE --label build-date=$BUILDDATE"
#Description for opencontainers
BUILDTITLE=$(echo $CI_PROJECT_TITLE | tr " " "_")
IMAGE_LABELS="$IMAGE_LABELS --label org.opencontainers.image.title=$BUILDTITLE --label org.opencontainers.image.description=$BUILDTITLE"
#Add ref.name for opencontainers
IMAGE_LABELS="$IMAGE_LABELS --label org.opencontainers.image.ref.name=$CI_REGISTRY_IMAGE:$CI_COMMIT_REF_NAME"
#Build Version Label and Tag from git tag, LastVersionTagInGit was placed by a previous job artifact
if [[ "$VERSIONLABELMETHOD" == "LastVersionTagInGit" ]]; then VERSIONLABEL=$(cat VERSIONTAG.txt); fi
if [[ "$VERSIONLABELMETHOD" == "OnlyIfThisCommitHasVersion" ]]; then VERSIONLABEL=$CI_COMMIT_TAG; fi
if [[ ! -z "$VERSIONLABEL" ]]; then
IMAGE_LABELS="$IMAGE_LABELS --label org.opencontainers.image.version=$VERSIONLABEL"
ADDITIONALTAGLIST="$ADDITIONALTAGLIST $VERSIONLABEL"
fi
ADDITIONALTAGLIST="$ADDITIONALTAGLIST $CI_COMMIT_REF_NAME $CI_COMMIT_SHORT_SHA"
if [[ "$CI_COMMIT_BRANCH" == "$CI_DEFAULT_BRANCH" ]]; then ADDITIONALTAGLIST="$ADDITIONALTAGLIST latest"; fi
if [[ -n "$ADDITIONALTAGLIST" ]]; then
for TAG in $ADDITIONALTAGLIST; do
FORMATTEDTAGLIST="${FORMATTEDTAGLIST} --tag $CI_REGISTRY_IMAGE:$TAG ";
done;
fi
#Reformat Docker tags to kaniko's --destination argument:
FORMATTEDTAGLIST=$(echo "${FORMATTEDTAGLIST}" | sed s/\-\-tag/\-\-destination/g)
echo "Kaniko arguments to run: --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile $KANIKO_CACHE_ARGS $FORMATTEDTAGLIST $IMAGE_LABELS"
mkdir -p /kaniko/.docker
echo "{\"auths\":{\"$CI_REGISTRY\":{\"auth\":\"$(echo -n $CI_REGISTRY_USER:$CI_REGISTRY_PASSWORD | base64)\"}}}" > /kaniko/.docker/config.json
/kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile $KANIKO_CACHE_ARGS $FORMATTEDTAGLIST $IMAGE_LABELS
build-for-gitlab-project-registry:
extends: .build_with_kaniko
environment:
#This is only here for completeness, since there are no CI CD Variables with this scope, the project defaults are used
# to push to this projects docker registry
name: push-to-gitlab-project-registry

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM python:3.9-buster
COPY requirements.txt ./
RUN pip install --no-cache-dir -U -r requirements.txt
COPY . .
CMD [ "python", "./bot.py" ]

47
bot.py Normal file
View File

@ -0,0 +1,47 @@
import io
import os
import re
import requests as r
import youtube_dl
from requests_toolbelt.downloadutils import stream
from telegram import Update
from telegram.ext import (
CallbackContext,
CommandHandler,
Filters,
MessageHandler,
Updater,
)
URL_REGEX = r"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()!@:%_\+.~#?&\/\/=]*)"
def url_search(update: Update, context: CallbackContext) -> None:
msg = update.message.text.strip()
print(msg)
if re.fullmatch(URL_REGEX, msg):
ydl_opts = {}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
vid = ydl.extract_info(msg)
print(vid.keys())
b = io.BytesIO()
resp = r.get(vid["url"], stream=True)
file = stream.stream_response_to_file(resp, path=b)
b.seek(0)
try:
update.message.reply_video(b, caption=vid["title"])
except Exception:
update.message.reply_video(b)
updater = Updater(os.environ["TELEGRAM"])
updater.dispatcher.add_handler(CommandHandler("dl", url_search))
updater.dispatcher.add_handler(MessageHandler(Filters.text, url_search))
updater.start_polling()
print("bot running")
updater.idle()

5
requirements.txt Normal file
View File

@ -0,0 +1,5 @@
# black
python-telegram-bot==13.7
youtube-dl==2021.6.6
requests==2.26.0
requests-toolbelt==0.9.1