-
Notifications
You must be signed in to change notification settings - Fork 0
/
ankiUpload.py
140 lines (111 loc) · 4.1 KB
/
ankiUpload.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# import upload so we can inherit the class
from upload import Upload
from search import Search
from sentence import Sentence
from ankiParse import ankiParse, strip_tags
import anki
from anki.cards import Card
from anki.facts import Fact
import os
# This class inherits upload and adds anki upload support.
class ankiUpload(Upload):
def __init__(self):
# Default variables that are only used in this class
self.search_terms = [] # Empy by default (empty = don't search)
self.search_tags = [] # Only use cards with these tags (empty = ignore)
self.sentence_field = {} # Dictionary containing which field to
# use as the sentence. Only fields in
# models in the dictionary will be used.
# In the form { modelname: fieldname }
self.tag_strip = [ # Tags to strip from tags obtained from cards.
'Media-Missing', # Remove anki specific tags that we don't want on the site.
'Leech',
'Extr',
]
# Call the usual __init__ function
Upload.__init__(self)
#Load
def load(self, filename):
# Load the anki deck and get all the sentences
deck = anki.DeckStorage.Deck(filename)
# Grab the sentences based on which field in each model is the
# sentence.
# Cycle through models of the deck
for model in deck.models:
# First check if we know which field to use in this model
if model.name in self.sentence_field:
# Grab all facts ids
fact_ids = deck.s.all("select id from facts where modelId = " + str(model.id))
# Get facts from the id's
facts = []
for fact_id in fact_ids:
facts.append(deck.s.query(Fact).get(fact_id[0]))
# Get the sentences from the facts, and media.
sentences = []
parser = ankiParse()
for fact in facts:
# The new sentence
newSentence = Sentence(sentence=fact[self.sentence_field[model.name]])
media = [] # Temperory media without full path
full_media = [] # Media with full path
# Loop through fields in fact searching for media
for field in model.fieldModels:
parser.feed(fact[field.name])
media.extend(parser.media)
parser.close()
# If this anki deck has a media directory add that to media path
if deck.mediaDir() != None:
# Add full path to media
del full_media[:]
for m in media:
#print os.path.join(deck.mediaDir(), m)
if deck.mediaDir():
path = deck.mediaDir()
else:
path = ""
full_media.append(os.path.join(path, m))
newSentence.media = full_media
# Sentence Language
newSentence.language = self.language
# Remove tags that we don't want (anki specific tags, etc)
tags = fact.tags
for tag in self.tag_strip:
tags = tags.strip(tag)
newSentence.tags = tags.split()
# Append the sentence
sentences.append(newSentence)
# Ok, now if there are search terms search, search the
# sentences.
if len(self.search_terms) > 0:
# Make a searcher
searcher = Search()
searcher.search_terms = self.search_terms
# Put the sentences into the searcher
for sentence in sentences:
searcher.raw_sentences.append(sentence.sentence)
# Search
searcher.search()
# Now update sentences to contain the results
for sentence in sentences:
if sentence.sentence not in searcher.sentences:
sentences.remove(sentence)
#get rid of all sentences that don't have these tags
if len(self.search_tags) > 0:
for sentence in sentences[:]:
removed = False
for tag in self.search_tags:
if tag not in sentence.tags and not removed:
sentences.remove(sentence)
removed = True
# Before we add these sentences, we need to strip any
# xml / html from them
i = 0
for sentence in sentences:
print sentence.media
sentences[i].sentence = strip_tags(sentence.sentence)
i += 1
# Now add these to the
self.sentences.extend(sentences)
# We should be done now, now everything is up to the base
# upload function.
deck.close()