Showing posts with label productivity. Show all posts
Showing posts with label productivity. Show all posts

Sunday, November 20, 2022

Download files from google drive with python - no API key

There are several scripts to download files from google drive using a shareable link. Some require
authentication others don't work. Here we're interested in those that do not require authentication.

The gdrivedl.py script provided by ndrplz just works fine.

> gdrivedl.py file_id

Another notable, currently non-working, alternative is googledrivedownloader. It probably worked at some point in time but today it fails. There's however a simple fix proposed in this thread. The code is here: 

import requests
 
def download_file_from_google_drive(id, destination):
    URL = "https://docs.google.com/uc?export=download"
 
    session = requests.Session()
 
    response = session.get(URL, params={"id": id, "confirm": 1}, stream=True)
    token = get_confirm_token(response)
 
    if token:
        params = {"id": id, "confirm": token}
        response = session.get(URL, params=params, stream=True)
 
    save_response_content(response, destination)
 
def get_confirm_token(response):
    for key, value in response.cookies.items():
        if key.startswith("download_warning"):
            return value
    return None
 
def save_response_content(response, destination):
    CHUNK_SIZE = 32768
 
    with open(destination, "wb") as f:
        for chunk in response.iter_content(CHUNK_SIZE):
            if chunk:  # filter out keep-alive new chunks
                f.write(chunk)
 
if __name__ == "__main__":
    file_id = "TAKE ID FROM SHAREABLE LINK"
    destination = "DESTINATION FILE ON YOUR DISK"
    download_file_from_google_drive(file_id, destination)

Thursday, September 2, 2021

Conditional formatting LaTeX tables depending on cell values

A package for generating conditional formatted LaTeX tables that extends the one proposed in a post by Siavoosh Payandeh Azad. I've just extended it with the command \gradientd for a divergent colormap. There are also several other solutions discussed in this stackexchange post. The example can be seen in Overleaf here.


The code for generating the table

\documentclass[table]{article}
\usepackage{highlight}

\begin{document}

% For a 2 color palette
% \gradientcell{cell_val}{min_val}{max_val}{colorlow}{colorhigh}{opacity} 

% For a 3 color/divergent palette
% \gradientcelld{cell_val}{min_val}{mid_val}{max_val}{colorlow}{colormid}{colorhigh}{opacity}

% Configure a shorthand macro
\newcommand{\g}[1]{\gradientcelld{#1}{-3}{0}{3}{red}{white}{green}{70}}

\begin{tabular}{l|ccccc}
{$\sigma$} &  40  &   80  & 100  &   200 &  800 \\
\hline
0  & \g{ 0.87} & \g{ 0.84} & \g{ 1.04} & \g{ 1.34} & \g{-4.30} \\
5  & \g{ 0.38} & \g{ 0.22} & \g{ 0.33} & \g{ 0.58} & \g{-2.18} \\
10 & \g{ 0.29} & \g{ 0.04} & \g{ 0.11} & \g{ 0.48} & \g{-0.67} \\
25 & \g{-0.05} & \g{-0.34} & \g{-0.39} & \g{-0.48} & \g{-0.53} \\
50 & \g{ 0.04} & \g{-0.18} & \g{-0.13} & \g{-0.08} & \g{ 0.11} \\
\end{tabular}

\end{document}

The package highlight.sty  implement these commands  

%% Siavoosh Payandeh Azad Jan. 2019
%% modified by Gabriele Facciolo Sep. 2021
\ProvidesPackage{highlight}[Cell background highlighting based on user data]
\RequirePackage{etoolbox}
\RequirePackage{pgf} % for calculating the values for gradient
\RequirePackage{xcolor} % enables the use of cellcolor make sure you have [table] option in the document class 

%======================================
% For a 2 color palette
% \gradientcell{cell_val}{min_val}{max_val}{colorlow}{colorhigh}{opacity} 
\newcommand{\gradientcell}[6]{
    % The values are calculated linearly between \midval and \maxval
    \ifdimcomp{#1pt}{>}{#3 pt}{\cellcolor{#5!100.0!#4!#6}#1}{
    \ifdimcomp{#1pt}{<}{#2 pt}{\cellcolor{#5!0.0!#4!#6}#1}{
         \pgfmathparse{int(round(100*(#1/(#3-#2))-(#2 *(100/(#3-#2)))))}
        \xdef\tempa{\pgfmathresult}
        \cellcolor{#5!\tempa!#4!#6}#1
    }}
 }
%======================================

%======================================
% For a 3 color/divergent palette % \gradientcelld{cell_val}{min_val}{mid_val}{max_val}{colorlow}{colormid}{colorhigh}{opacity} \newcommand{\gradientcelld}[8]{ \xdef\lowvalx{#2}% \xdef\midvalx{#3}% \xdef\maxvalx{#4}% \xdef\lowcolx{#5}% \xdef\midcolx{#6}% \xdef\highcolx{#7}% \xdef\opacityx{#8}% % The values are calculated linearly between \midval and \maxval \ifdimcomp{#1pt}{>}{\maxvalx pt}{\cellcolor{\highcolx!100.0!\midcolx!\opacityx}#1}{ \ifdimcomp{#1pt}{<}{\midvalx pt}{% \ifdimcomp{#1pt}{<}{\lowvalx pt}{\cellcolor{\midcolx!0.0!\lowcolx!\opacityx}#1}{ \pgfmathparse{int(round(100*(#1/(\midvalx-\lowvalx))-(\lowvalx*(100/(\midvalx-\lowvalx)))))}% \xdef\tempa{\pgfmathresult}% \cellcolor{\midcolx!\tempa!\lowcolx!\opacityx}#1% }}{ \pgfmathparse{int(round(100*(#1/(\maxvalx-\midvalx))-(\midvalx*(100/(\maxvalx-\midvalx)))))} \xdef\tempb{\pgfmathresult}% \cellcolor{\highcolx!\tempb!\midcolx!\opacityx}#1% }} }
%======================================

Wednesday, April 19, 2017

Stop TeXShop memory leak

Long TeXShop writing sessions (several weeks in my case) reveal that TeXShop leaks memory. I ended up with a 5Gb process without any open file!
This seems to be an old incomprehensible issue but it has a simple solution, just run the following line in your terminal

   defaults write TeXShop ReleaseDocumentClasses 0

If you want to check the status of the variable before changing it:

   defaults read TeXShop ReleaseDocumentClasses

Source: https://sourceforge.net/p/texshop/bugs/87/?SetFreedomCookie

Sunday, March 12, 2017

Fixing textext 0.4.4 in Inkscape 0.91 with OSX

textext is a great Inkscape extension that allows to incrustante LaTeX formulas in the drawings. However, it seems that textext was last updated in 2010 (v0.4.4) and since then some python calls to the md5 package have become deprecated.
This is obviously an issue for recent OS. 
Traceback (most recent call last):
  File "textext.py", line 210, in cb_ok
    self.callback(self.text, self.preamble_file, self.scale_factor)
  File "textext.py", line 369, in
    converter_cls, old_node))
  File "textext.py", line 387, in do_convert
    new_node = converter.convert(text, preamble_file, scale_factor)
  File "textext.py", line 879, in convert
    return PdfConverterBase.convert(self, *a, **kw)
  File "textext.py", line 750, in convert
    self.tex_to_pdf(latex_text, preamble_file)
  File "textext.py", line 727, in tex_to_pdf
    exec_command(['pdflatex', self.tmp('tex')] + latexOpts)
  File "textext.py", line 592, in exec_command
    raise RuntimeError("Command %s failed: %s" % (' '.join(cmd), e))
RuntimeError: Command pdflatex /var/folders/2y/h0000gn/T/tmpUcu75l/tmp.tex -interaction=nonstopmode -halt-on-error failed: [Errno 2] No such file or directory

Sunday, October 14, 2012

Download/backup BibTeX library from CiteULike, including all the attached PDF

CiteULike is a convenient solution for bibliographic management, but sometimes an off-line replica of the bibliography is also needed. This can be achieved by exporting the CiteULike library in BibTeX format, furthermore BibDesk (and probably other managers too) can be synchronized directly with the CiteULike server.

While it is easy to export a .bib file from CiteULike, downloading all the attached files and having the .bib linking to the downloaded files is a different story.

BibTeX and JSON exports of the CiteULike library can be retrieved from:
  http://www.citeulike.org/json/user/USERNAME
  http://citeulike.org/bibtex/user/USERNAME
The BibTeX export can be read directly into a bibliographic manager, but the JSON export contains more information than the .bib, in particular it contains the location of the attached PDFs. So the idea of the Python script linked below is to do the following
  1. download the CiteULike library in BibTeX and JSON formats
  2. parse the JSON export and download all the attachments
  3. modify the .bib file to include links to the downloaded copies of the attachments
the links in the .bib file should work for BibDesk and JabRef.

Before running the script:
  • setup CITEULIKE_USERNAME and CITEULIKE_PASSWORD variables in the script
  • verify you have wget and pybtex installed
Download the Python script:  citeulike_backup.zip

http://wiki.citeulike.org/index.php/Importing_and_Exporting#JSON


Gory details from http://wiki.citeulike.org/index.php/Importing_and_Exporting#JSON:
# save session cookies
> wget -O /dev/null --keep-session-cookies  --save-cookies cookies.txt --post-data="username=xxxx&password=yyyy&perm=1" http://www.citeulike.org/login.do
# download bibtex with private comments and download an attachment
> wget -O export.bib --load-cookies cookies.txt http://www.citeulike.org/bibtex/user/xxxx
> wget --load-cookies cookies.txt http://www.citeulike.org//pdf/user/xxxx/article/123456/891011/some_99_paper_123456.pdf