Commit c2b449fa authored by Antoine Lima's avatar Antoine Lima
Browse files

Image downloader (not working..)

parent 283f06ef
......@@ -35,6 +35,7 @@ if __name__=='__main__':
from Babyfut.modules import GameModule
from Babyfut.core.player import Side
from Babyfut.core.input import GPIOThread
from Babyfut.core.downloader import Downloader
from Babyfut.core.database import Database
from Babyfut.core.replay import Replay as ReplayThread
......@@ -56,9 +57,13 @@ if __name__=='__main__':
threadGPIO = GPIOThread(myapp)
threadGPIO.start()
threadDownloader = Downloader.instance()
threadDownloader.start()
myapp.show()
app.exec_()
threadDownloader.stop()
threadGPIO.stop()
if ReplayThread.isCamAvailable():
......@@ -66,6 +71,7 @@ if __name__=='__main__':
threadReplay.join()
threadGPIO.join()
threadDownloader.join()
finally:
GPIOThread.clean()
......
......@@ -6,6 +6,8 @@
import sqlite3
from Babyfut.babyfut import getContent
class DatabaseError(Exception):
pass
......@@ -14,7 +16,6 @@ class Database():
def __init__(self):
if not Database.__db:
from Babyfut.babyfut import getContent
db_path = getContent('babyfut.sqlite')
self._connection = sqlite3.connect(db_path)
......@@ -23,7 +24,6 @@ class Database():
'''
Singleton
'''
if not Database.__db:
Database.__db = Database()
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 18 18:34:40 2018
@author: Antoine Lima, Leo Reynaert, Domitille Jehenne
"""
import os
import logging
import urllib.request
from time import sleep
from threading import Thread
from PyQt5.QtCore import QObject, pyqtSignal, pyqtSlot
class Downloader(Thread, QObject):
'''
Helper for downloading images with the help of queues
'''
N_ATTEMPS = 5
__instance = None
finished = pyqtSignal(str)
def __init__(self):
Thread.__init__(self)
QObject.__init__(self)
# Mandatory header if we want servers to accept the request
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
self._close = False
self._request_stack = []
@staticmethod
def instance():
if Downloader.__instance==None:
Downloader.__instance = Downloader()
return Downloader.__instance
def request(self, url_in, uri_out, nAttemps=None):
if nAttemps==None:
nAttemps = Downloader.N_ATTEMPS
print('Adding "{}". {} before it'.format(url_in, len(self._request_stack)))
self._request_stack.append((url_in, uri_out, nAttemps))
def run(self):
while not self._close:
if len(self._request_stack)>0:
url_in, uri_out, nAttemps = self._request_stack.pop()
nAttemps -= 1
urllib.request.urlretrieve(url_in, uri_out)#FIXME , timeout=1000)
if os.path.exists(uri_out):
logging.debug('Downloaded "{}". {} still queued'.format(url_in, len(self._request_stack)))
self.finished.emit(uri_out)
elif nAttemps!=0:
logging.info('Failed to download "{}". {} attemps remaining'.format(url_in, nAttemps))
self.request(url_in, uri_out, nAttemps)
sleep(1)
def stop(self):
self._close = True
......@@ -9,10 +9,11 @@ Created on Wed Apr 18 18:34:40 2018
import logging
from enum import Enum
from PyQt5.QtCore import Qt, QCoreApplication
from PyQt5.QtWidgets import QDialog
from PyQt5.QtCore import Qt, QCoreApplication, QObject, pyqtSlot, QEvent
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QApplication
from Babyfut.babyfut import getMainWin
from Babyfut.babyfut import getMainWin, IMG_PATH
from Babyfut.core.database import Database, DatabaseError
from Babyfut.ui.consent_dialog_ui import Ui_Dialog as ConsentDialogUI
......@@ -55,7 +56,7 @@ class ConsentDialog(QDialog):
else:
self.reject()
class Player():
class Player(QObject):
__query_infos = 'SELECT id, fname, lname, pic FROM Players WHERE rfid==?'
__query_time_goals_games = 'SELECT SUM(Matchs.duration) AS timePlayed, SUM(Teams.nGoals) AS goalsScored, COUNT(*) AS gamesPlayed FROM Teams INNER JOIN Matchs ON (Teams.id==Matchs.winningTeam OR Teams.id==Matchs.losingTeam) WHERE (Teams.player1==? OR player2==?)'
__query_victories = 'SELECT COUNT(*) AS victories FROM Players INNER JOIN Teams ON (Players.id==Teams.player1 OR Players.id==Teams.player2) INNER JOIN Matchs ON (Teams.id==Matchs.winningTeam) WHERE Players.id==?'
......@@ -111,11 +112,20 @@ class Player():
if self.pic_path.startswith('http'):
# Download from the internet
container_widget.setStyleSheet('border-image: url({});'.format(Player._placeholder_pic_path))
# TODO DOWNLOAD
self.pic_container.setStyleSheet('border-image: url({});'.format(Player._placeholder_pic_path))
Downloader.instance().request(self.pic_path, os.path.join(IMG_PATH, '{}.jpg'.format(self.id)))
Downloader.instance().finished.connect(self._downloader_callback)
else:
# Already downloaded and stored locally
container_widget.setStyleSheet('border-image: url({});'.format(self.pic_path))
self.pic_container.setStyleSheet('border-image: url({});'.format(self.pic_path))
self._forceWidgetUpdate()
QApplication.processEvents()
@pyqtSlot(str)
def _downloader_callback(self, path):
self.pic_path = path
Downloader.instance().finished.disconnect(self._downloader_callback)
self.displayImg(self.pic_container)
def save(self):
'''
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment