Skip to content

Instantly share code, notes, and snippets.

@giwa
Last active June 3, 2016 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save giwa/211f65c08432c10df256fe2887f4e1b1 to your computer and use it in GitHub Desktop.
Save giwa/211f65c08432c10df256fe2887f4e1b1 to your computer and use it in GitHub Desktop.
import json
import time
from itertools import islice
import requests
door_endpoint = 'http://localhost/api/v1/word_rank'
lcd_endpoint = 'http://192.168.1.2:5000/api/lcd'
default_data = [{
'background': "black",
'text': "Interop 2016",
'color': "white",
'showImage': "miku"
},
{
'background': "black",
'text': "慶應義塾大学西研究室",
'color': "blue",
'showImage': ""
},
{
'background': "black",
'text': "Service-oriented Router",
'color': "red",
'showImage': ""
}
]
lcd_temp = dict(background='', text = '', color='', showImage='')
bg_colors = ['white', 'black', 'black']
txt_colors = ['balck', 'white', 'red']
def get_door_word_rank():
# ignore proxy
session = requests.Session()
session.trust_env = False
try:
r = session.get(door_endpoint)
if r.status_code < 400:
return json.loads(r.text)
else:
return None
except Exception as e:
return None
def door2lcd(door_data):
r = []
for idx, word in enumerate(islice(door_data, 3)):
d = lcd_temp.copy()
d['background'] = bg_colors[idx]
d['color'] = colors[idx]
d['text'] = word.get('name', '')
r.append(d)
return r
def main():
while True:
lcd_data = []
data = get_door_word_rank()
if data is None:
lcd_data = defalt_data
else:
lcd_data = door2lcd(data)
session = requests.Session()
session.trust_env = False
session.post(lcd_endpoint, json=lcd_data)
time.sleep(60 * 5)
if __name__ == '__main__':
main()
# -*- coding: utf-8 -*-
import requests,urllib2,sys,time
import json
except_data=[{
'background': "black",
'text': "Interop 2016",
'color': "white",
'showImage': "miku"
},
{
'background': "black",
'text': "慶應義塾大学西研究室",
'color': "blue",
'showImage': ""
},
{
'background': "black",
'text': "Service-oriented Router",
'color': "red",
'showImage': ""
}
]
while True:
opened_url = True
try:
url = "http://localhost:80/api/v1/word_rank"
openurl = urllib2.urlopen(url)
raw_data = openurl.read()
openurl.close()
opened_url = True
except:
raw_data = json.dumps(except_data)
opened_url = False
pass
print raw_data
decoded_data = json.loads(raw_data)
# print raw_data
# print decoded_data
i = 0
send_data = [{},{},{}]
for data in decoded_data:
# print data
if opened_url:
if i == 0:
send_data[0].update({
'background': 'white',
'text': u'1位の単語は:' + str(data.get('name','')),
'color': 'black',
'showImage': ''
})
if i == 1:
send_data[1].update({
'background': 'black',
'text': u'2位の単語は:' + str(data.get('name','')),
'color': 'white',
'showImage': ''
})
if i == 2:
send_data[2].update({
'background': 'black',
'text': u'3位の単語は:' + str(data.get('name','')),
'color': 'red',
'showImage': ''
})
if i > 2:
break
else:
if i == 0:
send_data[0].update({
'background': 'white',
'text': u''.join(data.get('name','')),
'color': 'black',
'showImage': ''
})
if i == 1:
send_data[1].update({
'background': 'black',
'text': u''.join(data.get('name','')),
'color': 'white',
'showImage': ''
})
if i == 2:
send_data[2].update({
'background': 'black',
'text': u''.join(data.get('name','')),
'color': 'red',
'showImage': ''
})
if i > 2:
break
i = i + 1
# print send_data
session = requests.Session()
session.trust_env = False
r = session.post('http://192.168.1.2:5000/api/lcd',json = send_data)
print r
print r.text
time.sleep(18000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment