Skip to content

Commit

Permalink
Removed image downloading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
npanuhin committed Dec 16, 2023
1 parent 64467ea commit 5b670ff
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Stages (roughly in order of importance):
- [x] Upload alll images to storage
- [x] Replace spaces by `\t` in api to reduce space
- [ ] Fix metadata for all images (currently done: ?/?)
- [ ] Finally remove all images from this repo and reduce the size of repo (+ number of commits in repo)
- [x] Finally remove all images from this repo and reduce the size of repo (+ number of commits in repo)
- [ ] Write a comprehensive README
- [ ] Enable other countries
- [ ] Improve website
Expand Down
6 changes: 1 addition & 5 deletions src/Region.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@ def __init__(self, region):
self.lang, self.country = map(str.lower, region.split('-'))
self.path = mkpath(os.path.dirname(__file__), '../api', self.country.upper())
self.api_path = mkpath(self.path, self.country.lower() + '.json')
self.images_path = mkpath(self.path, 'images')

os.makedirs(self.images_path, exist_ok=True)

self.relative_path = self.country.upper()
self.relative_images_path = mkpath(self.relative_path, 'images')
self.gcloud_images_path = mkpath(self.country.upper(), 'images')

@property
def mkt(self):
Expand Down
16 changes: 11 additions & 5 deletions src/bing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import os

import requests

Expand Down Expand Up @@ -62,8 +63,6 @@ def update(region: Region):

common_params = {'mkt': region.mkt, 'setlang': region.lang, 'cc': region.country}

# os.makedirs(region.images_path, exist_ok=True)

api_by_date = {item['date']: item for item in region.read_api()}

to_download = set()
Expand Down Expand Up @@ -151,15 +150,22 @@ def update(region: Region):
# ------------------------------------------------------------------------------------------------------------------
print('Downloading images and uploading to Google Cloud...')

os.makedirs('_temp', exist_ok=True)

for date in sorted(to_download):
filename = date + '.jpg'
image_path = mkpath(region.images_path, filename)
api_by_date[date]['path'] = posixpath(mkpath(region.relative_images_path, filename))
image_path = mkpath('_temp', filename)

with open(image_path, 'wb') as file:
file.write(requests.get(api_by_date[date]['bing_url']).content)

api_by_date[date]['url'] = gcloud.upload_file(image_path, api_by_date[date]['path'], skip_exists=True)
api_by_date[date]['url'] = gcloud.upload_file(
image_path, posixpath(mkpath(region.gcloud_images_path, filename)), skip_exists=True
)

os.remove(image_path)

os.rmdir('_temp')

# ------------------------------------------------------------------------------------------------------------------

Expand Down
9 changes: 5 additions & 4 deletions src/scripts/upload_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import os

sys.path.append('../')
from utils import mkpath, posixpath # noqa: E402
from Region import REGIONS # noqa: E402
from gcloud import GCloud # noqa: E402
from utils import mkpath # noqa: E402


def main():
Expand All @@ -14,9 +14,10 @@ def main():
api = region.read_api()

for item in api:
image_path = mkpath(region.images_path, item['date'] + '.jpg')
if os.path.isfile(image_path):
item['url'] = gcloud.upload_file(image_path, item['path'], skip_exists=True)
if os.path.isfile(item['path']):
item['url'] = gcloud.upload_file(
item['path'], posixpath(mkpath(region.gcloud_images_path, item['date'] + '.jpg')), skip_exists=True
)

region.write_api(api)

Expand Down

0 comments on commit 5b670ff

Please sign in to comment.