Skip to content

Commit

Permalink
Merge pull request #48 from c-okelly/feature/#41-url-support
Browse files Browse the repository at this point in the history
Feature/#41 url support
  • Loading branch information
c-okelly committed Aug 28, 2019
2 parents 04671f9 + 6d7d61e commit f53cb2a
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 15 deletions.
7 changes: 2 additions & 5 deletions src/org_to_anki/ankiClasses/AnkiQuestion.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

class AnkiQuestion:


def __init__(self, question = None):
self.deckName = None
self.question = []
Expand Down Expand Up @@ -32,10 +31,8 @@ def addQuestion(self, question): # (str
def getQuestions(self):
return self.question

def addImage(self, fileName, filePath):
with open(filePath, "rb") as file:
print(filePath)
self._media.append(AnkiQuestionMedia("image", fileName, file.read()))
def addImage(self, fileName, fileData):
self._media.append(AnkiQuestionMedia("image", fileName, fileData))

def hasMedia(self):
return len(self._media) > 0
Expand Down
8 changes: 4 additions & 4 deletions src/org_to_anki/ankiConnectWrapper/AnkiPluginConnector.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
import sys
# sys.path.insert(0, "org_to_anki/anki-connect/AnkiConnect.py")
import os
# TODO => need to embbeded the AnkiConnectCode
# dirname = os.path.dirname(__file__)
# ankiConnectPath = os.path.join(dirname, "../anki-connect/AnkiConnect.py")
# sys.path.append(ankiConnectPath)

# TODO This was missing due to poor testing
import base64

from .. import config
from .AnkiBridge import AnkiBridge
Expand All @@ -18,6 +17,7 @@
from aqt.utils import showInfo
except:
pass

class AnkiPluginConnector:

def __init__(self):
Expand Down
22 changes: 17 additions & 5 deletions src/org_to_anki/org_parser/DeckBuilderUtils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from ..ankiClasses.AnkiDeck import AnkiDeck
from .ParserUtils import getImageFromUrl

import os

Expand All @@ -11,26 +12,37 @@ def parseAnswerLine(self, answerLine, filePath, currentDeck): # (str, str, AnkiD
if "[" in answerLine and "]" in answerLine:
# print(answerLine)
# print(filePath)
if "http://" in answerLine or "www." in answerLine:
raise Exception("Line could not be parsed: " + answerLine)

elif answerLine.count("[") == 1:
if "http" in answerLine or "www." in answerLine:
if "[image=" in answerLine:
print("Trying to get image using: " + answerLine)

# TODO names should make some sense
url = answerLine[7:-1]
imageData = getImageFromUrl(url)
currentDeck.addImage(url, imageData)
answerLine = '<img src="' + url + '" />'

# Get image from
elif answerLine.count("[") == 1 and answerLine.count("]") == 1:
relativeImagePath = answerLine.split("[")[1].split("]")[0]
fileName = os.path.basename(relativeImagePath)
baseDirectory = os.path.dirname(filePath)
imagePath = os.path.join(baseDirectory, relativeImagePath)

if len(relativeImagePath) > 0 and os.path.exists(imagePath) and os.path.isfile(imagePath):
with open(imagePath, "rb") as file:
data = file.read()
currentDeck.addImage(fileName, data)

currentDeck.addImage(fileName, imagePath)
answerLine = '<img src="' + os.path.basename(imagePath) + '" />'
else:
print("Could not find image on line:", answerLine)
else:
print("Could not parse image from line: " + answerLine)

return answerLine

def removeAsterisk(self, line): # (str)
if line.strip()[0] == "*":
line = line.strip().split(" ")[1:]
Expand Down
22 changes: 22 additions & 0 deletions src/org_to_anki/org_parser/ParserUtils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@

# Get Anki sync or urllib2
try:
from anki.sync import AnkiRequestsClient
anki = "set"
except:
anki = None
import requests


def getImageFromUrl(url):

if anki == "set":
client = AnkiRequestsClient()
client.timeout = URL_TIMEOUT
resp = client.get(url)
content = client.streamContent(resp)
else:
resp = requests.get(url)
content = resp.content

return content

def convertCommentsToParameters(comments): # ([str])

parameters = {}
Expand Down
3 changes: 2 additions & 1 deletion tests/testAnkiClasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def testAddImageForAnkiQuestion():
deck = AnkiDeck("Test deck")
question = AnkiQuestion("test question")
deck.addQuestion(question)
question.addImage("image.png", fullImagePath)
with open(fullImagePath, 'rb') as data:
question.addImage("image.png", data.read())

with open(fullImagePath, 'rb') as data:
mediaItem = AnkiQuestionMedia("image", "image.png", data.read())
Expand Down
8 changes: 8 additions & 0 deletions tests/testParseData.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,14 @@ def testCardsWithTagsData():

assert(actualDeck.getQuestions()[0].getTags() == ['a', 'b', 'c'])

def testCardWithImageUrl_CommandLineMode():

data = ["* Question", "** Image below", "** [image=https://lh3.googleusercontent.com/gdEMfGtrSRTvbTiXwysYJ_5XxqieWt0Z9vtFw0jQxOlbjo43_PJYa4kCusZjmkbe_euwGa4KAWEo2xJvEzHkwIpVN3H-XvCxVXCpQNOcH9_tERcVodYf75t18hYlargfKgYtHYvM]"]
actualDeck = parseData._buildDeck(data, "test.org")

assert(len(actualDeck.getQuestions()[0].getMedia()[0].data) == 92858)


def testClozeQuestionCreatedCorrectly():

# TODO
Expand Down
10 changes: 10 additions & 0 deletions tests/testPraserUtils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ def testCommaSeperatedDataForTags():

data = ParserUtils.convertLineToParameters("# tag=a, tag=b, tag=c")
assert(data == {'tag': 'a,b,c'})


def test_getImageFromUrl_CommandLineMode():

googleDocsImageUrl = "https://lh3.googleusercontent.com/gdEMfGtrSRTvbTiXwysYJ_5XxqieWt0Z9vtFw0jQxOlbjo43_PJYa4kCusZjmkbe_euwGa4KAWEo2xJvEzHkwIpVN3H-XvCxVXCpQNOcH9_tERcVodYf75t18hYlargfKgYtHYvM"

imageData = ParserUtils.getImageFromUrl(googleDocsImageUrl)

# Poor way to assert an image
assert(len(imageData) == 92858)

0 comments on commit f53cb2a

Please sign in to comment.