Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c31093f

Browse files
committed
latexbot
1 parent e7b4d62 commit c31093f

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"emailbot=codebots.cli:emailbot",
4343
"sshbot=codebots.cli:sshbot",
4444
"deploybot=codebots.cli:deploybot",
45+
"latexbot=codebots.cli:latexbot",
4546
],
4647
},
4748
install_requires=requirements,

src/codebots/bots/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
from .sshbot import *
44
from .telebot import *
55
from .deploybot import *
6+
from .latexbot import *
7+
from .drivebot import *

src/codebots/bots/drivebot.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import authentication
2+
from googleapiclient.discovery import build
3+
from googleapiclient.http import MediaFileUpload
4+
import os
5+
6+
7+
from ._bot import BaseBot
8+
9+
__all__ = [
10+
'DriveBot'
11+
]
12+
13+
14+
class DriveBot(BaseBot):
15+
"""Bot to help with handling your Google Drive.
16+
17+
18+
"""
19+
20+
def __init__(self, config_file) -> None:
21+
super().__init__(config_file)
22+
23+
24+
def docxDriveUpload(service):
25+
26+
file_metadata = {'name': 'otuput.docx'}
27+
media = MediaFileUpload(
28+
'output.docx', mimetype='application/vnd.openxmlformats-officedocument.wordprocessingml.document')
29+
30+
file = service.files().create(body=file_metadata, media_body=media, fields='id').execute()
31+
32+
print('File upload successful! File ID: %s' % file.get('id'))
33+
34+
35+
if __name__ == '__main__':
36+
docxDriveUpload()

src/codebots/bots/latexbot.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import os
2+
import subprocess
3+
import tempfile
4+
from pathlib import Path
5+
6+
7+
from ._bot import BaseBot
8+
9+
__all__ = [
10+
'LatexBot',
11+
]
12+
13+
14+
class LatexBot(BaseBot):
15+
"""LatexBot.
16+
"""
17+
18+
def __init__(self, config_file=None, sender=None) -> None:
19+
self.__name__ = "emailbot"
20+
21+
def _clone_overleaf(self, document_code):
22+
"""Temporarily clone the overleaf project on the local machine.
23+
24+
Parameters
25+
----------
26+
document_code : str
27+
code of the overleaf project. you can find in the address bar in your
28+
broweser when you open the overleaf project.
29+
30+
Returns
31+
-------
32+
TemporaryDirctory object
33+
the temporary directory where the project has been cloned. This can
34+
be used later to be cleaned up using `temp_dir.cleanup()`.
35+
36+
Raises
37+
------
38+
Exception
39+
if this exception is raised, the cloning process has been aborted.
40+
Check if the `document_code` is correct and that you have `git`
41+
installed on your machine.
42+
"""
43+
temp_dir = tempfile.TemporaryDirectory()
44+
out = subprocess.run(["git", "clone", f"https://git.overleaf.com/{document_code}"], cwd=temp_dir.name)
45+
if out.returncode == 0:
46+
print(f"project temporary saved in {temp_dir.name}")
47+
else:
48+
raise Exception
49+
# temp_dir.cleanup()
50+
return temp_dir
51+
52+
def convert_tex_to_docx(self, input_path, output_path=None):
53+
"""convert all the .tex files in a folder into .docx files. If no `output_path`
54+
is provided, the .docx files will be saved in the same directory as the .tex
55+
files.
56+
57+
Parameters
58+
----------
59+
input_path : str
60+
path to the folder containing the .tex file(s)
61+
output_path : str, optional
62+
path to the output .docx file(s), by default None
63+
"""
64+
pathlist = Path(input_path).rglob('*.tex')
65+
for file in pathlist:
66+
output = str(file).split('.tex')[
67+
0]+'.docx' if not output_path else Path().joinpath(output_path, str(file.name).split('.tex')[0]+'.docx')
68+
out = subprocess.run(["pandoc", "-o", output, "-t", "docx", file])
69+
print("The exit code was: %d" % out.returncode)
70+
71+
def convert_overleaf_to_docx(self, document_code, output_path):
72+
"""convert the overleaf project into a .docx file. Any .tex file in the
73+
overleaf repository will be converted into a .docx file with the same name.
74+
75+
Parameters
76+
----------
77+
document_code : str
78+
code of the overleaf project. you can find in the address bar in your
79+
broweser when you open the overleaf project.
80+
output_path : str
81+
path to the output .docx file
82+
"""
83+
temp_dir = self._clone_overleaf(document_code)
84+
self.convert_tex_to_docx(Path().joinpath(temp_dir.name, document_code), output_path)
85+
print(f"project saved in {output_path}")
86+
temp_dir.cleanup()
87+
print("temporary clone removed")

src/codebots/cli/cli.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
slackbot --help
1111
sshbot --help
1212
deploybot --help
13+
latexbot --help
1314
1415
1516
"""
@@ -23,6 +24,7 @@
2324
from codebots.bots import EmailBot
2425
from codebots.bots import sshBot
2526
from codebots.bots.deploybot import DeployBot
27+
from codebots.bots.latexbot import LatexBot
2628
from codebots.utilities.ssh import gen_keypair, add_pubkey_to_server
2729
from codebots.utilities.tokens import add_token, get_telegram_chatid, set_token_dir, reset_token_dir
2830
from codebots.utilities.deploy import configure_local, configure_server
@@ -352,6 +354,49 @@ def configure_remote(project, branch, sshbot):
352354
out = configure_server(bot.server_repo_path, branch, sshbot)
353355
click.echo(out)
354356

357+
# ------------------------------- LATEX ----------------------------------#
358+
359+
360+
@click.group()
361+
def latexbot():
362+
"""bot to help with latex documents"""
363+
pass
364+
365+
366+
@latexbot.command()
367+
@click.option('--input', default='None', help='path to the folder containing the .tex files')
368+
@click.option('--output', default='None', help='path to the folder where the .docx files will be saved')
369+
def convert_tex_to_docx(input, output):
370+
"""Convert the .tex files in a folder to .docx.\n
371+
372+
Parameters\n
373+
----------\n
374+
input : str\n
375+
path to the folder containing the .tex files.\n
376+
"""
377+
bot = LatexBot()
378+
if input == 'None':
379+
input = os.getcwd()
380+
if output == 'None':
381+
output = None
382+
out = bot.convert_tex_to_docx(input, output)
383+
click.echo(out)
384+
385+
386+
@latexbot.command()
387+
# @click.argument('type')
388+
def convert_overleaf_to_docx():
389+
"""Convert an overleaf project to a .docx file.\n
390+
391+
Parameters\n
392+
----------\n
393+
project : str\n
394+
name of the project for the setting file.\n
395+
"""
396+
bot = LatexBot()
397+
out = bot.convert_overleaf_to_docx("5c5b20af636606525ca0dbc0", 'C:/temp')
398+
click.echo(out)
399+
355400

356401
# -------------------------------- DEBUG ----------------------------------#
357402
if __name__ == "__main__":

src/codebots/utilities/latex.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import requests
2+
3+
4+
def download():
5+
url = 'https://www.facebook.com/favicon.ico'
6+
r = requests.get(url, allow_redirects=True)
7+
8+
open('facebook.ico', 'wb').write(r.content)

0 commit comments

Comments
 (0)