Skip to content

Commit c15ea92

Browse files
committedJun 4, 2021
get incoming links for confluence pages
1 parent 577026e commit c15ea92

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
 

‎confluence-link/get-link.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Uses environmental variable for JIRA_TOKEN
2+
3+
import os, requests, json
4+
from bs4 import BeautifulSoup
5+
from jira import JIRA
6+
7+
8+
# CONFIG
9+
10+
11+
12+
13+
14+
## Get page children
15+
root_page = '1970864252'
16+
headers = {
17+
"Accept": "application/json"
18+
}
19+
expand = {
20+
"expand" : "descendants.page"
21+
}
22+
base_url = "https://implydata.atlassian.net"
23+
children_url = "https://implydata.atlassian.net/wiki/rest/api/content/%s/child/page"%(root_page)
24+
info_url = "https://implydata.atlassian.net/wiki/pages/viewinfo.action"
25+
#print(children_url)
26+
children =requests.get(children_url, auth=(os.getenv('JIRA_USER'), os.getenv('JIRA_TOKEN')),headers=headers, params=expand)
27+
#for a confluence page, get the incoming pages
28+
def print_incoming(page_id):
29+
payload = {'pageId' : page_id}
30+
resp = requests.get(info_url, auth=(os.getenv('JIRA_USER'), os.getenv('JIRA_TOKEN')), params=payload )
31+
mypage = BeautifulSoup(resp.text, 'lxml')
32+
try:
33+
for div in mypage.find_all('div'):
34+
if "class" in div.attrs.keys():
35+
if div.attrs["class"][0] == "basicPanelContainer":
36+
if "Incoming Links" in div.text:
37+
print("Incoming Links")
38+
for descendant in div.descendants:
39+
#print(descendant)
40+
if descendant.name == "a":
41+
#print("found an anchor")
42+
if "data-linked-resource-id" in descendant.attrs.keys():
43+
print(base_url+descendant.attrs["href"])
44+
except:
45+
print("Broke looking for anchors")
46+
print("\n\n")
47+
48+
49+
50+
#print(children.status_code)
51+
#print(json.dumps(json.loads(children.text), sort_keys=True, indent=4, separators=(",", ": ")))
52+
def print_pages(result):
53+
print("Title: %s\n"%(result["title"]))
54+
#print("ID: %s"%(result["id"])
55+
print("URL: %s/wiki%s\n"%(base_url,result["_links"]["webui"]))
56+
print_incoming(result["id"])
57+
print('\n\n')
58+
if "descendants" in result.keys():
59+
for descendant in result["descendants"]["page"]["results"]:
60+
print_pages(descendant)
61+
62+
63+
64+
children_json = json.loads(children.text)
65+
for result in children_json["results"]:
66+
print_pages(result)
67+
68+
69+
## Grab a page in confluence and get info by page id
70+

0 commit comments

Comments
 (0)