コード例 #1
0
 def update_status(self, timestamp=strftime('%Y%m%d_%H%M%S')):
     log.info('Updating status from %s', self.url)
     response = requests.get(self.url)
     if response.status_code == 200:
         self._previous_status = self.status
         response_text = response.text
         self.status = _map_status_to_model(json.loads(response_text))
         log.info('Loaded status successfully')
         if self.has_status_changed():
             (fd, path) = mkstemp('.json', 'tfltree_%s_' % timestamp)
             with open(fd, 'w') as f:
                 f.write(response_text)
                 log.debug('Wrote API response to %s', path)
     else:
         log.warning('Received status %s from API. Returning previous one', response.status_code)
     return self.status
コード例 #2
0
ファイル: app.py プロジェクト: danielthepope/tfl-tree
def main():
    camera = Camera()
    status_light.blink(0.02, 9.98)
    leds = lights.start_leds()
    while True:
        timestamp = strftime('%Y%m%d_%H%M%S')
        status = API.update_status(timestamp)
        lights.show_all_line_statuses(leds, status)
        if API.has_status_changed():
            log.info('Status is different')
            log.debug(status)
            lights.lamp_on()
            audio_statuses = speech.generate_audio_files(status, timestamp)
            log.debug('Audio files: %r', audio_statuses)
            total_duration = sum([f.duration_ms for f in audio_statuses])
            log.info('Total duration: %sms', total_duration)
            subtitle_file = subtitle.convert_to_srt_file(
                audio_statuses, timestamp)
            lights.play_a_sequence(leds, audio_statuses, 10)
            video_file = camera.record_for_seconds(total_duration / 1000 + 10,
                                                   timestamp)
            audio_filenames = [f.file_path for f in audio_statuses]
            lights.lamp_off()
            status_light.blink()
            log.info('Packaging MP4')
            packaged_file = video.package_mp4(video_file, audio_filenames,
                                              timestamp)
            status_light.blink(0.9, 0.1)
            tweet_text = tweets.generate_tweet_text(audio_statuses)
            tweets.post_video(tweet_text, packaged_file, subtitle_file)
            status_light.blink(0.02, 9.98)
            sleep(1200)
        else:
            log.debug('Status is the same')
            sleep(120)
コード例 #3
0
ファイル: tweets.py プロジェクト: danielthepope/tfl-tree
def post_video(tweet_text, video_file, subtitle_file):
    if enabled:
        api = twitter.Api(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN_KEY,
                          ACCESS_TOKEN_SECRET)

        log.debug('Uploading %s to Twitter', video_file)
        video_id = api.UploadMediaChunked(video_file,
                                          media_category='TweetVideo')
        log.debug('Video ID: %s', video_id)
        log.debug('Uploading %s to Twitter', subtitle_file)
        subtitle_id = api.UploadMediaChunked(subtitle_file,
                                             media_category='Subtitles')
        log.debug('Subtitle ID: %s', subtitle_id)
        api.PostMediaSubtitlesCreate(video_id, subtitle_id, 'en', 'English')
        log.debug('Successfully associated video with subtitles')
        attempts = 0
        twitter_error = None
        while attempts < 15:
            try:
                status = api.PostUpdate(tweet_text, media=video_id)
                log.info('Uploaded to Twitter successfully!')
                log.debug(status)
                return
            except TwitterError as e:
                attempts += 1
                log.warn(
                    'Error from Twitter after %s attempts. Trying a bit later',
                    attempts)
                sleep(5)
                twitter_error = e
        # Exceeded the maximum attempts
        log.error(
            'Twitter upload exceeded the maximum number of attempts. Giving up.'
        )
        log.error(twitter_error)

    else:
        log.warning('Twitter upload not enabled. Requires API tokens.')
コード例 #4
0
FRAMES_PER_SECOND = int(os.getenv('FRAMES_PER_SECOND', 15))
BRIGHTNESS = float(os.getenv('BRIGHTNESS', 0.1))
PIXEL_COUNT = int(os.getenv('PIXEL_COUNT', 25))
LED_SIZE = int(os.getenv('LED_SIZE', 25))
LED_ENDPOINT = os.getenv('LED_ENDPOINT')

# LAMP
LAMP_ON_COMMAND = os.getenv('LAMP_ON_COMMAND', 'echo lamp on')
LAMP_OFF_COMMAND = os.getenv('LAMP_OFF_COMMAND', 'echo lamp off')

# TWITTER
UPLOAD_FIRST = int(os.getenv('UPLOAD_FIRST', 0))
CONSUMER_KEY = os.getenv('CONSUMER_KEY')
CONSUMER_SECRET = os.getenv('CONSUMER_SECRET')
ACCESS_TOKEN_KEY = os.getenv('ACCESS_TOKEN_KEY')
ACCESS_TOKEN_SECRET = os.getenv('ACCESS_TOKEN_SECRET')

log.info('Configuration:')
log.info('FRAMES_PER_SECOND: %s', FRAMES_PER_SECOND)
log.info('BRIGHTNESS: %s', BRIGHTNESS)
log.info('PIXEL_COUNT: %s', PIXEL_COUNT)
log.info('LED_SIZE: %s', LED_SIZE)
log.info('LED_ENDPOINT: %s', LED_ENDPOINT)
log.info('UPLOAD_FIRST: %s', UPLOAD_FIRST)
log.info('LAMP_ON_COMMAND: %s', LAMP_ON_COMMAND)
log.info('LAMP_OFF_COMMAND: %s', LAMP_OFF_COMMAND)
log.info('CONSUMER_KEY: %s', CONSUMER_KEY)
log.info('CONSUMER_SECRET: %s', CONSUMER_SECRET)
log.info('ACCESS_TOKEN_KEY: %s', ACCESS_TOKEN_KEY)
log.info('ACCESS_TOKEN_SECRET: %s', ACCESS_TOKEN_SECRET)
コード例 #5
0
ファイル: lights.py プロジェクト: danielthepope/tfl-tree
    log.debug(LAMP_ON_COMMAND)
    os.system(LAMP_ON_COMMAND)
    log.debug('done')


def lamp_off():
    log.debug(LAMP_OFF_COMMAND)
    os.system(LAMP_OFF_COMMAND)
    log.debug('done')


if __name__ == '__main__':
    from tfltree.raspberrypi import tfl, speech
    from time import strftime

    log.info('Hello')
    timestamp = strftime('%Y%m%d_%H%M%S')
    statuses = tfl.TflApi().update_status(timestamp)
    audio_statuses = speech.generate_phrases_for_status(statuses)
    for s in audio_statuses:
        s.duration_ms = 5000

    log.info('Starting LEDs')
    leds = start_leds()
    play_a_sequence(leds, audio_statuses, 10)

    try:
        while True:
            sleep(1)
    except Exception as e:
        leds.all_off()
コード例 #6
0
ファイル: tweets.py プロジェクト: danielthepope/tfl-tree
        twitter_error = None
        while attempts < 15:
            try:
                status = api.PostUpdate(tweet_text, media=video_id)
                log.info('Uploaded to Twitter successfully!')
                log.debug(status)
                return
            except TwitterError as e:
                attempts += 1
                log.warn(
                    'Error from Twitter after %s attempts. Trying a bit later',
                    attempts)
                sleep(5)
                twitter_error = e
        # Exceeded the maximum attempts
        log.error(
            'Twitter upload exceeded the maximum number of attempts. Giving up.'
        )
        log.error(twitter_error)

    else:
        log.warning('Twitter upload not enabled. Requires API tokens.')


if __name__ == "__main__":
    from tfltree.raspberrypi import tfl, speech
    status = tfl.TflApi().update_status()
    audio_statuses = speech.generate_phrases_for_status(status)
    tweet_text = generate_tweet_text(audio_statuses)
    log.info(tweet_text)