Changeset d6f8304 in OpenWorkouts-current for ow/utilities.py


Ignore:
Timestamp:
Feb 11, 2019, 6:55:55 PM (5 years ago)
Author:
Borja Lopez <borja@…>
Branches:
current, feature/docs, master
Children:
02aee97
Parents:
93bbb89
Message:

(#52) - screenshots of the workouts maps were corrupted randomly.

Replaced our screenshot_map shell script that was calling chrome headless
directly with some python code running splinter + selenium webdriver.

Using chromedriver in such environment we can first visit the map site
for the given workout, then wait a bit for it to completely load, then
take the screenshot.

I've also removed all traces of screenshot_map from the code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • ow/utilities.py

    r93bbb89 rd6f8304  
    33import logging
    44import calendar
    5 import subprocess
     5import shutil
     6import time
    67from datetime import datetime, timedelta
    78from decimal import Decimal
     
    1112from xml.dom import minidom
    1213from ZODB.blob import Blob
     14from splinter import Browser
     15
    1316
    1417log = logging.getLogger(__name__)
     
    192195
    193196
    194 def save_map_screenshot(workout):
     197def save_map_screenshot(workout, request):
     198
    195199    if workout.has_gpx:
     200
     201        map_url = request.resource_url(workout, 'map')
     202
     203        browser = Browser('chrome', headless=True)
     204        browser.driver.set_window_size(1300, 436)
     205
     206        browser.visit(map_url)
     207        # we need to wait a moment before taking the screenshot, to ensure
     208        # all tiles are loaded in the map.
     209        time.sleep(5)
     210
     211        # splinter saves the screenshot with a random name (even if we do
     212        # provide a name) so we get the path to that file and later on we
     213        # move it to the proper place
     214        splinter_screenshot_path = browser.screenshot()
     215
    196216        current_path = os.path.abspath(os.path.dirname(__file__))
    197         tool_path = os.path.join(current_path, '../bin/screenshot_map')
    198 
    199217        screenshots_path = os.path.join(
    200218            current_path, 'static/maps', str(workout.owner.uid))
     
    206224        screenshot_path += '.png'
    207225
    208         subprocess.run(
    209             [tool_path, str(workout.owner.uid), str(workout.workout_id),
    210              screenshot_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
     226        shutil.move(splinter_screenshot_path, screenshot_path)
    211227
    212228        return True
Note: See TracChangeset for help on using the changeset viewer.