python3 asynchronous - coroutine version: common High German Methods

 1 import requests
 2 import time
 3 import csv
 4 from math import ceil
 5 import asyncio
 6 from aiohttp import ClientSession, TCPConnector, client_exceptions
 7 from random import choice
 8 
 9 
10 class KEY:
11     def __init__(self):
12         self.keys = list(csv.reader(open(u'高德秘钥.csv', 'r')))
13         self.keys_length = len(self.keys)
14         self.i = 0
15 
16     def get_key(self):
17         return self.keys[self.i][1]
18 
19     def next_key(self):
20         self.i += 1
21         if self.i == self.keys_length:
22             print("秘钥耗尽")
23             self.i = 0
24         return self.get_key()
25 
26     def random_key(self):
27         return choice(self.keys)[1]
28 
29     def check_out(self):
30         lon = 104.12159
31         lat = 30.65447
32         while self.i < self.keys_length:
33             print(self.i)
34             base_url = 'https://restapi.amap.com/v3/assistant/coordinate/convert'
35             _params_ = {'locations': '{},{}'.format(lon, lat),
36                         'coordsys': 'gps', 'output': 'json', 'key': self.keys[self.i][1]}
37 
38             infocode = int(requests.get(base_url, _params_).json()['infocode'])
39             if infocode == 10001:
40                 print(self.keys[self.i])
41 
42             self.i += 1
43 
44     def get_all_key(self):
45         self.keys = list(csv.reader(open(u'高德秘钥.csv', 'r')))
46 
47     def drop(self, key):
48         if key in self.keys:
49             self.keys.remove(key)
50 
51     def get_len(self):
52         return len(self.keys)
  . 1  class Method,:
   2      DEF  the __init__ (Self):
   . 3          self.keys = KEY ()
   . 4          self.k = self.keys.random_key ()
   . 5          self.event_loop = asyncio.get_event_loop ()
   . 6  
  . 7      # access the URL 
  . 8      the async DEF get_response was (Self, base_url_, params_):
   . 9          "" " 
10          : param base_url_: URL
 . 11          : param params_: access the URL parameter dictionary
 12 is          : return: in response to the content access URL
 13          " "" 
14         async with ClientSession(connector=TCPConnector(verify_ssl=False)) as session:
 15             while True:
 16                 try:
 17                     async with session.get(base_url_, params=params_) as r:
 18                         response_ = await r.json()
 19                         infocode = int(response_['infocode'])
 20                         if infocode == 10000:
 21                             return response_
 22                         else:
 23                             print(infocode)
 24                         print( " Switching key, re-query " )
 25                          self.k = self.keys.random_key ()
 26 is                          params_ [ ' Key ' ] = self.k
 27                  the except client_exceptions.ClientConnectorError:
 28                      Print ( ' the wait Moment A ' )
 29                      for I in Range (60, 0, -1 ):
 30                          Print ( " countdown seconds {} " .format (I), the flush = True, End = " \ B " * 10 )
31 is                          the time.sleep (. 1 )
 32                  the except Exception AS E:
 33 is                      Print (E)
 34 is                      for I in Range (60, 0, -1 ):
 35                          Print ( " countdown seconds {} " .format (I), the flush = True , End = " \ B " * 10 )
 36                          the time.sleep (. 1 )
 37 [  
38 is      # GPS coordinates into the coordinates of a high de 
39      DEF Exchange (Self, coordinate):
 40          "" " 
41 is          : param coordinate: coordinates longitude, latitude | longitude ,latitude……
 42         :return: 高德坐标
 43         """
 44         base_url = 'https://restapi.amap.com/v3/assistant/coordinate/convert'
 45         task = []
 46         for c in coordinate:
 47             _params_ = {'locations': c, 'coordsys': 'gps', 'output': 'json', 'key': self.k}
 48             task.append (self.get_response (the base_url, _params_))
 49  
50          Response = self.event_loop.run_until_complete (asyncio.gather (* Task))
 51 is          Result = []
 52 is          for R & lt in Response:
 53 is              result.append (R & lt [ ' locations ' ])
 54 is          return Result
 55  
56 is      # obtain administrative information 
57 is      DEF get_region (Self, coordinate_array, BATCH = False):
 58          "" " 
59          : param coordinate_array: [coordinate array" longitude, latitude ", ......]
 60         : param batch: batch query
 61          : return: provincial, municipal, district (county), Address
 62          "" " 
63  
64-          # coordinate converter 
65          Coordinate = []
 66          for c in coordinate_array:
 67              Coordinate + = [c]
 68          Coordinate = self.exchange (Coordinate)
 69  
70          Task = []
 71 is          for C in Coordinate:
 72              the base_url = ' https://restapi.amap.com/v3/geocode/regeo ' 
73 is             _params_ = {'location': c, 'extensions': 'all',
 74                         'output': 'json', 'key': self.k, 'batch': str(batch).lower()}
 75             task.append(self.get_response(base_url, _params_))
 76         response_array = self.event_loop.run_until_complete(asyncio.gather(*task))
 77 
 78         for response in response_array:
 79             if not batch:
 80                 province = response['regeocode']['addressComponent']['province']
 81                 city = response['regeocode']['addressComponent']['city']
 82                 district = response['regeocode']['addressComponent']['district']
 83                 address = response['regeocode']['formatted_address']
 84                 pois = [[r['type'].split(';')[0], float(r['distance'])] for r in response['regeocode']['pois'][:3]]
 85 
 86                 yield province, city, district, address, pois
 87             else:
 88                 regeocodes = response['regeocodes']
 89                 result = []
 90                 for r in regeocodes:
 91                     r['pois'] = sorted(r['pois'], key=lambda x: x['distance'])
 92                     result.append(
 93                         (
 94                             r['addressComponent']['province'],
 95                             r['addressComponent']['city'],
 96                             r['addressComponent']['district'],
 97                             r['formatted_address'],
 98                             [[i['type'].split(';')[0], float(i['distance'])] for i in R & lt [ ' pois ' ] [:. 3 ]]
 99                          )
 100                      )
 101                  the yield Result
 102  
103      # acquired POI information (polygons Search - a search rectangle) 
104      DEF get_poi (Self, coordinate_array, lon_gap, lat_gap, poi_type, output_poiname = False ):
 105          "" " 
106          : param Coordinate: [[latitude, longitude], [latitude, longitude]] center of the rectangle Coordinates
 107          : param lon_gap: rectangular longitude interval
 108          : param lat_gap: rectangular latitude interval in
 109          : param poi_type: query POI type
 110          : param output_poiname: output POI name
111         :return: 相应poi数量
112         """
113         # 坐标转换
114         coordinate = []
115         for c in coordinate_array:
116             coordinate += [c[0]-lon_gap/2, c[1]+lat_gap/2, c[0]+lon_gap/2, c[1]-lat_gap/2]
117         coordinate = self.exchange(coordinate)
118 
119         base_url = 'https://restapi.amap.com/v3/place/polygon'
120 
121         task = []
122         for i in range(0, len(coordinate), 4):
123             params = {'polygon': '{},{}|{},{}'.format(coordinate[i], coordinate[i+1], coordinate[i+2], coordinate[i+3]),
124                       'types': poi_type, 'output': 'json', 'key': self.keys.random_key(), 'page': 1, 'offset': 20}
125 
126             task.append(self.get_response(base_url, params))
127         response = self.event_loop.run_until_complete(asyncio.gather(*task))
128 
129         for r in response:
130             n = int(r['count'])
131             task = []
132             if output_poiname:
133                 print('Crawling POI {} name'.format(poi_type))
134                 name = ['{}({})'.format(e['name'], e['typecode']) for e in r['pois']]
135                 while params['page'] < ceil(n/20):
136                     params['page'] += 1
137                     task.append(self.get_response(base_url, params))
138                 result = self.event_loop.run_until_complete(asyncio.gather(*task))
139                 for res in result:
140                     name += ['{}({})'.format(e['name'], e['typecode']) for e in res['pois']]
141 
142                 yield n, name
143             else:
144                 yield n
145 
146     def get_distance(self, coordinate_array):
147         """
148         :param coordinate_array: [o_lon, o_lat, d_lon, d_lat]
149         :return:
150         """
151         # 坐标转换
152         coordinate = []
153         for c in coordinate_array:
154             coordinate += c
155         coordinate = self.exchange(coordinate)
156 
157         base_url = 'https://restapi.amap.com/v3/direction/driving'
158         task = []
159 
160         for i in range(0, len(coordinate), 2):
161             parameters = {
162                 'key': self.keys.random_key(), 'strategy': 2, 'nosteps': 1, 'extensions': 'base',
163                 'origin': coordinate[i],
164                 'destination': coordinate[i+1]
165             }
166             task.append(self.get_response(base_url, parameters))
167         response = self.event_loop.run_until_complete(asyncio.gather(*task))
168 
169         for r in response:
170             yield float(r['route']['paths'][0]['distance'])

 

Guess you like

Origin www.cnblogs.com/jpapplication/p/12597642.html