File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ from hashlib import md5
2+ from base64 import b64encode
3+ from pathlib import Path
4+ import requests
5+
6+ def get (email ):
7+ try :
8+ return {"status" : "ok" , "url" : get_image (email )}
9+ except Exception as exc :
10+ return {"status" : "error" , "error" : str (exc )}
11+
12+ def get_image (email ):
13+ print (f"get_image({ email } )" )
14+ if "@" not in email :
15+ raise ValueError ("E-Mail-Adresse ist ungültig." )
16+ mail_hash = md5 (email .strip ().encode ()).hexdigest ()
17+ url = "https://www.gravatar.com/avatar/" + mail_hash
18+ # return url
19+ try :
20+ image = requests .get (url ).content
21+ except requests .exceptions .ConnectionError as exc :
22+ image = Path ("fallback.jpg" ).read_bytes ()
23+ # raise
24+ # QML erwartet eine URL - wir haben aber das Bild als Bytes.
25+ # data-URIs lösen das Problem.
26+ return "data:image/jpeg;base64," + b64encode (image ).decode ()
Original file line number Diff line number Diff line change 1+ import QtQuick 2.5
2+ import QtQuick.Window 2.2
3+ import QtQuick.Layouts 1.3
4+ import QtQuick.Controls 1.4
5+ import io.thp.pyotherside 1.4
6+
7+ Window {
8+ id: window
9+ width: 360
10+ height: 360
11+ title: " Gravatar"
12+
13+ ColumnLayout {
14+ spacing: 2
15+ anchors .fill : parent
16+
17+ Image {
18+ id: avatar
19+ source: " "
20+ fillMode: Image .PreserveAspectFit
21+ Layout .fillHeight : true
22+ Layout .fillWidth : true
23+ }
24+ Label {
25+ id: error_label
26+ Layout .fillWidth : true
27+ }
28+ TextField {
29+ id: mailinput
30+ width: parent .width
31+ text: " mail@example.com"
32+ Layout .fillWidth : true
33+ onEditingFinished:
34+ {
35+ py .call (" gravatar.get" , [mailinput .text ], function (result ) {
36+ if (result[" status" ] == " ok" ) {
37+ avatar .source = result[" url" ];
38+ error_label .text = " " ;
39+ } else {
40+ avatar .source = " " ;
41+ error_label .text = result[" error" ];
42+ }
43+ });
44+ }
45+ }
46+ }
47+ Python {
48+ id: py
49+ Component .onCompleted :
50+ {
51+ // Add the directory of this .qml file to the search path
52+ addImportPath (Qt .resolvedUrl (' .' ));
53+ // Import the main module and load the data
54+ importModule (' gravatar' , null );
55+ }
56+ onError:
57+ {
58+ error_label .text = traceback;
59+ }
60+ }
61+ }
You can’t perform that action at this time.
0 commit comments