fixing merge

This commit is contained in:
Andrew Davidson 2020-04-01 10:21:03 -04:00
commit 0fb9e3246a
Signed by: amd
GPG key ID: 17AF8F2A49CF25C6
5 changed files with 36 additions and 56 deletions

View file

@ -1,24 +1,32 @@
import logging, sys
from archivist.lib import Config
import archivist.pinboard as pinboard
import archivist.github as github
log = logging.getLogger(__name__)
c = Config()
c.init()
class Archivist():
def run(self, command, source):
if command == "backup":
if source == "github":
log.info("Backing up Github")
github.backup_github()
github.backup_github(c.c["github_user"], c.backupdir())
elif source == "pinboard":
log.info("Backing up Pinboard")
pinboard.backup_pinboard()
pinboard.backup_pinboard(c.c["pinboard_user"], c.c["pinboard_token"], c.backupdir())
elif source == "all":
log.info("Backing up all sources")
github.backup_github()
pinboard.backup_pinboard()
if c.c["github_enabled"]:
log.info("Backing up Github")
github.backup_github(c.c["github_user"], c.backupdir())
if c.c["pinboard_enabled"]:
log.info("Backing up Pinboard")
pinboard.backup_pinboard(c.c["pinboard_user"], c.c["pinboard_token"], c.backupdir())
else:
log.error("Source %s not implemented" % source)

View file

@ -2,11 +2,9 @@ import requests, json, logging, os
from pathlib import Path
import pygit2
user = "amdavidson"
backupdir = Path.home() / "backups/github"
log = logging.getLogger(__name__)
def backup_gh_repos():
def backup_gh_repos(user, backupdir):
response = requests.get("https://api.github.com/users/"+user+"/repos")
repos = json.loads(response.text)
@ -22,7 +20,7 @@ def backup_gh_repos():
log.info("Fetching updates...")
localrepo = pygit2.Repository(localrepopath).remotes["origin"].fetch()
def backup_gh_gists():
def backup_gh_gists(user, backupdir):
response = requests.get("https://api.github.com/users/"+user+"/gists")
gists = json.loads(response.text)
@ -38,9 +36,11 @@ def backup_gh_gists():
log.info("Fetching updates...")
pygit2.Repository(localgistpath).remotes["origin"].fetch()
def backup_github():
backup_gh_repos()
backup_gh_gists()
def get_github_dir(backupdir):
github_dir = backupdir / "github"
return github_dir
if __name__ == '__main__':
backup_github()
def backup_github(user, backupdir):
github_dir = get_github_dir(backupdir)
backup_gh_repos(user, github_dir)
backup_gh_gists(user, github_dir)

View file

@ -4,13 +4,6 @@ from archivist.lib import Config
log = logging.getLogger(__name__)
backupdir = Path.home() / "backups/fastmail"
config = Config()
c = config.init()
def imap_connect(c):
server = imaplib.IMAP4_SSL(c['imap_server'], c['imap_port'])
server.login(c['imap_user'], c['imap_password'])

View file

@ -2,35 +2,23 @@ import yaml
import sys
from pathlib import Path
class Log():
LOGLEVEL = 3
DEST = "stdout"
def __call__(self, logstring, loglevel=None):
if loglevel == "error":
loglevel = 3
elif loglevel == "warning":
loglevel = 2
else:
loglevel = 1
if loglevel >= self.LOGLEVEL:
if self.DEST == "stderr":
print(logstring, file=sys.stderr)
else:
print(logstring)
class Config():
CONFIGFILE = Path.home() / ".archivist.yml"
c = {}
def init(self):
with open(self.CONFIGFILE) as stream:
try:
return yaml.full_load(stream)
config_dict = yaml.full_load(stream)
for key, value in config_dict.items():
self.c[key] = value
except yaml.YAMLError as exc:
print(exc)
def backupdir(self):
return Path(self.c["backup_folder"])

View file

@ -1,16 +1,10 @@
import requests, json, datetime, os, time, glob, logging
from pathlib import Path
from archivist.lib import Config
log = logging.getLogger(__name__)
backupdir = Path.home() / "backups/pinboard"
config = Config()
c = config.init()
def get_last_backup():
backups = backupdir.glob("*.json")
def get_last_backup(backups_folder):
backups = backups_folder.glob("*.json")
oldest = datetime.datetime.fromtimestamp(0)
for b in backups:
bdate = datetime.datetime.fromtimestamp(float(b.stem))
@ -19,22 +13,19 @@ def get_last_backup():
return oldest
def backup_pinboard():
response = requests.get("https://api.pinboard.in/v1/posts/update?format=json&auth_token="+c["pinboard_user"]+":"+c["pinboard_token"])
def backup_pinboard(user, token, backups_folder):
response = requests.get("https://api.pinboard.in/v1/posts/update?format=json&auth_token="+user+":"+token)
apiupdated = datetime.datetime.strptime(json.loads(response.text)["update_time"], "%Y-%m-%dT%H:%M:%SZ")
if apiupdated > get_last_backup() :
if apiupdated > get_last_backup(backups_folder) :
log.info("New bookmarks added, pulling latest backup...")
response = requests.get("https://api.pinboard.in/v1/posts/all?format=json&auth_token="+c["pinboard_user"]+":"+c["pinboard_token"])
response = requests.get("https://api.pinboard.in/v1/posts/all?format=json&auth_token="+user+":"+token)
backupfile = str(time.time()) + ".json"
backuppath = backupdir / backupfile
backupdir.mkdir(parents=True, exist_ok=True)
backuppath = backups_folder / backupfile
backups_folder.mkdir(parents=True, exist_ok=True)
with open(backuppath, "w+") as f:
f.write(response.text)
else:
log.info("No new bookmarks since last backup.")
if __name__ == '__main__':
backup_pinboard()