-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
138 lines (117 loc) · 4.63 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import sys
import qrcode
import boto3
import uuid
from PIL import ImageGrab, Image
from botocore.exceptions import NoCredentialsError
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget
from PyQt5.QtGui import QPixmap
from PyQt5.QtCore import Qt, QTimer
from io import BytesIO
import pyperclip
class QRApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("QR Code Generator from Clipboard")
layout = QVBoxLayout()
self.label = QLabel(self)
self.label.setAlignment(Qt.AlignCenter) # Center the text
self.qr_label = QLabel(self)
layout.addWidget(self.label)
layout.addWidget(self.qr_label)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
# Timer to refresh every 1 second
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_qr)
self.timer.start(1000)
def update_qr(self):
text = pyperclip.paste() # Get text from clipboard
if text != self.label.text(): # Only update if text changes
truncated_text = f"{text[:4]}******" # Truncate and add stars
self.label.setText(truncated_text)
qr_img = qrcode.make(text)
buf = BytesIO()
qr_img.save(buf)
buf.seek(0)
pixmap = QPixmap()
pixmap.loadFromData(buf.getvalue())
self.qr_label.setPixmap(pixmap)
self.resize(pixmap.width() + 20, pixmap.height() + 100) # Resize window
# Usage
# upload_file_to_s3('path/to/photo.jpg', 'your-bucket-name')
# function to upload a file to S3 bucket
def upload_file_to_s3(file_name, bucket, object_name=None):
s3_client = boto3.client('s3')
if object_name is None:
object_name = file_name
try:
response = s3_client.upload_file(file_name, bucket, object_name)
print(f"Upload Successful: {object_name}")
except Exception as e:
print(f"Error: {e}")
def upload_test_file(bucket_name, file_path, object_name=None):
# Create a test file
if object_name is None:
object_name = file_path.split('/')[-1]
with open(file_path, 'w') as f:
f.write("This is a test file.")
# Upload to S3
s3_client = boto3.client('s3')
try:
s3_client.upload_file(file_path, bucket_name, object_name)
print(f"Upload Successful: {object_name}")
except Exception as e:
print(f"Error: {e}")
# Usage
upload_test_file('my-qr-air-clipboard', 'test_file.txt')
def generate_signed_url(bucket_name, object_name, expiration=3600):
s3_client = boto3.client('s3')
try:
response = s3_client.generate_presigned_url(
'get_object',
Params={'Bucket': bucket_name, 'Key': object_name},
ExpiresIn=120
)
return response
except NoCredentialsError:
return "Credentials not available"
# Usage
# bucket_name = 'my-qr-air-clipboard'
# object_name = 'test_file.txt'
# signed_url = generate_signed_url(bucket_name, object_name)
# print("Signed URL:", signed_url)
def upload_clipboard_image_to_s3(bucket_name):
try:
# Get image from clipboard
img = ImageGrab.grabclipboard()
if isinstance(img, Image.Image):
# Create a unique file name
object_name = f"images/{uuid.uuid4()}.png"
# Save the image to a BytesIO object
img_byte_array = BytesIO()
img.save(img_byte_array, format="PNG")
img_byte_array.seek(0)
# Upload to S3
s3_client = boto3.client('s3')
s3_client.upload_fileobj(img_byte_array, bucket_name, object_name)
# Generate signed URL
signed_url = s3_client.generate_presigned_url(
'get_object',
Params={'Bucket': bucket_name, 'Key': object_name},
ExpiresIn=3600 # URL valid for 1 hour
)
return signed_url
else:
return "No image found in clipboard"
except Exception as e:
return f"Error: {str(e)}"
bucket_name = 'my-qr-air-clipboard'
signed_url = upload_clipboard_image_to_s3(bucket_name)
print("Signed URL:", signed_url)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QRApp()
window.show()
sys.exit(app.exec_())