def post(self, category): if not self.has_permission: return if self.request.files == {} or 'myfile' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择文件"}) return file_type_list = [] if category == 'music': file_type_list = ['audio/mpeg', 'audio/x-wav', 'audio/mp3'] if file_type_list == []: return send_file = self.request.files['myfile'][0] if send_file['content_type'] not in file_type_list: if category == 'music': self.write({ "status": "error", "message": "对不起,仅支持 mp3, wav 格式的音乐文件" }) return if category == 'music': if len(send_file['body']) > 20 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传20M以下的音乐文件"}) return user = self.current_user if category == 'music': upload_path = sys.path[0] + "/static/upload/music/" + get_year() + '/' +\ get_month() + "/" if not os.path.exists(upload_path): try: os.system('mkdir -p %s' % upload_path) except: pass timestamp = str(int(time.time())) +\ ('').join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba', 6)) + '_' + str(user.id) image_format = send_file['filename'].split('.').pop().lower() file_path = upload_path + timestamp + '.' + image_format with open(file_path, 'wb') as f: f.write(send_file['body']) path = '/' +\ '/'.join(file_path.split('/')[file_path.split('/').index("static"):]) if category == 'music': if self.is_ajax: return self.write({ 'path': path, 'status': "success", 'message': '上传成功', 'category': 'music', 'content_type': send_file['content_type'] }) return
def post(self, category): if not self.has_permission: return if self.request.files == {} or 'myfile' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择文件"}) return file_type_list = [] if category == 'music': file_type_list = ['audio/mpeg', 'audio/x-wav', 'audio/mp3'] if file_type_list == []: return send_file = self.request.files['myfile'][0] if send_file['content_type'] not in file_type_list: if category == 'music': self.write({"status": "error", "message": "对不起,仅支持 mp3, wav 格式的音乐文件"}) return if category == 'music': if len(send_file['body']) > 20 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传20M以下的音乐文件"}) return user = self.current_user if category == 'music': upload_path = sys.path[0] + "/static/upload/music/" + get_year() + '/' +\ get_month() + "/" if not os.path.exists(upload_path): try: os.system('mkdir -p %s' % upload_path) except: pass timestamp = str(int(time.time())) +\ ('').join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba', 6)) + '_' + str(user.id) image_format = send_file['filename'].split('.').pop().lower() file_path = upload_path + timestamp + '.' + image_format with open(file_path, 'wb') as f: f.write(send_file['body']) path = '/' +\ '/'.join(file_path.split('/')[file_path.split('/').index("static"):]) if category == 'music': if self.is_ajax: return self.write({ 'path': path, 'status': "success", 'message': '上传成功', 'category': 'music', 'content_type': send_file['content_type'], }) return
def requests_scrape(date): # Set up variables for query form_month = date[0] form_year = date[1] url = 'https://www.dmr.nd.gov/oilgas/feeservices/stateprod.asp' req_data = {'VTI-GROUP': '0', 'SELECTMONTH': str(form_month), 'SELECTYEAR': str(form_year), 'B1': 'Get State Volumes'} tries = 0 date_text = "{}-{}".format(get_month(date[0]), date[1]) print("Beginning scrape for month of {}".format(date_text)) while True: try: r = requests.post(url, data=req_data, auth=(username, password)) # Use Beautiful Soup to get the HTML table soup = BeautifulSoup(r.text, 'html.parser') table = soup.find(lambda tag: tag.name=='table' and tag.has_attr('id') and tag['id']=='largeTableOutput') if table: t_headers = list() for th in table.find_all("th"): # remove any newlines and extra spaces from left and right t_headers.append(th.text.replace('\n', ' ').strip()) t_rows = table.find_all(lambda tag: tag.name=='tr') else: print("No data for month of {}".format(date_text)) return # Parse the table rows df = parse_table(t_headers, t_rows, form_month, form_year) print("{} records found for {}".format(len(df), date_text)) return df except requests.exceptions.Timeout: tries += 1 # Try the scrape 5 times before failing if tries < 5: time.sleep(5) continue break
class ImgUploadHandler(BaseHandler): @db_session @tornado.web.authenticated def post(self): if not self.has_permission: return user = self.current_user if not user: return self.redirect_next_url() if self.request.files == {} or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png' ] send_file = self.request.files['myimage'][0] if send_file['content_type'] not in image_type_list: self.write({ "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError, error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width = image_one.size[0] height = image_one.size[1] if width < 80 or height < 80 or width > 30000 or height > 30000: tmp_file.close() self.write({ "status": "error", "message": "对不起,请上传长宽在80px~30000px之间的图片!" }) return user = self.current_user upload_path = sys.path[0] + "/static/upload/" + get_year() + '/' +\ get_month() + "/" if not os.path.exists(upload_path): try: os.system('mkdir -p %s' % upload_path) except: pass timestamp = str(int(time.time())) +\ ('').join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba', 6)) + '_' + str(user.id) image_format = send_file['filename'].split('.').pop().lower() tmp_name = upload_path + timestamp + '.' + image_format image_one.save(tmp_name) tmp_file.close() path = '/' +\ '/'.join(tmp_name.split('/')[tmp_name.split('/').index("static"):]) category = self.get_argument('category', None) print category del_path = None if category == 'head': del_path = user.head_img user.head_img = path result = { 'path': path, 'status': "success", 'message': '头部背景设置成功', 'category': 'head' } elif category == 'background': del_path = user.background_img user.background_img = path result = { 'path': path, 'status': "success", 'message': '背景设置成功', 'category': 'background' } else: result = {'path': path, 'status': "success", 'message': '图片上传成功'} if del_path: try: os.system('rm -f %s%s' % (sys.path[0], del_path)) except: pass if self.is_ajax: return self.write(result) return
filehandle.write(unzipped[0]) scrape_dates = build_scrape_dates(start_year) # Scrapes the monthly production data and outputs a .csv file for every month print("Scraping monthly production data with {} instances.".format(instance_count)) p = Pool(instance_count) monthly_dfs = p.map(requests_scrape, scrape_dates) p.terminate() p.join() # Combines all data into one .csv print("Aggregating monthly production data.") all_monthly_prod = pd.concat(monthly_dfs, axis=0) all_monthly_prod.columns = to_sql_friendly(all_monthly_prod.columns) results_name = "Well Production Data for {}-{} to {}-{}".format(get_month(1), start_year, get_month(today.month), today.year) all_monthly_prod.to_csv(path.join(outdir, "Monthly Production Aggregated.csv"), index=False) # Builds a new sqlite3 table for monthly production data print("Building MONTHLY_PRODUCTION SQLite3 table.") sqlite_filename = path.join(outdir, "results.sqlite3") conn = sqlite3.connect(sqlite_filename) all_monthly_prod.to_sql(monthly_prod_tn, conn, if_exists='append', index=False) # Builds a new sqlite3 table from the well index file print("Building WELL_INDEX SQLite3 table.") wi_df = pd.read_csv(BytesIO(unzipped[0])) wi_df.columns = to_sql_friendly(wi_df.columns) wi_df.to_sql(wi_tn, conn, if_exists='append', index=False) conn.close()
class ImgUploadHandler(BaseHandler): @orm.db_session @tornado.web.authenticated @require_admin def post(self, node_id): category = self.get_argument('category', None) node = Node.get(id=node_id) if not node: return self.redirect_next_url() if not self.request.files or 'myimage' not in self.request.files: self.write({"status": "error", "message": "对不起,请选择图片"}) return image_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png' ] icon_type_list = [ 'image/gif', 'image/jpeg', 'image/pjpeg', 'image/png', 'image/bmp', 'image/x-png', 'image/ico .ico', 'image/x-icon' ] send_file = self.request.files['myimage'][0] if category != 'icon' and send_file[ 'content_type'] not in image_type_list: self.write({ "status": "error", "message": "对不起,仅支持 jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if category == 'icon' and send_file[ 'content_type'] not in icon_type_list: self.write({ "status": "error", "message": "对不起,仅支持 ico, jpg, jpeg, bmp, gif, png\ 格式的图片" }) return if len(send_file['body']) > 6 * 1024 * 1024: self.write({"status": "error", "message": "对不起,请上传6M以下的图片"}) return tmp_file = tempfile.NamedTemporaryFile(delete=True) tmp_file.write(send_file['body']) tmp_file.seek(0) try: image_one = Image.open(tmp_file.name) except IOError, error: logging.info(error) logging.info('+' * 30 + '\n') logging.info(self.request.headers) tmp_file.close() self.write({"status": "error", "message": "对不起,此文件不是图片"}) return width = image_one.size[0] height = image_one.size[1] if width < 20 or height < 20 or width > 30000 or height > 30000: tmp_file.close() self.write({ "status": "error", "message": "对不起,请上传长宽在20px~30000px之间的图片!" }) return user = self.current_user upload_path = sys.path[0] + "/static/upload/" + get_year() + '/' +\ get_month() + "/" if not os.path.exists(upload_path): try: os.system('mkdir -p %s' % upload_path) except: pass timestamp = str(int(time.time())) + \ ''.join(random.sample('ZYXWVUTSRQPONMLKJIHGFEDCBAzyxwvutsrqponmlkjihgfedcba', 6)) + '_' + str(user.id) image_format = send_file['filename'].split('.').pop().lower() tmp_name = upload_path + timestamp + '.' + image_format image_one.save(tmp_name) tmp_file.close() path = '/' +\ '/'.join(tmp_name.split('/')[tmp_name.split('/').index("static"):]) del_path = None if category == 'head': del_path = node.head_img node.head_img = path msg = '节点头部背景设置成功' data = {'path': path, 'category': 'head'} elif category == 'icon': image_two = image_one.resize((75, 75), Image.ANTIALIAS) tmp_name2 = upload_path + timestamp + 'x75.' + image_format image_two.save(tmp_name2) try: os.system('rm -f %s%s' % (sys.path[0], path)) except: pass path = '/' +\ '/'.join(tmp_name2.split('/')[tmp_name2.split('/').index("static"):]) del_path = node.icon_img node.icon_img = path msg = '节点图标设置成功' data = {'path': path, 'category': 'icon'} elif category == 'background': del_path = node.background_img node.background_img = path msg = '节点背景设置成功' data = {'path': path, 'category': 'background'} else: msg = '图片上传成功' data = {'path': path} if del_path: try: os.system('rm -f %s%s' % (sys.path[0], del_path)) except: pass return self.send_success_result(msg=msg, data=data)