<s> import json <EOL> import urllib2 <EOL> import string <EOL> from settings import PROTOCOL , HOSTNAME <EOL> from http_server import stop <EOL> def refresh_devices ( ) : <EOL> stop ( ) <EOL> tokenFile = open ( "<STR_LIT>" ) <EOL> token = tokenFile . read ( ) <EOL> tokenFile . close ( ) <EOL> devicesFile = open ( "<STR_LIT>" , "<STR_LIT:w>" ) <EOL> with open ( "<STR_LIT>" , '<STR_LIT:r>' ) as fh : <EOL> for endpoint in fh . readlines ( ) : <EOL> if endpoint . __len__ ( ) > <NUM_LIT:1> : <EOL> for deviceType in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> endpoint = endpoint . strip ( ) <EOL> url = "<STR_LIT>" . format ( protocol = PROTOCOL , hostname = HOSTNAME , endpoint = endpoint , deviceType = deviceType ) <EOL> req = urllib2 . Request ( url ) <EOL> req . add_header ( '<STR_LIT>' , "<STR_LIT>" % token ) <EOL> response = urllib2 . urlopen ( req ) <EOL> the_page = response . read ( ) <EOL> jsonData = json . loads ( the_page ) <EOL> for device in jsonData : <EOL> deviceKey = "<STR_LIT>" . format ( endpoint = endpoint , deviceType = deviceType , deviceId = device [ '<STR_LIT:id>' ] ) <EOL> deviceValue = device [ '<STR_LIT:name>' ] if len ( device [ '<STR_LIT:label>' ] ) == <NUM_LIT:0> else device [ '<STR_LIT:label>' ] <EOL> deviceCache = "<STR_LIT>" . format ( key = deviceKey , value = deviceValue ) <EOL> devicesFile . write ( deviceCache ) <EOL> devicesFile . close ( ) <EOL> return "<STR_LIT>" </s>
