forked from toulibre/pybadges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qrcodes.py
101 lines (72 loc) · 2.5 KB
/
qrcodes.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib
import requests
import shutil
import os
API_URL = "https://api.qrserver.com/v1/create-qr-code/"
QR_PARAMS = "?color=000000&bgcolor=FFFFFF&qzone=1&margin=0&size=400x400&ecc=L&format=png"
def download_qr_code(description, output_directory="./qrcodes"):
if len(description) == 0:
return
if not os.path.exists(output_directory):
os.makedirs(output_directory)
name = description[0].strip()
if len(description) > 1:
last_name = description[1].strip()
else:
last_name = ''
if len(description) > 2:
company = description[2].strip()
else:
company = ''
if len(description) > 3:
role = description[3].strip()
else:
role = ''
if len(description) > 4:
phone_number = description[4].strip().replace(" ","")
else:
phone_number = ''
if len(description) > 5:
email = description[5].strip()
else:
email = ''
if len(description) > 6:
id = description[6].strip()
id = name + "_" + last_name + "_" +id
else:
id = name + "_" + last_name
vcard_data = generate_vcard_data(name, last_name, company, phone_number, email)
full_url = API_URL + QR_PARAMS + "&data=" + vcard_data
file_path = output_directory + "/qrcode_" + str(id) + ".png"
if os.path.exists(file_path):
print("QrCode already exists for " + str(id) + " (" + name + " " + last_name + ").")
return False
r = requests.get(full_url, stream=True)
if r.status_code == 200:
print("Downloading qr_code for " + name + " " + last_name)
with open(file_path, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
return True
# BEGIN:VCARD
# VERSION:2.1
# FN:Jean Dupont
# N:Dupont;Jean
# ADR;WORK;PREF;QUOTED-PRINTABLE:;Bruxelles 1200=Belgique;6A Rue Th. Decuyper
# LABEL;QUOTED-PRINTABLE;WORK;PREF:Rue Th. Decuyper 6A=Bruxelles 1200=Belgique
# TEL;CELL:+1234 56789
# EMAIL;INTERNET:[email protected]
# UID:
# END:VCARD
def generate_vcard_data(first_name, last_name, company, phone_number, email):
data = "BEGIN:VCARD\nVERSION:A2.1"
data += "\nFN:" + first_name +' '+last_name
data += "\nN:" + last_name + ";" + first_name
data += "\nTEL;CELL:" + phone_number
data += "\nEMAIL;WORK;INTERNET:" + email
data += "\nORG:" + company
data += "\nEND:VCARD\n"
url_encoded_data = urllib.quote(data)
return url_encoded_data