-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdescription_for_ebook.py
71 lines (64 loc) · 2.34 KB
/
description_for_ebook.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import csv
import json
import logging
import re
import requests
import yaml
from github import Github
from pathlib import Path
logging.basicConfig(
filename="description_added.log",
format="%(levelname)s: %(message)s",
level=logging.INFO
)
def notifier(msg):
logging.info(msg)
def get_title(meta):
if meta['source_metadata']['title'] != None:
title = meta['source_metadata']['title']
return title
else:
return None
def get_author(meta):
if meta['source_metadata']['authors'] != '':
authors = meta['source_metadata']['authors']
author = ''.join(authors)
return author
else:
return None
def get_bdrcid(meta):
if meta['source_metadata']['id'] != None:
bdrcid = meta['source_metadata']['id']
return bdrcid
else:
return None
def get_work_id(brdcid):
work_id = bdrcid[4:]
return work_id
if __name__=="__main__":
token = ""
g = Github(token)
headers = {"Authorization": f"bearer {token}"}
with open("catalog.csv", newline="") as csvfile:
pechas = list(csv.reader(csvfile, delimiter=","))
for pecha in pechas[4:9]:
pecha_id = re.search("\[.+\]", pecha[0])[0][1:-1]
repo = g.get_repo(f"Openpecha/{pecha_id}")
contents = repo.get_contents(f"./{pecha_id}.opf/meta.yml")
meta_content = contents.decoded_content.decode()
meta_content = yaml.safe_load(meta_content)
title = get_title(meta_content)
author = get_author(meta_content)
bdrcid = get_bdrcid(meta_content)
work_id = get_work_id(bdrcid)
if author != None:
data = {"description": f"{title} {author} Tsadra ID: {bdrcid}"}
else:
data = {"description": f"{title } Tsadra ID: {work_id}"}
response = requests.patch(f"https://api.github.com/repos/Openpecha/{pecha_id}", headers=headers, data=json.dumps(data))
if response.status_code == 200:
notifier(f"{pecha_id} Description added successfully")
print(f"{pecha_id} Description added successfully")
else :
notifier(f"{pecha_id} description not added due to status code {response.status_code}")
print(f"{pecha_id} description not added due to status code {response.status_code}")