def submit_urls_to_baidu(): articles = Article.objects.filter(status=BlogStatus.PUBLISHED).order_by('-id') urls = [DOMAIN + article.get_absolute_url() for article in articles] api = 'http://data.zz.baidu.com/urls?site=yangsihan.com&token=7tNlHmCq6GVPoYfb' response = requests.post(api, data='\n'.join(urls)) SysLogger.info(response.content.decode()) print(response.content.decode())
def submit_urls_to_baidu(): articles = Article.objects.filter(status=BlogStatus.PUBLISHED).order_by('-id') urls = [DOMAIN + article.get_absolute_url() for article in articles] api = 'http://data.zz.baidu.com/urls?site=yangsihan.com&token=7tNlHmCq6GVPoYfb' response = requests.post(api, data='\n'.join(urls)) SysLogger.info(response.content.decode()) print (response.content.decode())
def upload_file(self, filestream=None, dirs='', path='', prefix='', name='', suffix=''): '''上传文件 :params dirs: 分类目录 eg:account :params path: eg:user_avatar :param prefix: 前缀 :params name: 文件名 :pramas suffix: 文件后缀 返回值: oss_object -- oss对象 filename -- 文件路径 ''' if not filestream: return None, None filename = self._gen_filename(filestream.name, dirs=dirs, path=path, prefix=prefix, name=name, suffix=suffix) try: oss_object = self.bucket.put_object(filename, filestream) except Exception, exp: SysLogger.exception(exp) return None, None
def copy_file(self, old_filename, dirs='', path='', prefix='', name='', suffix='', delete_old=True): """ 同一个bucket中复制文件 :param delete_old: 是否删除旧的文件 :return: """ try: filename = self._gen_filename(old_filename, dirs=dirs, path=path, prefix=prefix, name=name, suffix=suffix) self.bucket.copy_object(self.bucket.bucket_name, old_filename, filename) if delete_old: self.bucket.delete_object(old_filename) return filename except Exception, exp: SysLogger.exception(exp) return None
def download_file(self, filename, target_path): """ 从oss下载指定文件到指定目录 :param filename: 文件名 :param target_path: 存储目标目录 :return: 是否成功 """ try: self.bucket.get_object_to_file(filename, target_path) return True except Exception, exp: SysLogger.exception(exp) return False
def get_lon_lat_by_address(address): """ 使用高德API通过地址获取经纬度(lon, lat) """ par = {'address': address, 'key': AMAP_SERVER_KEY} base = 'http://restapi.amap.com/v3/geocode/geo' response = requests.get(base, par) answer = response.json() try: GPS = answer['geocodes'][0]['location'].split(",") return GPS[0], GPS[1] except Exception as exp: SysLogger.exception(exp) return None, None
def batch_delete_files(self, filename_list): """ 同一个bucket中删除多个文件 :param filename_list: 文件短路径列表 :return: """ if not filename_list: return False try: self.bucket.batch_delete_objects(filename_list) return True except Exception, exp: SysLogger.exception(exp) return None
def delete_file(self, filename): """ 同一个bucket中删除单个文件 :param filename: 文件短路径 :return: """ if not filename: return False try: self.bucket.delete_object(filename) return True except Exception, exp: SysLogger.exception(exp) return None
def _printexc(self, ex): SysLogger.exception(ex)