From 49777095ade5b84701106b9cd525ac8b20acbfbf Mon Sep 17 00:00:00 2001 From: Anson Date: Sun, 22 Aug 2021 17:19:58 -0700 Subject: [PATCH] Initial commit --- .devcontainer/devcontainer.json | 25 +++++++++ .gitlab-ci.yml | 96 +++++++++++++++++++++++++++++++++ Dockerfile | 10 ++++ bot.py | 47 ++++++++++++++++ requirements.txt | 5 ++ 5 files changed, 183 insertions(+) create mode 100644 .devcontainer/devcontainer.json create mode 100644 .gitlab-ci.yml create mode 100644 Dockerfile create mode 100644 bot.py create mode 100644 requirements.txt diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 0000000..ea7ec83 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -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" +} \ No newline at end of file diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..0dc8941 --- /dev/null +++ b/.gitlab-ci.yml @@ -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 diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..c3d1083 --- /dev/null +++ b/Dockerfile @@ -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" ] diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..94d76c2 --- /dev/null +++ b/bot.py @@ -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() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..08a01a6 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +# black +python-telegram-bot==13.7 +youtube-dl==2021.6.6 +requests==2.26.0 +requests-toolbelt==0.9.1 \ No newline at end of file