Skip to content

Commit

Permalink
Stvad#46 added the basic functionality to create and update a reposit…
Browse files Browse the repository at this point in the history
…ory by default instead of downloading the repository zip file
  • Loading branch information
evolverine committed Mar 20, 2020
1 parent 85fb7e2 commit b5a5f67
Showing 1 changed file with 37 additions and 5 deletions.
42 changes: 37 additions & 5 deletions crowd_anki/github/github_importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@
import zipfile
from urllib.error import HTTPError, URLError
from urllib.request import urlopen

import sys
from io import BytesIO
from pathlib import Path

import aqt.utils
from aqt import QInputDialog
from ..importer.anki_importer import AnkiJsonImporter
from ..utils import utils
from ..git.repo import Repo
from ..git.exc import GitCommandNotFound, InvalidGitRepositoryError

BRANCH_NAME = "master"
GITHUB_LINK = "https://github.com/{}/archive/" + BRANCH_NAME + ".zip"
GITHUB_ZIP_URL = "https://github.com/{}/archive/" + BRANCH_NAME + ".zip"
GITHUB_REPO_URL = "https://github.com/{}.git"


class GithubImporter(object):
Expand All @@ -34,9 +37,38 @@ def import_from_github(self):
if repo and ok:
self.download_and_import(repo)

def download_and_import(self, repo):
def download_and_import(self, github_repo):
try:
try:
self.download_and_import_git(github_repo)
except GitCommandNotFound: # Git not available. Use .zip archive instead
print('Error accessing the git executable.\n '
'Please make sure git is installed and available on your PATH.\n'
'Downgrading to zip archive download.')
self.download_and_import_zip(github_repo)

except Exception as error:
# aqt.utils.showWarning("Error while trying to get deck from Github: {}".format(error))
raise error

def download_and_import_git(self, github_repo):
repo = None
repo_parts = github_repo.split("/")
deck_base_name = repo_parts[-1]
repo_dir = Path(self.collection.media.dir()).joinpath("..", "CrowdAnkiGit", deck_base_name, "git")
try:
repo_dir.mkdir(parents=True, exist_ok=True)
repo = Repo(repo_dir)
repo.remote("origin").pull()
except InvalidGitRepositoryError: # Clone repository
github_url = GITHUB_REPO_URL.format(github_repo)
Repo.clone_from(github_url, repo_dir)

AnkiJsonImporter.import_deck_from_path(self.collection, repo_dir)

def download_and_import_zip(self, repo):
try:
response = urlopen(GITHUB_LINK.format(repo))
response = urlopen(GITHUB_ZIP_URL.format(repo))
response_sio = BytesIO(response.read())
with zipfile.ZipFile(response_sio) as repo_zip:
repo_zip.extractall(tempfile.tempdir)
Expand All @@ -52,4 +84,4 @@ def download_and_import(self, repo):

except (URLError, HTTPError, OSError) as error:
aqt.utils.showWarning("Error while trying to get deck from GitHub: {}".format(error))
raise
raise

0 comments on commit b5a5f67

Please sign in to comment.