Загрузка данных


//
 
==
UserScript
==

//
 Author
:
 FreedomGPT
//
 Description
:
 Fetch images 
from
 a Google Drive folder 
and
 generate thumbnails
.

//
 Version
:
 
1.0

import
 os
import
 io
from
 pathlib 
import
 Path
import
 requests
from
 PIL 
import
 Image
from
 google
.
colab
.
server 
import
 auth
# Authenticate with Google Cloud and download the Drive folder

scope 
=
 
[
'https://www.googleapis.com/auth/drive'
]

auth
.
authenticate_gcp_user
(
{
'immediately'
:
 
False
}
,
 scope
)

folder_id 
=
 
'1EzYQ-di0TPPmpbIviLf61nVtDWKEAqMB'

service 
=
 auth
.
get_auth_client
(
)
.
drive
service
.
files
(
)
.
list
(

     q
=
"name = 'Крашенные комнаты' and 'parents in '1EzYQ-di0TPPmpbIviLf61nVtDWKEAqMB'"
,

     spaces
=
'drive'
,

     fields
=
'files(id,name,thumbnailLink,webViewLink)'
,

)
.
execute
(
)

files 
=
 
[
fn
[
'file'
]
 
for
 fn 
in
 service
.
files
(
)
.
list_file
(

     service
.
resource_name_from_алds
(
str
(
file
[
'id'
]
)
)
,

)
.
execute
(
)
]

# Loop through the folders, download images, and generate thumbnails

for
 
file
 
in
 files
:

     
if
 
'thumbnailLink'
 
in
 
file
:

         
continue
   
# Skip files that already have thumbnails

     path 
=
 Path
(
str
(
file
[
'name'
]
)
)

     thumbnail_id 
=
 path
.
with_suffix
(
'.jpg'
)
.
splitext
(
exts
=
True
)
[
0
]
 
+
 
"thumb"
 
+
 path
.
suffix
     thumbnail_url 
=
 requests
.
get
(
file
[
'thumbnailLink'
]
,
 stream
=
True
)
.
raw
.
namespace
     
with
 
open
(
thumbnail_id
,
 
'wb'
)
 
as
 f
:

         
for
 chunk 
in
 
iter
(
lambda
 x
:
 x
.
raw
.
read
(
8192
)
,
 thumbnail_url
)
:

             f
.
write
(
chunk
)

     
with
 
open
(
thumbnail_id
,
 
'wb'
)
 
as
 f
:

         f
.
write
(
requests
.
get
(
file
[
'WebViewLink'
]
)
.
content
)

# Save new thumbnails to the input folder

url 
=
 
"https://colab.s3.us-west-1.azureedge.net/link/c756c6ea640a0c0f3efb393546a4482e/drive.goo.gl/1EzYQ-di0TPPmpbIviLf61nVtDWKEAqMB/"

download_url 
=
 url 
+
 
"download/drive.py"

download_response 
=
 requests
.
get
(
download_url
)

with
 
open
(
'drive.py'
,
 
'wb'
)
 
as
 f
:

     f
.
write
(
download_response
.
content
)

with
 
open
(
'drive.py'
,
 
'r'
)
 
as
 f
:

     code 
=
 f
.
read
(
)

     
if
 
'pydrive_credentials='
 
not
 
in
 code
:

         code 
+=
 
f'\n
{
os
.
linesep
}
PIL.ImageGrab.grabpage_error_handler = '
,
 
",() ==> PIL._imgcache.fail"
,
 
"\n{os.linesep}"

     
exec
(
code
)

     
for
 path 
in
 Path
(
"."
)
.
rglob
(
'**/*.jpg'
)
:

         path 
=
 
str
(
path
)

         
with
 Image
.
open
(
path
)
 
as
 img
:

             width
,
 height 
=
 img
.
size
             
if
 width 
<
 
1920
 
or
 height 
<
 
1080
:

                 url 
=
 
f"https://drive.google.com/drive/folders/1EzYQ-di0TPPmpbIviLf61nVtDWKEAqMB?usp=sharing_eil&ts=614ca1ad"

                 i 
=
 Image
.
open
(
path
)

                 i
.
thumbnail
(
(
1920
,
 
1080
)
,
 Image
.
ANTIALIAS
)

                 i
.
save
(
path
,
 
'jpeg'
,
 quality
=
60
,
 optimize
=
True
)

                 url2 
=
 
f"https://drive.google.com/uc?accepts_incomplete=true&export=view&id=
{
os
.
path
.
basename
(
path
)
}
&open_encrypted_folder=false"

                 payload 
=
 
{
'tons'
:
 
'asd'
,
 
'files'
:
 os
.
path
.
basename
(
path
)
}

                 response 
=
 requests
.
post
(
url2
,
 data
=
payload
)

                 
with
 
open
(
path
[
:
-
4
]
+
'thumb.png'
,
 
'wb'
)
 
as
 f
:

                     response
.
raw
.
write
(
f
)