1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyO80A9f3UeM4jwCCLOxzeSw",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/pinapelz/nc-media-tools/blob/main/Google_Drive_to_WebDAV.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"# Google Drive Video to WebDAV\n",
"This script allows for a GDrive video to be downloaded and then automatically uplaoded via WebDAV\n",
"\n",
"# What this does?\n",
"- Downloads GDrive video via yt-dlp\n",
"- Upload via WebDAV\n",
"\n",
"Optionally if you are using Nextcloud you can also automatically generate a share\n",
"\n",
"# Usage\n",
"Fill in all the fields below. Then `Run all`"
],
"metadata": {
"id": "L9cXBP9OPmFt"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "vHGZttQWK8QJ"
},
"outputs": [],
"source": [
"# Install the dependencies needed\n",
"!pip install webdavclient3"
]
},
{
"cell_type": "code",
"source": [
"from webdav3.client import Client\n",
"webdav_url = \"\" #@param {type:\"string\"}\n",
"username = \"\" #@param {type:\"string\"}\n",
"password = \"\" #@param {type:\"string\"}\n",
"options = {\n",
" 'webdav_hostname': webdav_url,\n",
" 'webdav_login': username,\n",
" 'webdav_password': password\n",
"}\n",
"client = Client(options)\n",
"try:\n",
" client.list()\n",
" print(\"Success! Connection successful\")\n",
"except:\n",
" print(\"Login failed. Please check that your login and WebDAV url are correct\")"
],
"metadata": {
"id": "GAdz5SlkOIJO"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"import re\n",
"import gdown\n",
"video_url = \"\" #@param{type:\"string\"}\n",
"file_id = re.search(r\"(?<=/d/)[^/]+\", video_url).group(0)\n",
"url = f\"https://drive.google.com/uc?id={file_id}\"\n",
"downloaded_file = gdown.download(url, quiet=False)\n",
"print(downloaded_file)"
],
"metadata": {
"id": "GtiPVBYSQ1_6"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Uploads the file to the path specified\n",
"download_path = \"\" #@param{type:\"string\"}\n",
"remote_filename = f\"{download_path}/\" + downloaded_file.split(\"/\")[-1]\n",
"client.upload_sync(remote_path=remote_filename, local_path=downloaded_file)\n",
"print(f\"Done! Your file has been uploaded to {remote_filename}\")"
],
"metadata": {
"id": "3xYJpTl5SeVz"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"# OPTIONAL NEXTCLOUD AUTO SHARE\n",
"- This uses Nextcloud API to create a public share URL automatically. Completes the workflow"
],
"metadata": {
"id": "uabtse49Vy-L"
}
},
{
"cell_type": "code",
"source": [
"import requests\n",
"nextcloud_url = \"\" #@param {type:\"string\"}\n",
"api_url = f\"{nextcloud_url}/ocs/v2.php/apps/files_sharing/api/v1/shares\"\n",
"payload = {\n",
" \"path\": remote_filename,\n",
" \"shareType\": 3, # 3 = public link\n",
" \"permissions\": 31 # optional, full permissions (read/write/share)\n",
"}\n",
"\n",
"headers = {\n",
" \"OCS-APIRequest\": \"true\"\n",
"}\n",
"response = requests.post(api_url, auth=(username, password), headers=headers, data=payload)\n",
"if response.status_code == 200:\n",
" import xml.etree.ElementTree as ET\n",
" root = ET.fromstring(response.content)\n",
" ns = {\"ocs\": \"http://open-collaboration-services.org/ns\"}\n",
" url_elem = root.find(\".//url\")\n",
" if url_elem is not None:\n",
" share_link = url_elem.text\n",
" print(\"Share link:\", share_link+\"/download\")\n",
" else:\n",
" print(\"Could not find share URL in response\")\n",
"else:\n",
" print(\"Error:\", response.status_code, response.text)"
],
"metadata": {
"id": "MSmhsS6PUxaG"
},
"execution_count": null,
"outputs": []
}
]
}
|