Skip to content

Commit e08cfff

Browse files
Patch for images
1 parent 2bda0b6 commit e08cfff

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

tcsocket/app/management.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import logging
22
from contextlib import contextmanager
3+
from io import BytesIO
34
from time import sleep
45
from typing import Union
56

7+
import boto3
68
import psycopg2
7-
from sqlalchemy import create_engine, update
9+
import requests
10+
from sqlalchemy import create_engine, select, update
11+
from sqlalchemy.sql import functions
812

9-
from .models import Base, sa_companies
13+
from .models import Base, sa_companies, sa_contractors
1014
from .settings import Settings
1115

1216
logger = logging.getLogger('socket')
@@ -260,3 +264,39 @@ def run_sql_prepare(conn):
260264
run SQL_PREPARE code to (re)create procedures and triggers
261265
"""
262266
conn.execute(SQL_PREPARE)
267+
268+
269+
@patch
270+
def update_socket_images(conn):
271+
"""
272+
Downloading images from server on EC2 and uploading them to S3
273+
"""
274+
con_c = sa_contractors.c
275+
company_c = sa_companies.c
276+
base_url = 'https://socket.tutorcruncher.com/media'
277+
q_iter = select([con_c.id, company_c.public_key]).select_from(sa_contractors.join(sa_companies))
278+
session = requests.Session()
279+
settings = Settings()
280+
s3_client = boto3.client(
281+
's3', aws_access_key_id=settings.aws_access_key, aws_secret_access_key=settings.aws_secret_key
282+
)
283+
count = conn.execute(select([functions.count(con_c.id)]).select_from(sa_contractors))
284+
print(f'Processing images for {count.first()[0]} contractors')
285+
for row in conn.execute(q_iter):
286+
img_key = f'{row.public_key}/{row.id}.jpg'
287+
r = session.get(f'{base_url}/{img_key}')
288+
r.raise_for_status()
289+
with BytesIO() as temp_file:
290+
temp_file.write(r.content)
291+
temp_file.seek(0)
292+
s3_client.upload_fileobj(Fileobj=temp_file, Bucket=settings.aws_bucket_name, Key=img_key)
293+
print(f'Uploading image {img_key}')
294+
295+
img_thumb_key = f'{row.public_key}/{row.id}.thumb.jpg'
296+
r = session.get(f'{base_url}/{img_thumb_key}')
297+
r.raise_for_status()
298+
with BytesIO() as temp_file:
299+
temp_file.write(r.content)
300+
temp_file.seek(0)
301+
s3_client.upload_fileobj(Fileobj=temp_file, Bucket=settings.aws_bucket_name, Key=img_thumb_key)
302+
print(f'Uploading image {img_thumb_key}')

tcsocket/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ ipython==7.15.0
1515
pgcli==3.0.0
1616
ipython-sql==0.4.0
1717
yarl==1.4.2
18+
requests==2.24.0

tests/requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,3 @@ pytest-isort==1.0.0
1212
pytest-mock==3.1.0
1313
pytest-sugar==0.9.3
1414
pytest-toolbox==0.4
15-
requests==2.24.0

0 commit comments

Comments
 (0)