-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
105 lines (89 loc) · 2.88 KB
/
main.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/python3
import sys
import json
import os
import logging
import itertools
from logging.handlers import RotatingFileHandler
from pinboard import pinboard
from trello import TrelloClient
CONFIG_FILE_NAME = os.path.join(
os.path.dirname(os.path.realpath(__file__)), "config.json"
)
LOG_FILE_NAME = os.path.join(os.path.dirname(os.path.realpath(__file__)), "log.txt")
AUTH_DATA_KEY = "authentication"
# Enable logging
file_handler = RotatingFileHandler(LOG_FILE_NAME, maxBytes=(1048576 * 5), backupCount=7)
console_handler = logging.StreamHandler()
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
handlers=[file_handler, console_handler],
)
logger = logging.getLogger(__name__)
# Reading configuration and authentication data
with open(CONFIG_FILE_NAME, "r") as conf_file:
conf_data = json.load(conf_file)
auth_data = conf_data[AUTH_DATA_KEY]
# Logging in to Pinboard
pb = pinboard.Pinboard(conf_data[AUTH_DATA_KEY]["pinboard_api_token"])
logger.info("Logged in to Pinboard")
# Logging in to Trello
trello_client = TrelloClient(
api_key=conf_data[AUTH_DATA_KEY]["trello_api_key"],
token=conf_data[AUTH_DATA_KEY]["trello_token"],
)
logger.info("Logged in to Trello")
trello_list = trello_client.get_list(conf_data["trello_list_id"])
trello_board = trello_list.board
trello_labels = {}
for label in trello_board.get_labels():
label.fetch()
trello_labels[label.name] = label
logging.debug("Labels: %s", trello_labels)
trello_label_color_generator = itertools.cycle(
[
"green",
"yellow",
"orange",
"red",
"purple",
"blue",
"sky",
"lime",
"pink",
"black",
]
)
pb_tag = conf_data["pinboard_tag"]
new_pb_items = pb.posts.all(tag=[pb_tag])
logger.info("Fetched new Pinboard items")
if len(new_pb_items) == 0:
logger.info("No new items.")
sys.exit(0)
for pb_item in new_pb_items:
logger.debug(
f"""Found item with
url: {pb_item.url},
description: {pb_item.description},
extended: {pb_item.extended},
tags: {pb_item.tags},
time: {pb_item.time},
"""
)
card = trello_list.add_card(name=pb_item.description, desc=pb_item.extended)
logger.info(f"Created card '{pb_item.description}')")
card.attach(url=pb_item.url)
logger.info(f"Attached link {pb_item.url} to item")
for tag in pb_item.tags:
# ignore marker tag
if pb_tag and tag == pb_tag:
continue
# create new label
if tag not in trello_labels:
label_color = next(trello_label_color_generator)
label = trello_board.add_label(tag, label_color)
trello_labels[tag] = label
card.add_label(trello_labels[tag])
pb.posts.delete(url=pb_item.url)
logger.info("Deleted Pinboard item %s", pb_item.url)