def get_js_files(self): """Получить js файлы""" contents = self.load_file() # -- # JS # <script type="text/javascript" src="scripts/jquery-1.4.3.min.js"></script> # <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> # -- rega_js = re.compile( '<script[^>]+src[\s]*=[\s]*["\']([^>"\']+\.js)[^>]*', re.I + re.U + re.DOTALL) matches_js = rega_js.findall(contents) for item in matches_js: js = self.findpath(self.base_path, item) self.js_files.append(js) js_name = os.path.split(js)[1] js_name = self.clean_path(js_name) contents = contents.replace(item, 'js/%s' % js_name) with open_file(self.index_file, "w+") as f: f.write(contents) # ---------------- # Дергаем JS файлы # ---------------- for js in self.js_files: contents = self.grab_file(js, False) if not contents: continue js_name = os.path.split(js)[1] js_file = os.path.join(self.js_path, js_name) with open_file(js_file, "wb+") as f: f.write(contents)
def analyze_css_file(self, css): """Отдельно пройтись по каждому css файлу :param css: путь к css файлу""" contents_css = '' # Для перезаписи css # Тащим файл contents = self.grab_file(css) if not contents: logger.warning('[ERROR]: bad file %s' % css) return css_name = os.path.split(css)[1] css_name = self.clean_path(css_name) css_file = os.path.join(self.css_path, css_name) if not check_path(css_file): now = str(time.time()).replace('.', '_') css_file = '%s-%s' % (css_file, now) with open_file(css_file, 'w+') as f: f.write(contents) self.stylizecss(css_file) # Парсим css файл css = self.clean_path(css) with open_file(css_file, 'r') as f: lines = f.readlines() for line in lines: line = self.parse_css_line(css, line) contents_css = contents_css + line with open_file(css_file, 'w+') as f: f.write(contents_css)
def get_categories(self): """Получаем список категорий для товаров""" fname = 'vkontakte_getCategories.txt' if not check_path(fname): with open_file(fname, 'r') as f: content = json.loads(f.read()) return content method = 'market.getCategories' params = { 'count': 100, 'offset': 0, } self.required_params(params) categories = [] while True: r = self.s.get('%s%s' % (self.API_URL, method), params=params) content = r.json() items = content['response']['items'] if not items: break categories += items params['offset'] += content['response']['count'] with open_file(fname, 'wb+') as f: f.write(json.dumps(categories).encode('utf-8')) return categories
def stylizecss(self, path): """Облагородить css разметку переносами :param path: путь до файла css""" contents = '' with open_file(path, 'r') as f: lines = f.readlines() for line in lines: if line.count('}') >= 2 and not 'data:' in line: line = line.replace('}', '}\r\n') if line.count(';') >= 2 and not 'data:' in line: line = line.replace(';', ';\r\n') contents = contents + line with open_file(path, 'w+') as f: f.write(contents)
def upload_img(self, is_main, path): """Загрузка изображения на сервер""" if check_path(path): return None upload_img_url = self.get_upload_img_url(is_main) files = {'file': open_file(path, 'rb')} r = self.s.post(upload_img_url, files=files) imga = r.json() if 'error' in imga: logger.error(imga) return None method = 'photos.saveMarketPhoto' params = { 'group_id': self.group_id, 'photo': imga['photo'], 'server': imga['server'], 'hash': imga['hash'], 'crop_data': imga.get('crop_data', ''), 'crop_hash': imga.get('crop_hash', ''), } self.required_params(params) r = self.s.get('%s%s' % (self.API_URL, method), params=params) resp = r.json() if 'error' in resp: logger.error(resp) return None return resp['response'] # Массив
def handle(self, *args, **options): today = datetime.date.today() end_date = date_plus_days(today, days=1) start_date = date_plus_days(end_date, days=-360) # --------------------------- # Задать дату начала парсинга # --------------------------- if options.get('start'): date = str_to_date(options['start']) if date: start_date = date # ------------------------------ # Задать дату окончания парсинга # ------------------------------ if options.get('end'): date = str_to_date(options['end']) if date: end_date = date root_url = "http://jsmnew.doko.link/service_api.php" key = "d296c101daa88a51f6ca8cfc1ac79b50" urla = "{}?key={}&module=yahoo&startDate={}&endDate={}".format(root_url, key, start_date.strftime("%Y-%m-%d"), end_date.strftime("%Y-%m-%d")) fname = "ylots.json" r = requests.post(urla) content = r.json() f = open_file(fname, "w+") f.write(json.dumps(content)) f.close()
def create_structure(self): """Создаем структуру""" make_folder(self.themeforest) self.template_path = '%s/%s' % (self.themeforest, self.template) self.css_path = os.path.join(self.template_path, 'css') self.js_path = os.path.join(self.template_path, 'js') self.img_path = os.path.join(self.template_path, 'img') self.misc_path = os.path.join(self.template_path, 'misc') drop_folder(self.css_path) drop_folder(self.js_path) drop_folder(self.img_path) drop_folder(self.misc_path) make_folder(self.css_path) make_folder(self.js_path) make_folder(self.img_path) make_folder(self.misc_path) # ------------------------------------------ # Записать ссылку на источник, откуда тыбзим # ------------------------------------------ reference = '%s/%s' % (self.template_path, 'reference.txt') with open_file(reference, 'w+') as f: f.write(self.url_template)
def upload_img(self, img): """Загрузка изображения""" new_img = None if self.img: self.drop_img() # --------------------------------------------------- # django.core.files.uploadedfile.InMemoryUploadedFile # --------------------------------------------------- if type(img) == InMemoryUploadedFile: ext = extension(img.name) if ext: media_folder = self.get_folder() if check_path(media_folder): make_folder(media_folder) new_img = "%s%s" % (self.id, ext) path = os.path.join(media_folder, new_img) if catch_file(img, path): imageThumb(path, 1920, 1280) # ------------------------------ # Загрузка изображения по ссылке # ------------------------------ elif type(img) == str: new_img = grab_image_by_url(img, self.get_folder(), str(self.id)) if new_img: path = os.path.join(self.get_folder(), new_img) img = imageThumb(path, 1920, 1280) if not img: self.drop_img() return None else: new_img = None # ---------------------------------------------------- # django.core.files.uploadedfile.TemporaryUploadedFile # ---------------------------------------------------- elif type(img) == TemporaryUploadedFile: path = img.temporary_file_path() tmp_file = open(path, 'rb') img_content = tmp_file.read() tmp_file.close() ext = extension(img.name) new_img = '{}{}'.format(str(self.id), ext) media_folder = self.get_folder() if check_path(media_folder): make_folder(media_folder) dest = os.path.join(media_folder, new_img) img_file = open_file(dest, 'wb+') img_file.write(img_content) img_file.close() img = imageThumb(dest, 1920, 1280) if not img: self.drop_img() return None else: logger.error('Unknown img type %s' % (type(img), )) assert False self.__class__.objects.filter(pk=self.id).update(img=new_img) self.img = new_img return new_img
def handle(self, *args, **options): ticks_data = [] output_img = save_with_plotly(ticks=ticks_data, image='png') with open_file(output_img, 'r') as f: TelegramBot().send_photo(f) if output_img.endswith('.png'): drop_file(output_img)
def create_daemon(daemon_name, exec_script, port, pyenv: str = None, api_url: str = None): """Создаем демона :param daemon_name: название демона, например daemon_1.service :param exec_script: исполняемый файл, например, /home/jocker/sites/astwobytes/apps/ws_a223/wss_server.py :param port: параметр для передачи в испольняемый файл, например порт на котором должен запускаться демон скрипт должен принимать этот параметр, демон к этому не имеет отношения, просто передает :param pyenv: питонячий путь для запуска через python, например, /home/jocker/sites/astwobytes/env/bin/python :param api_url: endpoint, чтобы обращаться за настройками, отправлять обновления :return: -------- В демоне не нужно активировать виртуальное окружение, можно просто запускать исполняемый файл вирт. окружения, там уже содержатся все нужные пути - Проверить это можно через # python -m site сравниваем с # /home/jocker/sites/astwobytes/env/bin/python -m site - покажет базу и пути """ if check_path(DAEMON_FOLDER): make_folder(DAEMON_FOLDER) if not pyenv: pyenv = '%s/env/bin/python' % (settings.BASE_DIR.rstrip('/'), ) #daemon_path = '%s/%s.service' % (SYSTEM_FOLDER, daemon_name) #if os.path.exists(daemon_path): # logger.info('daemon already exists') # os.unlink(daemon_path) daemon_script = """[Unit] Description=%s After=multi-user.target ConditionPathExists=%s [Service] ExecStart=%s %s %s %s Restart=always Type=idle [Install] WantedBy=multi-user.target #Alias=%s # Алиас не должен быть таким же как имя сервсиа """ % (daemon_name, exec_script, pyenv, exec_script, port, api_url, daemon_name) daemon = '%s/%s' % (DAEMON_FOLDER, daemon_name) with open_file(daemon, 'w+') as f: f.write(daemon_script)
def get_misc_files(self): """Получить misc файлы""" contents = self.load_file() # ----------------------- # search images in styles # ----------------------- rega_imaga_style = re.compile('(url["(]+([\.]*[^")]+)[")]+)', re.I + re.U + re.DOTALL) matches_imga_style = rega_imaga_style.findall(contents) # ---- # MISC # ---- rega_imga = re.compile('(<img[^>]+src[\s]*=[\s]*["\']([^>"\']+)[^>])', re.I + re.U + re.DOTALL) matches_imga = rega_imga.findall(contents) + matches_imga_style counter = 0 for item in matches_imga: counter += 1 imga = self.findpath(self.base_path, item[1]) imga_name = os.path.split(imga)[1] cur_imga = item[0].replace(item[1], 'misc/' + imga_name) contents = contents.replace(item[0], cur_imga) imga_contents = self.grab_file(imga, False) if imga_contents: imga_file_path = os.path.join(self.misc_path, imga_name) imga_file_path = self.clean_path(imga_file_path) # ----------------- # ПРОВЕРКА НА ДУБЛИ # ----------------- if not check_path(imga_file_path): if not imga in self.img_repeated: self.z += 1 imga_file_path = imga_file_path + str(self.z) if not imga in self.img_repeated: self.img_repeated.append(imga) try: with open_file(imga_file_path, 'wb+') as f: f.write(imga_contents) except: logger.warning('[ERROR]: image %s' % imga_file_path) with open_file(self.index_file, "w+") as f: f.write(contents)
def open_wb(path, data_only: bool = True): """Загружаем эксельку :param path: путь до эксельки :param data_only: без формул - только значения """ with open_file(path, 'rb') as excel_file: wb = load_workbook(BytesIO(excel_file.read()), data_only=data_only) # Посмотреть листы эксельки: logger.info(wb.sheetnames) return wb
def handle(self, *args, **options): html_path = 'mammoth' html_files = ListDir(html_path) result = [] for html_file in html_files: if not html_file.endswith('.html'): continue cur_html = os.path.join(html_path, html_file) content = '' with open_file(cur_html, 'r') as f: content = f.read() tree = lxml_html.fromstring(content) tables = tree.xpath('//table') for table in tables: ths = table.xpath('.//thead/tr/th/p') if ths: th = ths[0] if '№ п/п' in th.text: result += accumulate_result( table, html_file.replace('.html', '')) dest = full_path('joint_table_from_html.xlsx') book = xlsxwriter.Workbook(dest) sheet = book.add_worksheet('Лист 1') row_number = 0 sheet.write(row_number, 0, 'Файл') sheet.write(row_number, 1, '№ п/п') sheet.write(row_number, 2, '№ стыка') sheet.write(row_number, 3, 'Клеймо') sheet.write(row_number, 4, 'Материал') sheet.write(row_number, 5, 'Диаметр,мм') sheet.write(row_number, 6, 'Толщина,мм') sheet.write(row_number, 7, 'Дата') sheet.write(row_number, 8, 'Тип сварки') sheet.write(row_number, 9, 'Вид соединения') sheet.write(row_number, 10, 'Соединение') for item in result: row_number += 1 size = item.get('size', 'x').split('/')[0] sizes = size.split('x') diameter = sizes[0] side_thickness = sizes[1] sheet.write(row_number, 0, item.get('file')) sheet.write(row_number, 1, item.get('n')) sheet.write(row_number, 2, item.get('number')) sheet.write(row_number, 3, item.get('stigma')) sheet.write(row_number, 4, item.get('material')) sheet.write(row_number, 5, diameter) sheet.write(row_number, 6, side_thickness) sheet.write(row_number, 7, item.get('date')) sheet.write(row_number, 8, item.get('welding_type')) sheet.write(row_number, 9, item.get('conn_view')) sheet.write(row_number, 10, ' - '.join(item.get('elements', []))) book.close()
def load_file(self, path: str = None): """Прочитать файл :param path: путь к файлу, если не указан - индексный""" if not path: path = self.index_file contents = '' with open_file(path, 'r') as f: lines = f.readlines() for line in lines: contents += line return contents
def get_index_html(self): """Получить индексный файл""" self.index_file = os.path.join(self.template_path, 'index.html') if self.local_index: return contents = self.grab_file(self.url_template) with open_file(self.index_file, 'w+') as f: f.write(contents) # --------- # BASE HREF # Например, # <base href="http://kos9.scompiler.ru/oceanic2/" /> # --------- rega_base = re.compile('<base href=["\']([^>"\']+)', re.I + re.U + re.DOTALL) matches_base = rega_base.search(contents) if matches_base: self.base_path = matches_base.group(1) # --------------------------------------------------- # Наипалово для хитрожопых, юзающих https:// # которые повторяют ссылку от корня типа yootheme.com # --------------------------------------------------- if self.base_path.startswith('https://'): self.base_path = 'http://%s/' % self.base_path.replace( 'https://', '').split('/')[0] logger.warning('New base path %s' % (self.base_path, )) # ------------------------ # Стилизуем индексный файл # ------------------------ contents = '' with open_file(self.index_file, 'r') as f: lines = f.readlines() for line in lines: if line.count('<') > 2: line = line.replace('<', '\r\n<') contents += line with open_file(self.index_file, 'w+') as f: f.write(contents)
def load_exmple_from_old_format(self, lot_number: str): """Загрузить файл ylots.json (старый формат) и найти в нем лот - для сопоставления с новым форматом :param lot_number: номер лота """ with open_file('ylots.json', 'r', encoding='utf-8') as f: content = json.loads(f.read()) for item in content: if not item['numar'] == lot_number: continue with open_file('%s_old.json' % lot_number, 'w+', encoding='utf-8') as f: f.write(json.dumps(item)) date = item.get('data') new_lots = self.get_lots(start_date=date, end_date=date) for new_lot in new_lots: if not new_lot['yahoo_id'] == lot_number: continue with open_file('%s_new.json' % lot_number, 'w+', encoding='utf-8') as f: f.write(json.dumps(new_lot)) catched = [] # --------------------------- # Пробуем отобразить линковки # --------------------------- for k, v in new_lot.items(): print('--- %s=%s ---' % (k, v)) is_catched = False for i, j in item.items(): if v == j: catched.append(i) print(' %s=%s' % (i, j)) for i, j in item.items(): if not i in catched: print('___ Not catched %s=%s' % (i, j)) return new_lot
def import_templates(): """Импорт шаблонов""" import_path = 'import' if check_path(import_path): logger.info('[ERROR]: There is not folder import in media') return for item in ListDir(import_path): template_path = os.path.join(import_path, item) if isForD(template_path) == 'file': if template_path.startswith('.DS_Store'): drop_file(template_path) continue json_path = os.path.join(template_path, 'template.json') blocks_path = os.path.join(template_path, 'blocks') with open_file(json_path, 'r', encoding='utf-8') as f: json_obj = json.loads(f.read()) template = Containers.objects.filter(tag=json_obj['tag'], state=json_obj['state']).first() if not template: template = Containers() for k, v in json_obj.items(): if k in ('blocks', 'img', 'id', 'position'): continue setattr(template, k, v) template.save() if not template.img: imga = json_obj.get('img') if imga: src = os.path.join(template_path, imga) dest = os.path.join(template.get_folder(), imga) copy_file(src, dest) Containers.objects.filter(pk=template.id).update(img=imga) blocks = json_obj.get('blocks') if template.blocks_set.all().aggregate(Count('id'))['id__count'] > 0: #logger.info('pass template %s, it is already with blocks' % template.tag) pass else: recursive_fill_blocks(template, blocks, None, None, blocks_path) template_fname = '%s.html' % template.tag template_src = os.path.join(settings.MEDIA_ROOT, template_path, template_fname) template_dest = os.path.join(settings.BASE_DIR, 'apps/site/templates/web/containers/', template_fname) if not os.path.exists(template_dest): if os.path.exists(template_src): shutil.copy2(template_src, template_dest)
def migrate_joints(): """Выполняется после миграции""" f = open_file('migrate_joints.json') data = json.loads(f.read()) f.close() for welding_joint in data: joint = Joint.objects.filter(pk=welding_joint['id']).first() if not joint: logger.info('joint is absent %s' % welding_joint['id']) continue if welding_joint['diameter']: joint.diameter = welding_joint['diameter'] if welding_joint['side_thickness']: joint.side_thickness = welding_joint['side_thickness'] joint.welding_date = str_to_date(welding_joint['welding_date']) joint.save()
def fix_links(self, path: str = None): """Подправить ссылки в файле""" contents = self.load_file(path) # - # A # <a href="http://demo.mohd-biz.com/envato/thedawn-html/"><img src="images/skins/dark-blue/thedawnlogo.png" alt="" /></a> # - matches_a = [] rega_a = re.compile('(<a [^>]*?href=["\']([^"\']+?)["\'][^>])', re.I + re.U + re.DOTALL) matches_a = rega_a.findall(contents) for item in matches_a: if item[1].startswith("http"): cur_link = item[0].replace(item[1], "/") contents = contents.replace(item[0], cur_link) with open_file(self.index_file, "w+") as f: f.write(contents)
def update_access_token(self): """Обновление access_token через refresh_token """ endpoint = '/oauth2/access_token' params = { 'client_id': self.client_id, 'client_secret': self.client_secret, 'grant_type': 'refresh_token', 'refresh_token': self.refresh_token, 'redirect_uri': self.redirect_uri, } r = self.s.post('%s%s' % (self.host, endpoint), data=params) resp = r.json() print(json_pretty_print(resp)) self.set_credentials(resp) with open_file(self.file, 'w+') as f: f.write(json.dumps(resp)) return resp
def get_lots(self, start_date: str, end_date: str): """Получение лотов :param start_date: Дата начала :param end_date: Дата окончания """ urla = '%s%s' % (self.domain, self.lots_endpoint) params = self.default_params.copy() params['start_date'] = start_date params['end_date'] = end_date r = requests.get(urla, params=params) result = {} try: result = r.json() except Exception as e: logger.info('[ERROR]: %s, %s' % (e, r.text)) if result: with open_file('jdocs_get_lots.json', 'w+', encoding='utf-8') as f: f.write(json.dumps(result)) return result
def prepare_migrate_joints(): """Выполняется только до миграции""" f = open_file('migrate_joints.json', 'w+') data = [] for welding_joint in WeldingJoint.objects.select_related('joint').all(): data.append({ 'id': welding_joint.joint.id, 'diameter': '%s' % welding_joint.diameter if welding_joint.diameter else None, 'side_thickness': '%s' % welding_joint.side_thickness if welding_joint.side_thickness else None, 'welding_date': welding_joint.welding_date.strftime('%Y-%m-%d') if welding_joint.welding_date else None, }) f.write(json.dumps(data)) f.close()
def get_css_files(self): """Получить css файлы""" contents = self.load_file() # --- # CSS # <link rel="stylesheet" href="css/style.css" type="text/css" /> # --- if self.with_params: rega_css_with_params = re.compile( '<link[^>]*?href[\s]*=[\s]*["\']([^>"\']+\.[acsxphle]{3,4}[^"\']*?)["\']', re.I + re.U + re.DOTALL) matches_css = rega_css_with_params.findall(contents) else: rega_css = re.compile( '<link[^>]*href[\s]*=[\s]*["\']([^>"\']+\.[acsxphle]{3,4})[^>]*', re.I + re.U + re.DOTALL) matches_css = rega_css.findall(contents) for item in matches_css: css = self.findpath(self.base_path, item) self.css_files.append(css) css_name = os.path.split(css)[1] css_name = self.clean_path(css_name) contents = contents.replace(item, 'css/%s' % css_name) # ----------------------------------- # SUPPORT for @import "css/main.css"; # ----------------------------------- matches_import = rega_import.findall(contents) if matches_import: for item in matches_import: css = self.findpath(self.base_path, item) self.css_files.append(css) css_name = os.path.split(css)[1] css_name = self.clean_path(css_name) contents = contents.replace(item, 'css/%s' % css_name) with open_file(self.index_file, 'w+') as f: f.write(contents) counter = 0 for css in self.css_files: counter += 1 self.analyze_css_file(css)
def handle(self, *args, **options): """Создаем uwsgi файл, который нужно будет разместить в /etc/uwsgi/apps-available/, например, /etc/uwsgi/apps-available/astwobytes.ini""" logger.info("""please, do following: --------------------------------- --- INSTALL uwsgi for python3 --- --------------------------------- # apt-get install uwsgi-plugin-python3 # apt install uwsgi-plugin-python look at /usr/lib/uwsgi/plugins, check python3_plugin.so python_plugin.so if it does not exists if we have python LOWER 3.6, let is build ------------------------------- --- BUILD uwsgi for python3 --- ------------------------------- # sudo apt install python3.6 python3.6-dev uwsgi uwsgi-src uuid-dev libcap-dev libpcre3-dev libssl-dev $ cd ~ $ export PYTHON=python3.6 $ uwsgi --build-plugin "/usr/src/uwsgi/plugins/python python36" $ sudo mv python36_plugin.so /usr/lib/uwsgi/plugins/python36_plugin.so $ sudo chmod 644 /usr/lib/uwsgi/plugins/python36_plugin.so $ uwsgi --plugin python36 -s :0 """) folder = 'demonize' if check_path(folder): make_folder(folder) uwsgi_path = '%s/astwobytes.ini' % (folder, ) with open_file(uwsgi_path, 'w+') as f: f.write("""[uwsgi] processes = 1 master = true plugins = python36 chdir = /home/jocker/sites/astwobytes harakiri = 40 wsgi-file = /home/jocker/sites/astwobytes/conf/wsgi.py pythonpath = /home/jocker/sites/astwobytes/env/lib/python3.6/site-packages buffer-size = 8192 """) logger.info('created %s' % (uwsgi_path, ))
def prepare_data(self, lots): """Подготавливаем лоты для вставки в таблицу :param lots: лоты, которые будем подготавливать """ lots_old_format = [] # Маппинг на старый формат mapping = { 'Client': 'numeprenume', # Клиент 'yahoo_id': 'numar', # Номер лота 'purchase_date': 'data', # Дата покупки 'payment_date': 'data_oplacino', 'warehouse': 'data_primire', 'arrived_date': 'data_primire_ru', # Дата прихода 'qnt1': 'kolicistvo', 'qnt2': 'kolicistvo_client', 'product_name': 'description', # Название лота 'client_price': 'price_pocup', # price? 'client_bank_fee': 'price_bank_com', 'client_delivery_fee': 'price_deliveri_client', # price_deliveri? 'client_others': 'price_alte_client', 'commission': 'price_com', 'client_total': 'price_total', # Итого, клиент ¥ 'international_courier': 'transport', 'yahoo_part_name': 'marka_part', 'part_category_name': 'model_part', 'tracking_no': 'nrotpravka', 'export_date': 'data_livrare', 'weight': 'ves', } for lot in lots: lot_old_format = {} for new_key, old_key in mapping.items(): lot_old_format[old_key] = lot[new_key] lot_old_format['images'] = [{ 'link': img['image_full_path'], } for img in lot.get('images', [])] lots_old_format.append(lot_old_format) with open_file('jdocs_prepared_data.json', 'w+', encoding='utf-8') as f: f.write(json.dumps(lots_old_format)) return lots_old_format
def create_robots_txt(domain: dict = None): """Вспомогательная функция для создания robotx.txt файла :param domain: словарь с параметрами домена """ if not domain: domain = {} robots_txt = Files.objects.create( link=robots_txt_link, name='robots.txt', desc='Файл для запретов индексации поисковым системам', domain=domain.get('pk')) folder = robots_txt.get_folder() make_folder(folder) dest = '%s%s' % (folder.rstrip('/'), robots_txt_link) with open_file(dest, mode='w+', encoding='utf-8') as f: domain_site = domain.get('domain') if not domain_site: domain_site = settings.MAIN_DOMAIN or 'masterme.ru' f.write(robots_txt_content.format(domain_site)) robots_txt.path = robots_txt_link.lstrip('/') robots_txt.save()
def handle(self, *args, **options): """Работа с задачами Tasks""" # Вывод логов if options.get('log'): if check_path('tasks_errors.txt'): logger.info('empty file') return with open_file('tasks_errors.txt', 'r') as f: content = f.read() print(content) return is_running = search_process(q=('tasks', 'manage.py')) if is_running: logger.info('Already running %s' % (is_running, )) exit() now = datetime.datetime.today() tasks = Tasks.objects.filter(is_active=True).order_by('updated') for task in tasks: if task.updated <= now: run_task(task)
def __init__(self, **kwargs): """На примере mir-ins.ru https://directmir163ru.amocrm.ru Документация: https://www.amocrm.ru/developers/content/oauth/step-by-step """ self.file = 'amocrm_credentials.txt' self.redirect_uri = 'https://mir-ins.ru/amocrm/' self.host = 'https://directmir163ru.amocrm.ru' self.s = requests.Session() self.client_secret = 'Tpi94PsVqnwF7JRoJsi0KnnAG4Od3msqlqzGyflONkl5MZtyfCY5SPMQE49kLaSe' self.client_id = 'cd184f47-26b3-4011-9820-83f3ef5232e5' # Код из админки для получения access_token self.code = 'def502002981f459578c7f343c5e31acfa11de7e642733e27ca945a58e9b66b2ce44b5a993d2569422aa4c1c80d02e2936dfca0981870683e4ad40841cba1f6eff165349e2f8cca73097f970f8e676e0343de5526f1c071ee134b735e65592252e15a9e6ecb9271769c45b2e5f65f67d92d456a056773bdba0fa19f9392b01e7c2c7ea3ac67ccc7b7fafa17a916031cbf49bdee372d7d6544668740aa070500aced0e0d5453d2381cd7f305495670ce828386229fed61a7d86fb23329e8d399869f0eb187aaae39a6b7578049b1d3d9c86cb3277c1ce02c33d8e89d1e342997cceddd48cc93f3fc06f87320e6874b9e17b555003fa5cb153e056900753d79c797fa76e6dc94130d866a5f63e5ea414f239c9afc17c06c847c809ed305678e7d38d1979639c1c3cd36db77e703f93901f21c9998e2b2e4b27273aaaa00a9114e7f8e7faa7fb83938b8838bf368a1ee73b1e7941206c70eab4e3585b48708504ee0345878fc09cd6880fbfcaac65a274ee2024d3da337703c0b45007c8e924132231e98a924a90de2ebc8c9a4d0b50b2e7636a68e030d29fd7a7c2df2b61396cf008c407a3752478cac5cb15871a81c0083b7a78cb645f95ec973aa58e7bc65f2a9392a6ba25' self.access_token = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImp0aSI6IjNjOTY2ZmMxY2NmOGZmMzdmMGFmNzQwNGRmMDc2Njk2MTMwMjNjYWQwMzRhMWNjM2RkZmU5MmJiNjI1MTAxNTE3NWQ2NjAzMzBjOGI5MWJjIn0.eyJhdWQiOiJjZDE4NGY0Ny0yNmIzLTQwMTEtOTgyMC04M2YzZWY1MjMyZTUiLCJqdGkiOiIzYzk2NmZjMWNjZjhmZjM3ZjBhZjc0MDRkZjA3NjY5NjEzMDIzY2FkMDM0YTFjYzNkZGZlOTJiYjYyNTEwMTUxNzVkNjYwMzMwYzhiOTFiYyIsImlhdCI6MTYwNDkxOTk0MywibmJmIjoxNjA0OTE5OTQzLCJleHAiOjE2MDUwMDYzNDMsInN1YiI6Ijk5NTQ0NiIsImFjY291bnRfaWQiOjE5NDgwODEzLCJzY29wZXMiOlsicHVzaF9ub3RpZmljYXRpb25zIiwiY3JtIiwibm90aWZpY2F0aW9ucyJdfQ.a-rejYPPlCmJT4-A8cQZGdaOC3XVBmNugxLcn0hxKGKagndE5Q-PD3q7PhPl2bhJcRhW6EJS1AvB8lao07KqIkj7MZ9nlxI170ily1sivJbKCSyqeL8Rhen6f8Kx2032VD0dWCv2Zwy4eP1m-fpusTdvPwt5eYKmyXo5ig6haH1mwEbM2QRXPXPmMjZLAO6F-T4eKdYJPI2waFYFCWvMm3xhAco4wKfhjmkckRBEPPzaT0aFA3Gi8ivg8SsKfnRzfNgCKxWXHkPPjpj5HuLt5svBYzHUejcHEbq7d_YF3TT0RLN98dmKTrzzkb8B0XYScci9mRIVEZve12tG1UKi7Q' self.refresh_token = 'def502006b01bcf4b70f3b4988642c13f5544c079671d0d06bbc68beab4cda5d692370014b059037b13678e51f87d393eeba2a12683c2e874503ae5e0abc7698e335024e635426ce2f25ba2daa5d083758743e0d40a43bb4548aa24d5c190c263b350b29a34c3fe506a911669b92c15ee9562f6d55b2344c2bf74fe977d9d7cb8244f8fe6cf8696a40a26de81e11c8e0aebe2b452bf4e3ccd8822768acb9837dffa175abf8cf9eaf3f5921db5f974d5fda7c26ea2c28e2ad73f67d98c2be4ce666d3dc43b1f8906366778cb367994e29020128caf44f05f63e6b8b1f5266b6533fe0e6456b3529594f3cb08da758f56081363215f04915198af0ccbfa5ab3861382c08eb4a7d3a641dd328dfabe3212421da46f21174ed7d70c6c84e27daabf7be37bb26538920cf1c97b936ea73165a2a0a3612505fd7a4a3e200240d34cf78a20ad5c43c7c4ff10f292eae99c4672cd75d02d6dece6c6b086e9e882e7c45299b2716641ea1c24e6e297562087e126aa07f29445cb45c772d86108b4f9d1a636e98462aac7bd810d2a753561c4892151b6a95094d1da7cd5dfab81b66c036c7e31cd0388874554153ae5ead082b26524a906f0655a605a00fd086051469783e09b7bc81ee2441e180' self.headers = {'Authorization': 'Bearer %s' % self.access_token} if not check_path(self.file): with open_file(self.file, 'r') as f: content = json.loads(f.read()) self.set_credentials(content)
def run_task(task): """Запускает фоновую задачу из Tasks очереди :param task: фоновая задача, где task.command - команда строкой """ opts = {} command = task.command # Саму себя не выполняем if not command or 'tasks' in command: task.delete() return now = datetime.datetime.today().strftime('%H:%M:%S %d/%m/%Y') logger.info("START: %s" % now) with open_file('tasks_errors.txt', 'a+') as ferrors: ferrors.write('%s - %s\n' % (now, command)) if '--' in command: cur_name = command.split('--') command = cur_name[0].strip() for i in range(len(cur_name)): if i == 0: continue if cur_name[i]: if '=' in cur_name[i]: key, value = cur_name[i].split('=', 1) key = str(key.strip()) value = value.strip() opts[key] = value else: key = cur_name[i].strip() opts[key] = True try: task.delete() call_command(command, **opts) except: error = traceback.format_exc() ferrors.write('%s\n' % error)
def export_templates(): """Экспорт шаблонов""" templates = Containers.objects.filter(state__in=(99, 100), tag__isnull=False) for template in templates: make_folder('export') path = os.path.join('export', template.tag) make_folder(path) if template.img: copy_file(os.path.join(template.get_folder(), template.img), os.path.join(path, template.img)) obj = object_fields(template) obj['blocks'] = [] blocks = template.blocks_set.all() blocks_path = os.path.join(path, 'blocks') make_folder(blocks_path) for block in blocks: if block.img: copy_file(os.path.join(block.get_folder(), block.img), os.path.join(blocks_path, block.img)) obj['blocks'].append( object_fields(block, pass_fields=('container', ))) json_path = os.path.join(path, 'template.json') with open_file(json_path, 'w+', encoding='utf-8') as f: f.write(json.dumps(obj)) template_fname = '%s.html' % template.tag template_src = os.path.join(settings.BASE_DIR, 'apps/site/templates/web/containers/', template_fname) template_dest = os.path.join(settings.MEDIA_ROOT, path, template_fname) if os.path.exists(template_src): shutil.copy2(template_src, template_dest) else: logger.info('template not found %s' % template_src)