Advertisement
Foxxything

quote.py

Nov 14th, 2021
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. from flask import Flask, jsonify, request
  2. import threading
  3. import pygame
  4. from queue import Queue
  5. import socket
  6.  
  7. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  8. s.connect(("8.8.8.8", 80))
  9.  
  10. q = Queue()
  11.  
  12. q.put('Make Put Reqest To Update Text, ip: ' + s.getsockname()[0])
  13.  
  14. s.close()
  15.  
  16. app = Flask(__name__)
  17. @app.route("/", methods=['GET', 'PUT'])
  18. def home():
  19.  
  20.   if request.method == 'PUT':
  21.     try:
  22.       Text = request.args.get('text')
  23.       print(f"Text was: {Text}")
  24.       data = {"satus": "Screen is updating", "Text": Text}
  25.       q.put(Text)
  26.       return jsonify(data)
  27.     except Exception as e:
  28.       print(e)
  29.       data = {"error": "an error as accored"}
  30.       return jsonify(data)
  31.   else:
  32.     data = {'Text': 'Make put request to update screen'}
  33.     print(request.host)
  34.     return jsonify(data)
  35.  
  36. def blit(surface, text, center):
  37.   blitsurface = font.render("A", False, (0, 0, 0))
  38.   did_blit = False
  39.   median = blitsurface.get_size()
  40.   max_size = [surface.get_width()-50, surface.get_height()]
  41.   allwords = ''
  42.   wordict = []
  43.   y = 0
  44.   for line in text.split('\n'):
  45.     for word in line.split(' '):
  46.       blitsurface = font.render(allwords + word, False, (0, 0, 0))
  47.       word_size = blitsurface.get_size()
  48.       w, h = blitsurface.get_size()
  49.       if word_size[0] > max_size[0]:
  50.         blitsurface = font.render(allwords, False, (0, 0, 0))
  51.         w, h = blitsurface.get_size()
  52.         wordict.append({"word":allwords, "width":w, "y":y})
  53.         allwords = ''
  54.         y+=median[1]
  55.       allwords += word + ' '
  56.     if wordict[-1]["word"] != allwords:
  57.       blitsurface = font.render(allwords, False, (0, 0, 0))
  58.       w, h = blitsurface.get_size()
  59.       wordict.append({"word":allwords, "width":w, "y":y})
  60.     for item in wordict:
  61.       item['y'] -= median[1]*0.5*(len(wordict)-1)
  62.       blitsurface = font.render(item["word"], False, (0, 0, 0))
  63.       surface.blit(blitsurface, (center[0]-(item["width"]/2), center[1]-(median[1]/2)+item['y']))
  64.  
  65. def pyg(q):
  66.   global font
  67.   pygame.init()
  68.   pygame.font.init()
  69.   font = pygame.font.Font('footlight-mt-std-extra-bold.otf', 35)
  70.   window = pygame.display.set_mode((800, 480))
  71.   center = (window.get_width()/2, window.get_height()/2)
  72.   if not q.empty():
  73.     text = q.get()
  74.   while True:
  75.     events = pygame.event.get()
  76.     for event in events:
  77.       if event.type == pygame.QUIT:
  78.         pygame.quit()
  79.         exit()
  80.       if event.type == pygame.KEYDOWN:
  81.         if event.key == pygame.K_ESCAPE:
  82.           pygame.quit()
  83.           exit()
  84.     if not q.empty():
  85.       text = q.get()
  86.     window.fill((200, 200, 200))
  87.     bkg = pygame.image.load("./images/background.png")
  88.     window.blit(bkg, (0, 0))
  89.     blit(window, text, center)
  90.     pygame.display.update()
  91.  
  92. t1 = threading.Thread(target=pyg, args=(q,))
  93. t1.start()
  94. app.run(debug = False, host="0.0.0.0", port=5000)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement