-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgit.py
265 lines (225 loc) · 10.6 KB
/
git.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
from github import Github, InputGitAuthor
from pprint import pprint
#import json
import config
import utils
import AgeEncHandler
from dictionaries import git_messages
import flashcards
GitHubToken = config.GitHubToken
GitHubFullRepo = config.GitHubUser + "/" + config.GitHubRepo
GitHubBranch = config.GitHubBranch
BotName = config.BotName
# TODOCommand = config.TODOCommand
assetsFolder = config.getAssetsFolder()
g = Github(GitHubToken)
repo = g.get_repo(GitHubFullRepo)
def push(path, message, content, branch, update=False):
author = InputGitAuthor(
config.GitHubAuthor,
config.GitHubEmail
)
#source = repo.get_Branch(Branch)
#repo.create_git_ref(ref=f"refs/heads/{Branch}", sha=source.commit.sha) # Create new Branch from master
if update: # If file already exists, update it
#pass
contents = repo.get_contents(path, ref=branch) # Retrieve old file to get its SHA and path
repo.update_file(contents.path, message, content, contents.sha, branch=branch, author=author) # Add, commit and push Branch
else: # If file doesn't exist, create it
#pass
repo.create_file(path, message, content, branch=branch, author=author) # Add, commit and push Branch
def updateJournal(entry, needsBuilding=True, path=None, overwrite=False, alias='', ignoreURL=False, isJournalFile=True):
if path == None:
path = utils.getJournalPath()
if needsBuilding:
entry = buildJournalEntry(entry, ignoreURL)
if(GitFileExists(path)):
file = repo.get_contents(path, ref=GitHubBranch) # Get file from Branch
if(overwrite):
data = "---\ntitle: " + utils.getPageTitle(path) + "\nalias: " + alias + "\n---\n\n"
else:
data = file.decoded_content.decode("utf-8") # Get raw string data
if(config.isGraphAgeEncrypted()):
if AgeEncHandler.isAgeEncrypted(data) == 1: # encrypted but without \n requires transformation
data = (AgeEncHandler.ageDecrypt(AgeEncHandler.convertToAgeString(data)))
elif AgeEncHandler.isAgeEncrypted(data) == 2: # encrypted no transformation required
data = (AgeEncHandler.ageDecrypt(data))
data += (entry).strip() + "\n"
if(config.isGraphAgeEncrypted()):
data = AgeEncHandler.ageEncrypt(data)
push(path, git_messages['COMMIT_MESSAGE'].format(BotName, utils.getTimestamp()) , data, GitHubBranch, update=True)
else:
if isJournalFile:
journalTemplate = utils.getJournalTemplate()
if journalTemplate:
data = "---\ntitle: " + utils.getJournalTitle() + "\n---\n\n" + journalTemplate + (entry).strip() + "\n"
else:
data = "---\ntitle: " + utils.getJournalTitle() + "\n---\n\n" + (entry).strip() + "\n"
else:
data = "---\ntitle: " + utils.getPageTitle(path) + "\nalias: " + alias + "\n---\n\n" + (entry).strip() + "\n"
if(config.isGraphAgeEncrypted()):
data = AgeEncHandler.ageEncrypt(data)
push(path, git_messages['COMMIT_MESSAGE'].format(BotName, utils.getTimestamp()) , data, GitHubBranch, update=False)
def GitFileExists(path):
try:
repo.get_contents(path, ref=GitHubBranch) # Get file from Branch
return True
except Exception as e:
if (e.args[0] == 404):
print (e.args[0])
return False
def buildJournalEntry(entry, ignoreURL):
journalEntry = ""
currentTime = utils.getCurrentTime()
if currentTime:
currentTime += " "
else:
currentTime = ""
# print(processCommandsMapping('21:40 some non todo entry T'))
journalEntry = config.defaultIndentLevel + " " + utils.processCommandsMapping(currentTime + entry)
# if(TODOCommand in entry):
# journalEntry = config.defaultIndentLevel + " TODO " + currentTime + entry.replace(TODOCommand,'')
# else:
# journalEntry = config.defaultIndentLevel + " " + currentTime + entry
if(not(ignoreURL)):
# print(entry)
journalEntryURL = utils.containsYTURL(entry)
# print (journalEntryURL)
if(journalEntryURL):
#title = getWebPageTitle(journalEntryURL)
journalEntry = journalEntry.replace(journalEntryURL, '{{youtube ' + journalEntryURL +'}}')
else:
journalEntryURL = utils.containsTWUrl(entry)
if(journalEntryURL):
journalEntry = utils.generateTwitterIframe(journalEntryURL)
else:
journalEntryURL = utils.containsURL(entry)
if(journalEntryURL):
title = utils.getWebPageTitle(journalEntryURL)
if(config.journalsFilesExtension == '.md'):
journalEntry = journalEntry.replace(journalEntryURL, '#' + config.BookmarkTag + ' [' + title + '](' + journalEntryURL + ')')
elif(config.journalsFilesExtension == '.org'):
journalEntry = journalEntry.replace(journalEntryURL, '#' + config.BookmarkTag + ' [[' + journalEntryURL + '][' + title + ']]')
print (journalEntry)
return journalEntry
def updateAsset(data, fileType):
print('u')
path = assetsFolder + "/" + utils.getTimestamp(True,True) + "." + fileType
print(config.getAssetsDestination())
if(config.getAssetsDestination() == 'github'):
update = False
if(GitFileExists(path)):
update = True
push(path, git_messages['COMMIT_MESSAGE'].format(BotName, utils.getTimestamp()) , data, GitHubBranch, update=update)
path = ("![](./" + path + ")")
elif(config.getAssetsDestination() == 'firebase'):
path = ("![](" + utils.UploadToFirebase(data, path) + ")")
return path
def getGitFileContent(file, fetchContent = False):
if (fetchContent):
file = repo.get_contents(file, ref=GitHubBranch) # Get file from Branch
try:
content = file.decoded_content.decode("utf-8") # Get raw string data
if(config.isGraphAgeEncrypted()):
if AgeEncHandler.isAgeEncrypted(content) == 1: # encrypted but without \n requires transformation
return (AgeEncHandler.ageDecrypt(AgeEncHandler.convertToAgeString(content)))
elif AgeEncHandler.isAgeEncrypted(content) == 2: # encrypted no transformation required
return (AgeEncHandler.ageDecrypt(content))
else: # not encrypted
return content
else:
return content
except Exception as e:
print(e)
return None
def scanGit4Flashcards(path=""):
contents = repo.get_contents(path)
flashcardsList = []
#print (contents)
while contents:
content = contents.pop(0)
# print(content.url)
if '/assets/' not in content.url: #TODO change to assetsfolder
if content.type == "dir":
contents.extend(repo.get_contents(content.path))
else:
#pass
#file = content
flashcardsList += flashcards.scan4Flashcards( getGitFileContent(content) )
return(flashcardsList)
def updateFlashCards():
return flashcards.saveFlashcardsDB( scanGit4Flashcards() )
def Git2Json(path=""):
AllFilesContent = []
contents = repo.get_contents(path)
while contents:
content = contents.pop(0)
# print("fetching " + content.path)
if '/assets/' not in content.url:
if content.type == "dir":
contents.extend(repo.get_contents(content.path))
else:
gitFileContent = getGitFileContent(content)
if gitFileContent:
AllFilesContent.append(gitFileContent)
utils.saveasJson(AllFilesContent,"GitDump.json")
def updateCalendarsFile():
path = "pages/" + config.getcalendarFile()
contents = getGitFileContent(path, True)
contents = utils.generateCalendarsFile(contents)
if(config.isGraphAgeEncrypted()):
contents = AgeEncHandler.ageEncrypt(contents)
push(path, git_messages['COMMIT_MESSAGE'].format(BotName, utils.getTimestamp()) , contents, GitHubBranch, update=True)
def getAllThemes():
AllThemes = []
contents = repo.get_contents('/logseq')
while contents:
content = contents.pop(0)
if 'custom.css' in content.path:
if content.path != "logseq/custom.css":
entry = [content.path.replace('logseq/','').replace('.custom.css',''), content]
AllThemes.append(entry)
return(AllThemes)
def switchTheme(cssFile):
cssContent = getGitFileContent(cssFile)
push('logseq/custom.css', git_messages['COMMIT_MESSAGE'].format(BotName, utils.getTimestamp()) , cssContent, GitHubBranch, update=True)
# a = getAllThemes()
# print(a[0][1])
# switchTheme(a[0][1])
def encryptGraph():
contents = repo.get_contents("")
while contents:
content = contents.pop(0)
if '/assets/' not in content.url and '/logseq/' not in content.url:
if content.type == "dir":
contents.extend(repo.get_contents(content.path))
else:
gitFileContent = getGitFileContent(content)
if gitFileContent:
print("encrypting " + content.path)
try:
encContents = (AgeEncHandler.ageEncrypt(gitFileContent))
push(content.path, git_messages['COMMIT_MESSAGE'].format(BotName, utils.getTimestamp()) , encContents, GitHubBranch, update=True)
except:
print("***********" + content.path + "*******************")
# print(content.path)
print("*********** All Files Decrytped *******************")
config.setGraphAgeEncrypted('true')
def decryptGraph():
contents = repo.get_contents("")
while contents:
content = contents.pop(0)
if '/assets/' not in content.url and '/logseq/' not in content.url:
if content.type == "dir":
contents.extend(repo.get_contents(content.path))
else:
gitFileContent = getGitFileContent(content)
if gitFileContent:
print("decrypting " + content.path)
try:
push(content.path, git_messages['COMMIT_MESSAGE'].format(BotName, utils.getTimestamp()) , gitFileContent, GitHubBranch, update=True)
except:
print("***********" + content.path + "*******************")
# print(content.path)
print("*********** All Files Encrypted *******************")
config.setGraphAgeEncrypted('false')