def find_emails(website): keyword = 'mail ' + website url = GOOGLE_SEARCH + quote(keyword) resp = network.get(url) if resp.status_code != 200: logger.error('Response from "%s" error, code %s', url, resp.status_code) logger.debug('Response: %s', resp.text) raise IOError('Failed to search mail from ' + url) response = Selector(text=resp.text) target_url = response.xpath('//div[@class="g"]/h3/a/@href').extract_first() if target_url.startswith('/url'): m = GOOGLE_RESULT_PATTERN.search(target_url) if not m: raise ValueError('Failed to parse google search result %s', target_url) target_url = m.group(1) target_response = network.get(target_url) if target_response.status_code != 200: logger.warning('Target site "%s" may be down, code %s', target_url, target_response.status_code) logger.debug('Target site response\n %s', target_response.text) matches = EMAIL_PATTERN.findall(target_response.text) return [m[0] for m in matches]
def setup_preferences(cls): current_summoner = network.get('/lol-summoner/v1/current-summoner') user_name = current_summoner['displayName'] highlights_folder = network.get( '/lol-highlights/v1/highlights-folder-path') preferences = cls.get_preferences() preferences['first_time'] = False preferences['user_name'] = user_name preferences['current-highlights-folder'] = highlights_folder preferences['lol-highlights-folder'].append(highlights_folder) region = network.get( '/lol-platform-config/v1/namespaces/LoginDataPacket/platformId') region = 'na1' if region == 'NA' else region.lower() preferences['region'] = region normalised_region = network.get( '/lol-platform-config/v1/namespaces/LoginDataPacket/' 'competitiveRegion') preferences['normalised_region'] = normalised_region.lower() cls.save_preferences(preferences, to_file=True)
def _request(self, url): """Request a URL to read the version from.""" reply = self._reply = network.get(url) if reply.isFinished(): self._handleReply() else: reply.finished.connect(self._handleReply)
class ArchiveOrg: home = 'https://archive.org/' name = 'Internet Archive' def decode_results(self, j, curr_url): pass def search(self, keyword, page=1, options=None): url = 'https://archive.org/advancedsearch.php' params = { 'q': keyword.replace(' TO ', ' to '), 'rows': '50', 'page': page, 'output': 'json', 'callback': '', 'save': 'yes', 'fl[]': [ 'creator', 'description', 'identifier', 'licenseurl', 'source', 'subject', 'title' ], 'sort[]': ['downloads desc', 'stars desc', 'avg_rating desc'], } req = get(url, params=params) try: return self.decode_results(req.json(), url) except (KeyError, AttributeError, TypeError) as e: raise ValueError('Internet Archive returned unexpected results')
def get_access_token(): APP_ID = 'wx01ffcc360fd234d4' APP_SECRET = '18cf885a8064b19b2297444902cd723c' ACCESS_TOKEN_REQUEST_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % ( APP_ID, APP_SECRET) r = nt.get(ACCESS_TOKEN_REQUEST_URL) try: return r.json()['access_token'] except: return None
def _upload_cdn(url): raw_image_resp = network.get(url) if raw_image_resp.status_code != 200: raise IOError('Failed to download image ' + url) ext = _file_ext(url) cdn_image_resp = requests.post( UPLOAD_API, files={'file': ('image.' + ext, raw_image_resp.content)}) ret = cdn_image_resp.json() if cdn_image_resp.status_code != 200: logger.error('Failed to upload cdn ' + str(ret)) return None return ret['url']
def resolve_version(dependency, version, date): time = ntw.get(dependency)['time'] versions = get_times(time, date) # resolve the range nvrp = NodeVersionRangeParser() svr = nvrp.parse(version) # get the best satisfies range new_version = get_version(dependency, versions, svr) # if one version satisfies if new_version: return str(new_version) else: return version
def run(self): self.running = True while self.running: login_status = network.get('/lol-summoner/v1/current-summoner') if login_status.get('httpStatus') is not None: self.login_status.emit( 'Not logged in. Please Log in to the client') time.sleep(2) continue else: self.login_status.emit('Logged in.') if self.is_first_time: DataStore.setup() self.done_setup.emit() self.running = False
def get_media(media_id): '''根据media_id 从微信服务器下载媒体资源''' MEIDA_REQUEST_URL = "https://file.api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s" url = MEIDA_REQUEST_URL % (get_access_token(), media_id) # print(nt.get(url).content) return nt.get(url).content
import network import objects import settings import parser socket = network.get(settings.MAX_CUBE_IP, settings.MAX_CUBE_PORT) message = network.read(socket) data = parser.parse(message) header = data['H'] meta = data['M'] devices = data['L'] cube = objects.Cube( address=header['address'], serial=header['serial'], firmware_version=header['firmware_version'] ) for room in meta['rooms'].values(): cube.add_room(objects.Room(**room)) for device in meta['devices']: device_class = objects.Device.get_device_type(device['type']) device = device_class(**device) cube.add_device(device)
def connect_station(self, network): """ :param network: """ network_name = network['ssid'] log.info('WiFi STA: Preparing connection to network "{}"'.format( network_name)) auth_mode = None if platform_info.vendor == platform_info.MICROPYTHON.Pycom: log.info('WiFi STA: Getting auth mode') auth_mode = self.get_auth_mode(network_name) log.info('WiFi STA: Auth mode is "{}"'.format(auth_mode)) password = network['password'] # TODO: Optionally, configure hostname. # https://docs.micropython.org/en/latest/library/network.WLAN.html # https://github.com/pycom/pycom-micropython-sigfox/pull/165 # https://forum.pycom.io/topic/3326/new-firmware-release-v1-18-0 if 'dhcp_hostname' in network: if hasattr(self.station, 'config'): log.ingo('WiFi STA: Using dhcp_hostname "{}"'.format( network['dhcp_hostname'])) self.station.config(dhcp_hostname=network['dhcp_hostname']) else: log.error('Could not set hostname on older MicroPython') # Optionally, configure static IP address. if 'ifconfig' in network: log.info( 'WiFi STA: Using static network configuration "{}"'.format( network_name)) self.station.ifconfig(config=network['ifconfig']) # Obtain timeout value. # TODO: Make default timeout configurable. network_timeout = network.get('timeout', 15.0) # Connect to WiFi station. log.info('WiFi STA: Starting connection to "{}" ' 'with timeout of {} seconds'.format(network_name, network_timeout)) self._connect(network_name, password, auth_mode=auth_mode, timeout=network_timeout) # After reset, WiFi regularly does not connect. # So, let's just try again working around this weird obstacle. self.wait_for_connection(2) if not self.is_connected(): self._connect(network_name, password, auth_mode=auth_mode, timeout=network_timeout) # Wait for network to arrive. self.wait_for_connection(network_timeout) # Finally, check for WiFi connection. if not self.is_connected(): raise WiFiException( 'WiFi STA: Unable to connect to "{}"'.format(network_name)) # Inform about networking status. self.print_short_status() self.print_address_status() return True
def connect_station(self, network): network_name = network['ssid'] log.info('WiFi STA: Preparing connection to network "{}"'.format( network_name)) auth_mode = None if self.platform_info.vendor == MicroPythonPlatform.Pycom: log.info('WiFi STA: Getting auth mode') auth_mode = self.get_auth_mode(network_name) log.info('WiFi STA: Auth mode is "{}"'.format(auth_mode)) password = network['password'] # TODO: Optionally, configure hostname. # https://docs.micropython.org/en/latest/library/network.WLAN.html # https://github.com/pycom/pycom-micropython-sigfox/pull/165 # https://forum.pycom.io/topic/3326/new-firmware-release-v1-18-0 if 'dhcp_hostname' in network: if hasattr(self.station, 'config'): log.ingo('WiFi STA: Using dhcp_hostname "{}"'.format( network['dhcp_hostname'])) self.station.config(dhcp_hostname=network['dhcp_hostname']) else: log.error('Could not set hostname on older MicroPython') # Optionally, configure static IP address. if 'ifconfig' in network: log.info( 'WiFi STA: Using static network configuration "{}"'.format( network_name)) self.station.ifconfig(config=network['ifconfig']) # Obtain timeout value. network_timeout = network.get('timeout', 15.0) # Connect to WiFi station. if self.platform_info.vendor == MicroPythonPlatform.Pycom: log.info( 'WiFi STA: Starting connection to "{}" with timeout of {} seconds' .format(network_name, network_timeout)) self.station.connect(network_name, (auth_mode, password), timeout=int(network_timeout * 1000)) else: log.info( 'WiFi STA: Starting connection to "{}"'.format(network_name)) self.station.connect(network_name, password) # Wait for network to arrive. self.wait_for_connection(network_timeout) if not self.is_connected(): raise WiFiException( 'WiFi STA: Unable to connect to "{}"'.format(network_name)) # Inform about networking status. self.print_short_status() self.print_address_status() return True
import network import objects import settings import parser socket = network.get(settings.MAX_CUBE_IP, settings.MAX_CUBE_PORT) message = network.read(socket) data = parser.parse(message) header = data['H'] meta = data['M'] devices = data['L'] cube = objects.Cube(address=header['address'], serial=header['serial'], firmware_version=header['firmware_version']) for room in meta['rooms'].values(): cube.add_room(objects.Room(**room)) for device in meta['devices']: device_class = objects.Device.get_device_type(device['type']) device = device_class(**device) cube.add_device(device) if device.address in devices: device.__dict__.update(devices[device.address])
u.pos = (u.pos[0]+v[0],u.pos[1] + v[1]) d = pygame.display.set_mode((400,400)) units = [] me = int(sys.argv[3]) for x in range(6): u = Unit() u.pos = (x*20.,x*20.) u.dest = u.pos u.owner = x%2 units.append(u) raw_input() while True: orders = get_my_orders(units) network.send(int(sys.argv[2]),repr(orders)) i = eval(network.get()) all_orders = [] all_orders.extend(orders) all_orders.extend(i) # magic logic(units, all_orders) d.fill((0,0,0)) for u in units: f = 1 if u in selection: f = 0 pygame.draw.circle(d, (255*(u.owner!=me),255*(u.owner==me),0), u.pos, 16 if u.type==1 else 4,f) pygame.display.update() #print "cycle" #pygame.time.wait(10)
d = pygame.display.set_mode((400, 400)) units = [] me = int(sys.argv[3]) for x in range(6): u = Unit() u.pos = (x * 20., x * 20.) u.dest = u.pos u.owner = x % 2 units.append(u) raw_input() while True: orders = get_my_orders(units) network.send(int(sys.argv[2]), repr(orders)) i = eval(network.get()) all_orders = [] all_orders.extend(orders) all_orders.extend(i) # magic logic(units, all_orders) d.fill((0, 0, 0)) for u in units: f = 1 if u in selection: f = 0 pygame.draw.circle(d, (255 * (u.owner != me), 255 * (u.owner == me), 0), u.pos, 16 if u.type == 1 else 4, f) pygame.display.update() #print "cycle"