def publish(soup, args): client = ConfluenceAPI(username=args.username, password=args.password, uri_base=args.endpoint) EXPAND_FIELDS = 'body.storage,space,ancestors,version' content = client.get_content_by_id(content_id=args.page_id, expand=EXPAND_FIELDS) content_data = { 'id': args.page_id, 'type': 'page', #'space': {'key': space_key}, 'title': 'New Page', 'body': { 'storage': { 'value': str(soup), 'representation': 'storage' } }, #'ancestors': [] if not ancestor_id else [{'id': ancestor_id}] "version": { "number": content['version']['number'] + 1 }, } client.update_content_by_id(content_data=content_data, content_id=args.page_id)
def __init__(self): username = self.username password = self.password if not self.username: username = raw_input('Confluence user name: ') if not self.password: password = getpass.getpass('Confluence password (hidden): ') self.api = ConfluenceAPI(username, password, self.base_url)
class ConfluenceAPIHelper: username = '******' password = '' base_url = 'https://brianjing.atlassian.net/wiki' def __init__(self): username = self.username password = self.password if not self.username: username = raw_input('Confluence user name: ') if not self.password: password = getpass.getpass('Confluence password (hidden): ') self.api = ConfluenceAPI(username, password, self.base_url) # api.create_new_space({ # 'key': 'LOL', # 'name': 'My testing space', # 'description': { # 'plain': {'value': 'This is my new testing space', 'representation': 'plain'} # } # }) #Generate a page for our space def create_page(self, content): print content space_key = raw_input('Space key: ') page_title = raw_input('Page title: ') self.api.create_new_content({ 'type': 'page', 'title': page_title, 'space': { 'key': space_key }, 'body': { 'storage': { 'value': content, 'representation': 'storage' } } }) def test_create_page(self): #Generate a page for our space self.api.create_new_content({ 'type': 'page', 'title': 'My landing page for TESTSPACE!', 'space': { 'key': 'HAC' }, 'body': { 'storage': { 'value': '<h1>Welcome to the landing page!</h1><p>Lorem Ipsum</p>', 'representation': 'storage' } } })
def __init__(self, username, password, uri_base): ConfluenceAPI.__init__(self, username, password, uri_base) self.uri_base = uri_base if uri_base.endswith('/') else uri_base + "/" self.search_words = [] self.lim = 100 self.expands = [] self.response = None self.to_be_updated = [] self.responses = []
def getFileContent(accountInfo, fileID): # Create API object connect = ConfluenceAPI( accountInfo['username'], accountInfo['password'], 'https://' + accountInfo['siteDomain'] + '.atlassian.net/wiki/') # Get latest visible content from confluence instance page = connect.get_content_by_id( fileID, expand= 'body.view,childTypes.all,operations,history,metadata.currentuser') # Get content content = page['body']['view']['value'] return content
class ConfluenceAPIHelper: username = '******' password = '' base_url = 'https://brianjing.atlassian.net/wiki' def __init__(self): username = self.username password = self.password if not self.username: username = raw_input('Confluence user name: ') if not self.password: password = getpass.getpass('Confluence password (hidden): ') self.api = ConfluenceAPI(username, password, self.base_url) # api.create_new_space({ # 'key': 'LOL', # 'name': 'My testing space', # 'description': { # 'plain': {'value': 'This is my new testing space', 'representation': 'plain'} # } # }) #Generate a page for our space def create_page(self, content): print content space_key = raw_input('Space key: ') page_title = raw_input('Page title: ') self.api.create_new_content({ 'type': 'page', 'title': page_title, 'space': {'key': space_key}, 'body': { 'storage': {'value': content, 'representation': 'storage'} } }) def test_create_page(self): #Generate a page for our space self.api.create_new_content({ 'type': 'page', 'title': 'My landing page for TESTSPACE!', 'space': {'key': 'HAC'}, 'body': { 'storage': {'value': '<h1>Welcome to the landing page!</h1><p>Lorem Ipsum</p>', 'representation': 'storage' } } })
def getFile(accountInfo, fileID): """Returns complete exported file data""" # Create API object connect = ConfluenceAPI( accountInfo['username'], accountInfo['password'], 'https://' + accountInfo['siteDomain'] + '.atlassian.net/wiki/') # Get latest visible content from confluence instance page = connect.get_content_by_id( fileID, expand= 'body.view,childTypes.all,operations,history,history.lastUpdated,metadata.currentuser' ) # Convert to file info f = confluenceToFile(page, accountInfo) return f
def __init__(self, ConfluenceConfig: Configuration.ConfluenceConfig, MediaWIKIConfig: Configuration.MediaWIKIConfig, xWikiConfig: Configuration.XWikiConfig): self.confluenceAPI = ConfluenceAPI(ConfluenceConfig.USER, ConfluenceConfig.PASS, ConfluenceConfig.ULR) self.ConfluenceConfig = ConfluenceConfig self.MediaWIKIConfig_instance = MediaWIKIConfig self.xWikiConfig = xWikiConfig self.xWikiClient = XWikiClient(xWikiConfig.api_root, xWikiConfig.auth_user, xWikiConfig.auth_pass) self.tag_list = [] self.file_list = [] self.current_page_id = None
def listFiles(accountInfo, after=False, number=500): # 'after' and 'number' don't do anything! # Create API object connect = ConfluenceAPI( accountInfo['username'], accountInfo['password'], 'https://' + accountInfo['siteDomain'] + '.atlassian.net/wiki/') # Get latest file info response = connect.get_content( expand= 'childTypes.all,operations,history,history.lastUpdated,metadata.currentuser' ) pp.pprint(response) files = [ confluenceToFile(page, accountInfo) for page in response['results'] ] return files
def initialize(task_pages_dict: dict, logging_mode: str = 'INFO', log_to_file_var: bool = False, contrib_log_mode: str = 'silent'): ################################################################################################################### # Contrib_Compare_inst - used to analyze pages and create page contribution maps based on the content, # # collected from one of supported platforms # # mysql_connector_inst, sql_connector_inst - used to store\load page contribution maps in\from SQL\mySQL # # xWikiAPI_inst, mWikiAPI_instance, ConfluenceAPI_inst - used to get data from the supported sources # # # ################################################################################################################### formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') logger_inst = logging.getLogger() logger_inst.setLevel(logging_mode) integration_config = Configuration.Integration() if log_to_file_var is True: if len(task_pages_dict) == 1: task_pages_dict_temp = task_pages_dict.copy() log_title, log_platform = task_pages_dict_temp.popitem() log_name = integration_config.CC_log_location + "Core_v2.1_" + str( datetime.now().strftime( "%Y-%m-%d_%H_%M_%S")) + '_' + str(log_title).replace( '/', '').replace('\\', '').replace(' ', '_').replace( '"', '_') + '_' + log_platform + '.log' else: log_name = integration_config.CC_log_location + "Core_v2.1_" + str( datetime.now().strftime("%Y-%m-%d_%H_%M_%S")) + '.log' fh = logging.FileHandler(log_name) fh.setLevel(logging_mode) fh.setFormatter(formatter) logger_inst.addHandler(fh) ch = logging.StreamHandler() ch.setLevel(logging_mode) ch.setFormatter(formatter) logger_inst.addHandler(ch) contrib_compare_inst = ContributionComparator(contrib_log_mode) sql__config_inst = Configuration.SQLConfig() confluence__config_inst = Configuration.ConfluenceConfig() m_wiki__config_inst = Configuration.MediaWIKIConfig() x_wiki__config_inst = Configuration.XWikiConfig( ['Migration pool', 'Sandbox', 'Main', 'StagingWiki']) mysql__config_inst = Configuration.MySQLConfig() mysql_connector_inst = MysqlConnector(mysql__config_inst) sql_connector_inst = SQLConnector(sql__config_inst) confluence_api_inst = ConfluenceAPI(confluence__config_inst.USER, confluence__config_inst.PASS, confluence__config_inst.ULR) x_wiki_api_inst = XWikiClient(x_wiki__config_inst.api_root, x_wiki__config_inst.auth_user, x_wiki__config_inst.auth_pass) m_wiki_api_instance = Site( (m_wiki__config_inst.Protocol, m_wiki__config_inst.URL), path=m_wiki__config_inst.APIPath, clients_useragent=m_wiki__config_inst.UserAgent) search_config_inst = Configuration.SearchConfig() return contrib_compare_inst, mysql_connector_inst, confluence_api_inst, sql_connector_inst, logger_inst, x_wiki_api_inst, m_wiki_api_instance, search_config_inst
def activate(self): """Triggers on plugin activation""" self.log.debug( self.config['CONFLUENCE_USERNAME'], self.config['CONFLUENCE_PASSWORD'], self.config['CONFLUENCE_ENDPOINT'] ) self.api = ConfluenceAPI( self.config['CONFLUENCE_USERNAME'], self.config['CONFLUENCE_PASSWORD'], self.config['CONFLUENCE_ENDPOINT'] ) super(Chatfluence, self).activate()
def initialize(logging_mode: str = 'DEBUG', log_to_file: bool = True): ################################################################################################################### # Contrib_Compare_inst # # Main instance, used to analyze pages and create page contribution maps based on the content, # # collected from one of supported platforms # # Mysql_Connector_inst # # Here used to connect to xWIKI DB to get a list of pages from a requested space # # ConfluenceAPI_inst # # ConfluenceAPI_inst - same for Confluence # # SQL_Connector_inst # # Used to store\load page contribution maps in\from SQL # # Page_Creator_inst # # Creates PAGE objects - data handlers for currently analyzed page # ################################################################################################################### formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s') logger_inst = logging.getLogger() logger_inst.setLevel(logging_mode) Integration_config = Configuration.Integration() if log_to_file is True: log_name = Integration_config.log_location + "Comparer_task_builder_v1.0_" + str(datetime.now().strftime("%Y-%m-%d_%H_%M_%S", )) + '.log' fh = logging.FileHandler(log_name) fh.setLevel(logging_mode) fh.setFormatter(formatter) logger_inst.addHandler(fh) ch = logging.StreamHandler() ch.setLevel(logging_mode) ch.setFormatter(formatter) logger_inst.addHandler(ch) contrib_compare_inst = ContributionComparator() SQL_config_inst = Configuration.SQLConfig() confluence_config_inst = Configuration.ConfluenceConfig() try: media_w_i_k_i__config_inst = Configuration.MediaWIKIConfig() media_wiki_api_inst = Site((media_w_i_k_i__config_inst.Protocol, media_w_i_k_i__config_inst.URL), path=media_w_i_k_i__config_inst.APIPath, clients_useragent=media_w_i_k_i__config_inst.UserAgent,retry_timeout=1, max_retries=1) except Exception: media_wiki_api_inst = None xWiki_Config_inst = Configuration.XWikiConfig(['Migration pool', 'Sandbox', 'Main', 'StagingWiki']) MySQL_Config_inst = Configuration.MySQLConfig() Mysql_connector_inst = MysqlConnector(MySQL_Config_inst) SQL_connector_inst = SQLConnector(SQL_config_inst) # getting all pages in Confluence: confluenceAPI_inst = ConfluenceAPI(confluence_config_inst.USER, confluence_config_inst.PASS, confluence_config_inst.ULR) return contrib_compare_inst, Mysql_connector_inst, confluenceAPI_inst, SQL_connector_inst, logger_inst, media_wiki_api_inst
#!/usr/bin/env python import sys import os import urllib import json from PythonConfluenceAPI import ConfluenceAPI user = sys.argv[1] pswd = sys.argv[2] source_path = sys.argv[3] file_name = sys.argv[4] #url = "http://confluence.test.com/rest/api/content/" #api = ConfluenceAPI(user, pswd, 'http://confluence.test.com/') url = "http://dev.confluence.test.com:8090/rest/api/content/" api = ConfluenceAPI(user, pswd, 'http://dev.confluence.test.com:8090/') def get_content_dict(url): open_url = urllib.urlopen(url) content = open_url.read() return json.loads(content) list_id = [] for page_dict in get_content_dict(url)["results"]: list_id.append(page_dict["id"]) for content_id in list_id: file_object = open(os.path.join(source_path, file_name), 'r') attachment = {"file": file_object, "comment": ""}
__author__ = 'Robert Cope' from PythonConfluenceAPI import ConfluenceAPI USERNAME = '' PASSWORD = '' WIKI_SITE = 'https://my-awesome-organization.atlassian.net/wiki' api = ConfluenceAPI(USERNAME, PASSWORD, WIKI_SITE) api.create_new_space({ 'key': 'LOL', 'name': 'My testing space', 'description': { 'plain': { 'value': 'This is my new testing space', 'representation': 'plain' } } }) # Generate a page for our space api.create_new_content({ 'type': 'page', 'title': 'My landing page for TESTSPACE!', 'space': { 'key': 'LOL' }, 'body': { 'storage': { 'value': '<h1>Welcome to the landing page!</h1><p>Lorem Ipsum</p>', 'representation': 'storage' }
from PythonConfluenceAPI import ConfluenceAPI api = ConfluenceAPI('admin', '123@qwe', 'http://127.0.0.1:8090') spaces = api.get_space_content('TKB') print(spaces) PageBody = '<ac:layout><ac:layout-section ac:type="two_equal"><ac:layout-cell>' \ '<h2>General info:</h2>' \ '<ac:structured-macro ac:name="details"><ac:parameter ac:name="id">1</ac:parameter><ac:rich-text-body>' \ '<table>' \ '<tbody>' \ '<tr>' \ '<td><strong>Case ID:</strong></td>' \ '<td colspan="1">01980837</td></tr>' \ '<tr>' \ '<td><strong>Found in Version:</strong></td>' \ '<td colspan="1">9.0</td></tr>' \ '<tr>' \ '<td><strong>To be fixed in:</strong></td>' \ '<td colspan="1">9.5 update 1</td></tr>' \ '<tr>' \ '<td><strong>Current status:</strong></td>' \ '<td colspan="1"><ac:structured-macro ac:name="status"><ac:parameter ac:name="colour">Green</ac:parameter><ac:parameter ac:name="title">fixed</ac:parameter><ac:parameter ac:name="subtle">true</ac:parameter></ac:structured-macro></td></tr>' \ '<tr>' \ '<td><strong>Comments:</strong></td>' \ '<td colspan="1">none</td></tr>' \ '<tr>' \ '<td colspan="1"><strong>Components: </strong></td>' \ '<td colspan="1"><span>Agent, Backup, Rescan</span></td></tr></tbody></table></ac:rich-text-body></ac:structured-macro></ac:layout-cell><ac:layout-cell>' \ '<p><ac:structured-macro ac:name="attachments" /></p></ac:layout-cell></ac:layout-section><ac:layout-section ac:type="single"><ac:layout-cell>' \ '<h2><span class="mw-headline">Description</span></h2>' \ '<p>Server is added to Veeam using public ip, which is not listed inside guest.<br />Veeam does not have direct access to private ip on linux, so the communication should go over public ip.<br />We login to this Server via SSH using correct ip and start agent there, but agents connection goes only via private one.</p><ac:structured-macro ac:name="code"><ac:parameter ac:name="theme">Emacs</ac:parameter><ac:parameter ac:name="linenumbers">true</ac:parameter>' \
def confluence(html, metadata): # Create API object. api = ConfluenceAPI(g_user, g_pass, g_confluence_url) update_content = False url = '' current_history = 0 update_content_id = '' # Check if confluence_id is not null and refer to a valid post if metadata['confluence_id'] is not None: content = api.get_content_by_id(metadata['confluence_id']) if content is not None: update_content = True current_history = content['version']['number'] update_content_id = metadata['confluence_id'] logging.info('updating post with given id') # Check if space is available if (metadata['confluence_space'] is not None) and (not update_content): space = api.get_space_information(metadata['confluence_space']) if space is None: raise Exception('Space not found') # Check if title is unique if (metadata['title'] is not None) and (not update_content): content = api.get_content(title=metadata['title']) if content is not None: if content['size'] > 0: if g_update_title: update_content = True found_id = content['results'][0]['id'] found_post = api.get_content_by_id(found_id) if found_post is not None: current_history = found_post['version']['number'] update_content_id = found_id logging.info('Updating duplicate Title') else: raise Exception('Title is found but not updating') if not update_content: post_content = { "type": "page", "title": metadata['title'], "space": { "key": metadata['confluence_space'] }, "body": { "storage": { "value": html, "representation": "storage" } }, "ancestors": [{ "id": metadata['confluence_parent_id'] }] } res = api.create_new_content(post_content) links = res['_links'] url = links['base'] + links['webui'] else: post_content = { "id": update_content_id, "type": "page", "title": metadata['title'], "space": { "key": metadata['confluence_space'] }, "version": { "number": current_history + 1, "minorEdit": False }, "body": { "storage": { "value": html, "representation": "storage" } } } res = api.update_content_by_id(post_content, update_content_id) links = res['_links'] url = links['base'] + links['webui'] return url
__author__ = 'Robert Cope' from PythonConfluenceAPI import ConfluenceAPI USERNAME = '' PASSWORD = '' WIKI_SITE = 'https://my-awesome-organization.atlassian.net/wiki' api = ConfluenceAPI(USERNAME, PASSWORD, WIKI_SITE) api.create_new_space({ 'key': 'LOL', 'name': 'My testing space', 'description': { 'plain': {'value': 'This is my new testing space', 'representation': 'plain'} } }) # Generate a page for our space api.create_new_content({ 'type': 'page', 'title': 'My landing page for TESTSPACE!', 'space': {'key': 'LOL'}, 'body': { 'storage': {'value': '<h1>Welcome to the landing page!</h1><p>Lorem Ipsum</p>', 'representation': 'storage' } } })
__author__ = 'Robert Cope' from PythonConfluenceAPI import ConfluenceAPI import sys def compatible_print(msg): sys.stdout.write("{}\n".format(msg)) sys.stdout.flush() USERNAME = '' PASSWORD = '' WIKI_SITE = 'https://my-awesome-organization.atlassian.net/wiki' api = ConfluenceAPI(USERNAME, PASSWORD, WIKI_SITE) new_pages = api.get_content('') compatible_print("Newest pages:") for page in new_pages: compatible_print("{} - {} ({})".format( page.get("space", {}).get("key", "???"), page.get("title", "(No title)"), page.get("id", "(No ID!?)"))) content = page.get("body", {}).get("view", {}).get("value", "No content.") content = content[:37] + "..." if len(content) > 40 else content compatible_print("Preview: {}".format(content))
__author__ = "Robert Cope" from PythonConfluenceAPI import ConfluenceAPI import sys def compatible_print(msg): sys.stdout.write("{}\n".format(msg)) sys.stdout.flush() USERNAME = "" PASSWORD = "" WIKI_SITE = "https://my-awesome-organization.atlassian.net/wiki" api = ConfluenceAPI(USERNAME, PASSWORD, WIKI_SITE) new_pages = api.get_content("") compatible_print("Newest pages:") for page in new_pages: compatible_print( "{} - {} ({})".format( page.get("space", {}).get("key", "???"), page.get("title", "(No title)"), page.get("id", "(No ID!?)") ) ) content = page.get("body", {}).get("view", {}).get("value", "No content.") content = content[:37] + "..." if len(content) > 40 else content compatible_print("Preview: {}".format(content))
class Chatfluence(BotPlugin): """An Err plugin chatfluence""" min_err_version = '2.0.0' # Optional, but recommended max_err_version = '3.3.0' # Optional, but recommended def get_configuration_template(self): return CONFIG_TEMPLATE def activate(self): """Triggers on plugin activation""" self.log.debug( self.config['CONFLUENCE_USERNAME'], self.config['CONFLUENCE_PASSWORD'], self.config['CONFLUENCE_ENDPOINT'] ) self.api = ConfluenceAPI( self.config['CONFLUENCE_USERNAME'], self.config['CONFLUENCE_PASSWORD'], self.config['CONFLUENCE_ENDPOINT'] ) super(Chatfluence, self).activate() @botcmd() def recent(self, mess, args): """A command that shows recent blog posts. Optionally pass in a result limit.""" if args: limit = args else: limit = self.config['LIMIT'] results = self._recent(limit=limit) endpoint = self.config['CONFLUENCE_ENDPOINT'] for result in results: posted = self._get_posted_date(result['id'])[:10] line = '{0} {1} {2}{3}'.format( posted, result['title'], endpoint.rstrip('/'), result['_links']['tinyui'] ) yield line def _recent(self, limit=None, space=None): """Method to fetch recent blog posts wihtin a space.""" if not limit: limit = self.config['LIMIT'] if not space: space = self.config['CONFLUENCE_KEYSPACE'] recent = self.api.get_content( content_type='blogpost', space_key=space, limit=limit, ) return recent['results'] @botcmd() def search(self, mess, args): """A command that searches confluence. This uses the CQL language. https://developer.atlassian.com/confdev/confluence-rest-api/advanced-searching-using-cql """ if '=' not in str(args) and '~' not in str(args): cql = 'text ~ "{0}"'.format(args) else: cql = str(args) # remove slack's smart quotes cql = cql.replace('“', '"').replace('”', '"') self.log.debug('search: {0}'.format(cql)) results = self._search(cql) for result in results: posted = self._get_posted_date(result['id'])[:10] line = '{0} {1}'.format( posted, result['title'], ) yield line self.log.debug(result) def _search(self, cql, limit=None): """Searches confluence.""" if not limit: limit = self.config['LIMIT'] results = self.api.search_content( cql_str=cql, limit=limit ) return results['results'] def _get_posted_date(self, pageid): """Gets date content was created""" page = self.api.get_content_by_id(content_id=pageid) posted = page['history']['createdDate'] return posted # TODO: can these patterns come from the configuration? @re_botcmd(pattern=r"(^| )@news?( |$)", prefixed=False, flags=re.IGNORECASE) def listen_for_news(self, msg, match): """Mention @news and I will blog it.""" content = 'Posted by {0}: \n {1}'.format(msg.frm, msg.body) title = ' '.join(msg.body.split()[0:5]) keyspace = self.config['CONFLUENCE_KEYSPACE'] result = self._post(title, content, keyspace) return result def _post(self, title, content, space): """Method to make a blog post in a space.""" try: r = self.api.create_new_content({ 'type': 'blogpost', 'title': title, 'space': {'key': space}, 'body': { 'storage': {'value': content, 'representation': 'storage' } } }) self.log.debug(r) line = 'POSTED: {0} {1} {2}/{3}'.format( r['history']['createdDate'][:10], r['title'], r['_links']['base'], r['_links']['tinyui'], ) except Exception as e: self.log.critical(e) return line
from grab import Grab import logging from simple_salesforce import Salesforce import pyodbc import re import traceback from datetime import datetime from PythonConfluenceAPI import ConfluenceAPI import KB_upload import Configuration ConfluenceConfig = Configuration.ConfluenceConfig() SQLConfig = Configuration.SQLConfig() SFConfig = Configuration.SFConfig() #configuring Confluence connection: api = ConfluenceAPI(ConfluenceConfig.USER, ConfluenceConfig.PASS, ConfluenceConfig.ULR) #configuring SQL connection: cnxn = pyodbc.connect('DRIVER=' + SQLConfig.Driver + ';PORT=1433;SERVER=' + SQLConfig.Server + ';PORT=1443;DATABASE=' + SQLConfig.Database + ';UID=' + SQLConfig.Username + ';PWD=' + SQLConfig.Password) cursor = cnxn.cursor() #configuring SF connection: sf = Salesforce(username=SFConfig.User, password=SFConfig.Password, security_token=SFConfig.SecurityToken) #configuring Grab: g = Grab() def checking_existence(dictionary):
from mwclient import Site from PythonConfluenceAPI import ConfluenceAPI import re from simple_salesforce import Salesforce api = ConfluenceAPI('admin', '123@qwe', 'http://127.0.0.1:8090') FileOut = open('PagesBUSList.txt', 'w') UserAgent = 'Wiki_parser/0.1 run by DremSama' site = Site(('http', 'wiki.support.veeam.local'), path='/', clients_useragent=UserAgent) sf = Salesforce(username='******', password='******', security_token='dNr44yHsFXaSuRmKXunWPlzS') PagesList = [] PagesBUGSList = [] for page in site.allpages(): PagesList.append(page) if page.name.startswith('Bug') or page.name.startswith('bug'): print('----------------------------------') BugID = re.match(r'[A-z,a-z]ug\b.(\d*)[-|\s]*(.*)', page.page_title) if BugID: BugID_NUM = BugID.group(1) BugID_SUBJECT = BugID.group(2) textALL = page.text(0) if not textALL: print('ERROR: Page "' + page.name + '" has no text') elif textALL.startswith('#REDIRECT'): print('Page "' + page.name + '" is only a redirect page, skipping') else:
class MigrationAssistant(object): def __init__(self, ConfluenceConfig: Configuration.ConfluenceConfig, MediaWIKIConfig: Configuration.MediaWIKIConfig, xWikiConfig: Configuration.XWikiConfig): self.confluenceAPI = ConfluenceAPI(ConfluenceConfig.USER, ConfluenceConfig.PASS, ConfluenceConfig.ULR) self.ConfluenceConfig = ConfluenceConfig self.MediaWIKIConfig_instance = MediaWIKIConfig self.xWikiConfig = xWikiConfig self.xWikiClient = XWikiClient(xWikiConfig.api_root, xWikiConfig.auth_user, xWikiConfig.auth_pass) self.tag_list = [] self.file_list = [] self.current_page_id = None def get_tags(self, platform: str, id: str = None, test_str: str = None): self.tag_list = [] if test_str is None and id is None: return False if platform == 'Confluence': self.current_page_id = id result = self.confluenceAPI.get_content_labels(id, prefix=None, start=None, limit=None, callback=None) for each in result['results']: self.tag_list.append(each['name']) return self.tag_list elif platform == 'MediaWIKI': regex = r"\[\[Category:(.[^\]]*)\]\]" matches = re.finditer(regex, test_str, re.IGNORECASE) for matchNum, match in enumerate(matches): matchNum = matchNum + 1 match = match.group(1) self.tag_list.append(match) return self.tag_list def get_files(self, platform: str, id: str = None, test_str: str = None): self.file_list = [] if test_str is None and id is None: return False if platform.lower() == 'confluence': self.current_page_id = id regex = r"\<ri\:attachment ri\:filename=\"(.[^\"]*)\" \/\>" matches = re.finditer(regex, test_str) for matchNum, match in enumerate(matches): matchNum = matchNum + 1 match = match.group(1) self.file_list.append(match) self.file_list = list(set(self.file_list)) print(self.file_list) return self.file_list elif platform.lower() == 'mediawiki': regex = r"\[\[File:((\w|\d|-| |\.[^\|])*).*" matches = re.finditer(regex, test_str, re.IGNORECASE) # added ignore case option # print('test_str', test_str) for matchNum, match in enumerate(matches): matchNum = matchNum + 1 match = match.group(1) self.file_list.append(match) return self.file_list def make_and_attach(self, platform: str, file_name: str, page: str, space): source_url = None if platform.lower() == 'confluence': if self.current_page_id is None: print('current_page_id is still None') return False attachment = self.confluenceAPI.get_content_attachments( self.current_page_id, expand=None, start=None, limit=None, filename=file_name, media_type=None, callback=None) source_url = self.ConfluenceConfig.ULR + attachment['results'][0][ '_links']['download'] elif platform.lower() == 'mediawiki': # so, now we need to locate the attachment request_url = self.MediaWIKIConfig_instance.APIPath_long + 'action=query&titles=File:' + file_name + '&prop=imageinfo&iiprop=url&format=json' r = requests.get(request_url, stream=True) if r.status_code == 200: respond = r.json() answer = str(respond['query']['pages']) regex = r"'url': '(.[^']*)'" matches = matches = re.finditer(regex, answer) for matchNum, match in enumerate(matches): matchNum = matchNum + 1 match = match.group(1) source_url = match break # using only the fist link else: print('ERROR: unable to find the source link for', file_name, request_url) return False if source_url is None: return None r = requests.get(source_url, stream=True) file_content = None if r.status_code == 200: file_content = r.content if file_content is not None: print(file_content) result = self.xWikiClient.add_new_attach_application( space=space, page=page, attach_name=file_name, attach_content=file_content) return result