Last active
August 29, 2015 14:12
-
-
Save stopfaner/1442b7f06cd3ea31eef7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cups | |
import time | |
import json,httplib | |
import random | |
import qrcode | |
from xhtml2pdf import pisa | |
import mysql.connector | |
import datetime | |
#Data to get access to database in hosting | |
host_name = '146.120.89.132' | |
db_name = 'farbu68_temp' | |
usr_name = 'farbu68_usr' | |
usr_password = '10101010' | |
table_name = "Easy2PayUsers" | |
#Data about checking Parse database and frequency | |
notes = 0 | |
array = {} | |
#Function that checks if table exists in DB in hosting | |
def checkTableExists(db, tablename): | |
cursor = db.cursor() | |
cursor.execute(""" | |
SELECT COUNT(*) | |
FROM information_schema.tables | |
WHERE table_name = 'Easy2PayUsers' | |
""".format(tablename.replace('\'', '\'\''))) | |
if cursor.fetchone()[0] == 1: | |
cursor.close() | |
return True | |
cursor.close() | |
return False | |
#Function creates new table to work with | |
def createTable(db): | |
cursor = db.cursor() | |
sql = """CREATE TABLE Easy2PayUsers ( | |
USER_LOGIN CHAR(20) NOT NULL, | |
USER_PASSWORD CHAR(20), | |
MONEY INT, | |
EMAIL CHAR(20), | |
DATE_OF_PAY CHAR(20) )""" | |
cursor.execute(sql) | |
#Function provides inserting data into table of database in hosting | |
def insertToDatabase(db, login, password, | |
money, email, date): | |
cursor = db.cursor() | |
sql = "INSERT INTO Easy2PayUsers(USER_LOGIN, \ | |
USER_PASSWORD, MONEY, EMAIL, DATE_OF_PAY) \ | |
VALUES ('%s', '%s', '%d', '%s', '%s' )" % \ | |
(login, password, money, email, date) | |
try: | |
cursor.execute(sql) | |
db.commit() | |
except: | |
db.rollback() | |
#Function that gets all ParseObjects from DB in Parse.com and returns count of objects | |
def queries(): | |
connection = httplib.HTTPSConnection('api.parse.com', 443) | |
connection.connect() | |
connection.request('GET', '/1/classes/TestPay', '', { | |
"X-Parse-Application-Id": "GuhHHtOKF2Sy4CEJ7sP7HZnZiW28DgDnPs4C3r00", | |
"X-Parse-REST-API-Key": "wboJ1C6gcqH2DYnUQmfw4PU3X7zoS7bgyfvBQACZ" | |
}) | |
result = json.loads(connection.getresponse().read()) | |
global array | |
array = result | |
return len(result['results']) | |
#Function generates random number to insert it in check number field | |
def checkNumber(): | |
number = "" | |
for i in range(10): | |
number += str(random.randint(0,9)) | |
return number | |
#Entrance point of script | |
def main(): | |
#Get connection with database in hosting | |
db = mysql.connector.connect(user = usr_name, | |
password = usr_password, | |
host = host_name, | |
database = db_name) | |
#Infinity cycle to monitor notes | |
while True: | |
global notes | |
#Get start count of notes | |
notes = queries() | |
#Print current count of notes | |
print queries() | |
if queries() > notes: | |
notes+=1 | |
#Set pdf file path | |
filename = "/home/stopfan/Developing/python/print.pdf" | |
#Get current time of operation | |
cur_time = datetime.datetime.now().strftime("%I:%M%p on %B %d, %Y") | |
#Generate html page later to convert it in pdf | |
xhtml = "<h1>Easy2Pay</h1>\n" | |
xhtml += "<h2>Check number: "+checkNumber()+"</h2>\n" | |
xhtml += "<h2>Date: "+cur_time+"</h2>" | |
xhtml += "<h2>Login: "+array['results'][notes-1]['Login']+"</h2>" | |
xhtml += "<h2>Password: "+array['results'][notes-1]['Password']+"</h2>" | |
xhtml += "<h2>Email: "+array['results'][notes-1]['Email']+"</h2>" | |
xhtml += "<h2>Count: "+str(array['results'][notes-1]['Pay'])+"</h2>" | |
xhtml += "<h2>http://www.easy2pay.com.ua</h2>" | |
#Check if table exists and fill table with our values | |
if checkTableExists(db, table_name): | |
insertToDatabase(db, array['results'][notes-1]['Login'], | |
array['results'][notes-1]['Password'], | |
array['results'][notes-1]['Pay'], | |
array['results'][notes-1]['Email'], | |
cur_time) | |
else: | |
createTable(db) | |
insertToDatabase(db, array['results'][notes-1]['Login'], | |
array['results'][notes-1]['Password'], | |
array['results'][notes-1]['Pay'], | |
array['results'][notes-1]['Email'], | |
cur_time) | |
#Generate qr code with such parametrs | |
qr = qrcode.QRCode(version=1, | |
error_correction=qrcode.constants.ERROR_CORRECT_L, | |
box_size=5, | |
border=4, | |
) | |
qr.add_data('http://www.easy2pay.cou.ua') | |
qr.make(fit=True) | |
img = qr.make_image() | |
img.save("/home/stopfan/Developing/python/qr.jpg") | |
xhtml += "<p><img src='/home/stopfan/Developing/python/qr.jpg'/></p>" | |
#Convert html to pdf | |
pdf = pisa.CreatePDF(xhtml, file(filename, "w")) | |
#If no error - print it | |
if not pdf.err: | |
pdf.dest.close() | |
conn = cups.Connection() | |
printers = conn.getPrinters() | |
for printer in printers: | |
print printer, printers[printer]["device-uri"] | |
printer_name = printers.keys()[0] | |
conn.printFile(printer_name, filename, "Python_print",{}) | |
else: | |
print "Unable to create file" | |
#Close database connection | |
db.close() | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment