U
    Aez                     @   s  d dl Zd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
mZmZ d dlZdejd< d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZmZ d d
lmZ d dlmZ dd Zdd Zdd Z dd Z!dd Z"dd Z#dd Z$dd Z%dd Z&dd Z'd d! Z(d"d# Z)d$d% Z*d&d' Z+dWd(d)Z,d*d+ Z-dXd,d-Z.d.d/ Z/d0d1 Z0dYd2d3Z1dZd4d5Z2d6d7 Z3d8d9 Z4d[d;d<Z5d=d> Z6d?d@ Z7d\dBdCZ8dDdE Z9dFdG Z:d]dIdJZ;dKdL Z<dMdN Z=dOdP Z>dQdR Z?d^dSdTZ@dUdV ZAdS )_    N)Markdowndisplayz$/home/nano/.pybliometrics/config.iniZPYB_CONFIG_FILE)AbstractRetrieval)	FreeProxy)Works)SequenceMatcher)get_templaterender_to_string)Context)HttpResponsec              
   C   s  ddl m}m} dd l}ddlm} | D ]}|jj|jd}|j	dddgdd	}|
  d
|_d|_d|_d|_d
|_d|_d|_d|_d|_d|_d|_|d}ddg}	| }
|	D ]}|
|| qtdd}|
| |  W 5 Q R X d|_|  tdd q(d S )Nr   )PdfFileReaderPdfFileWriterCVidZpdflatexzcv.texz-interaction=nonstopmodez"/var/www/nanotud/uploads/cv_files/)cwd4uploads/cv_files/auto_include/papers_auto_django.texzuploads/cv_files/cv.logzuploads/cv_files/cv.pdfzuploads/cv_files/cv.texz.uploads/cv_files/auto_include/conferences_autoz*uploads/cv_files/auto_include/patents_auto1uploads/cv_files/auto_include/papers_auto_sel.texz'uploads/cv_files/auto_include/phds_autoz(uploads/cv_files/auto_include/talks_autoz'uploads/cv_files/auto_include/rest_autoz(/var/www/nanotud/uploads/cv_files/cv.pdf   z./var/www/nanotud/uploads/cv_files/cv_short.pdfwbzuploads/cv_files/cv_short.pdfz/data/uploads/cv_files/cv.pdfz$/data/uploads/cv_files/cv_backup.pdf)ZPyPDF2r   r   
subprocessinternal.modelsr   objectsgetr   PopenwaitZpub_includelog_filepdf_filetex_filepublication_includeconferences_includepatents_includepapers_selected_includephds_includetalks_includerest_includeZaddPageZgetPageopenwriteclose	pdf_shortsaveshutilcopy2)querysetr   r   r   r   qsentryppdfpagesZ	pdfWriterZpage_numf r5   #/var/www/nanotud/modules_webpage.pypdflatex_cv   s:    
r7   c              	   C   s   ddl m} ddlm} d|jjddi}td|dd	}td
d}|| W 5 Q R X d|jjddi}td|dd	}tdd}|| W 5 Q R X | D ](}|jj	|j
d}d|_d|_|  qd S )Nr   Publicationr   Zpublication_list	CunibertiZauthor_list__icontainszpublication_export.textex)usingzE/var/www/nanotud/uploads/cv_files/auto_include/papers_auto_django.texwT)
cvselectedzpublication_export_selected.texzB/var/www/nanotud/uploads/cv_files/auto_include/papers_auto_sel.texr   r   r   )research.modelsr9   r   r   r   filterr	   r'   r(   r   r   r    r#   r+   )r.   r9   r   contextcontentZstatic_filer/   r0   r5   r5   r6   create_publication_includeA   s    rD   c                 C   s,   ddl m} |j }|D ]}|  qd S )Nr   Member)r   rF   r   rA   r+   )r.   rF   membersmemberr5   r5   r6   
set_slug_2Y   s
    

rI   c                 C   s,   ddl m} |j }|D ]}|  qd S Nr   r8   r@   r9   r   rA   r+   )r.   r9   pubspubr5   r5   r6   
set_slug_3`   s
    

rN   c                 C   s,   ddl m} |j }|D ]}|  qd S )Nr   )Event)external.modelsrO   r   rA   r+   )r.   rO   Zevsnr5   r5   r6   
set_slug_4g   s
    

rR   c                 C   s,   ddl m} |j }|D ]}|  qd S rJ   rK   )r.   r9   rL   Zsemr5   r5   r6   set_slug~   s
    

rS   c                 C   s  t | dkrv| D ]^}td|j d|j d t|j}tt|}t	 }|d D ]}|j
|d ddd	}q\|d |d
< ||d    }|d d|d< |jd kr|}n||d t|jk }tt | d t |dkr|jj|jd}d|_|   d S ddlm} t|j|jD ]F\}	}
t|	rDd }	nt|	}	||
|	|jdd d}|  q*qntd d S )Nr   zFind publications for z from z in Google ScholarpublicationsbibTFignore_indexsort
google_pubtitlestrpub_yearz publications foundr   r   z&No publication found in Google Scholar)Add_Publication,)rZ   yearauthorzPlease select only one entry)lenprintr`   r_   	scholarlysearch_authorfillnextpd	DataFrameappendisnullastyper[   r   r   r   commentr+   r@   r]   zipr\   rZ   isnaintsplit)r.   r/   search_queryr`   
pub_googlerM   r\   r]   r0   r_   rZ   Znew_pubr5   r5   r6   add_dois_scholar_info   s8    

rs   c                 C   s(  | d krd S d}| d dks\d| dd ks\d| d d ks\d| dd ks\d	| d d
 kr|  ddkr|| |  dd d   d }nD|  ddkr|| |  dd d   }n|| |  dd d   }n0d| dd  kr|| d |  d  }n||  }|dd |dd |dd |S )N r      ©	Copyright      c○   	COPYRIGHTThis journal is   Weinheim   
Switzerland   .r   z</jats:sub>z
<jats:sub>z!<jats:title>Abstract</jats:title>)findreplace)txtstringr5   r5   r6   clean_abstract   s     L r   c                 C   s0   d}| D ]"}t |dkr"|d7 }q||7 }q|S )Nrt   i   -)ord)Zinp_strnewc0r5   r5   r6   check_unicode_char   s    

r   c                 C   s$  ddl m}m} ddlm} | D ]}d}|jj|jd}t|j	
d}|d ksb|dksb|dkr|jd krtd t|j|j}|d kst|rd	|_|  d
}n||_	|  |jj|d }d\}	}
}}}}}}}}}|dkr|dkrzt }|	|}W n   t|d d }Y nX zt|d
d}W n   t|d d }Y nX |d krx|d krxd|_|  qtt||}|d krxt|j}	t|	}	tt||}
|j}|jd kr|j}t|}|jd kr|j}t|}z|j !dd 
 }W n(   z
|j"}W n   d }Y nX Y nX z|j#}|dd }W n   d }d }Y nX z
|j$}W n   d }Y nX |d kr|
d krt%||d}
|	d kr|d d }	|d krzt|d }W n   Y nX |d kr|d }|d krBz>t&|}|!dd  d|!dd  d|!dd  }W n   d }Y nX |d krfz|d }W n   Y nX |d krz|d }W n   Y nX |d krz|d d }W n   Y nX |d krz|d d }W n   Y nX |d krzt'|d d d d }W n   Y nX |d kr*|}|d kr*d }||d||	|
||||||||d
d!}|  |d krhd"|_n|d krzd#|_nd$|_d
|_(|  n:|d
krd%|_d|_(n"td& d'|_d
|_)d|_(|  |j* }|D ]D}|jjdd(j|j+d d) |j, d*}|D ]}|j-.| qqq d S )+Nr   )r9   r]   rE   Fr   rt   NoneZherezerozNo DOI found for titleT)Zdoi__iexact)NNNNNNNNNNNz not found in Crossref.)Zrefreshz8 not found in Scopus. API key and permission correct?(2)zODOI neither found in Scopus nor in Crossref. Please do check if DOI is correct.r      itemrZ   volumetyper   r~   r   pageissueshort-container-titlezcontainer-titlecreated
date-partsZXXXX)doiapprovedr_   rZ   author_listpub_dater   open_accessabstractr   journal_shortjournalpub_type	chair_pubzInformation taken from Scopus.z Information taken from Crossref.z+Information taken from Scopus and Crossref.zNo DOI found.zpublication already therezpDOI already in publication list. If you want to add it again, please delete the corresponding publication first.)r   . r;   )/r@   r9   r]   r   rF   r   r   r   r[   r   striprZ   rb   find_doi_for_titler`   rg   rn   rl   r+   rA   countr   r   r   get_abstracthtmlunescaper   format_authors_scopusr   sourcetitle_abbreviationZpublicationNameZ	pageRangerp   startingPage	coverDateZopenaccessFlagformat_authors_crossrefpub_date_crossro   addeddoi_in_pubsall
given_name	last_namepublicationadd)r.   r9   r]   rF   r/   Zkakar0   r   numrZ   authorsr   r   r_   r   r   Ztypeenumberr   r   worksitem_CRitem_scopusr   rM   ZiddirL   pir5   r5   r6   add_publication_info   s   
















6







      




&r   c           
   	   C   s  t | d}| }W 5 Q R X |ddd }|D ]t}|d}i }|D ]"}|dd}z|d d	kr`|d
kr`d	|dd kr`d|dd kr|dd  |d< nd|kr|jdddd  |d< nd|kr|jdddd  |d< nd|jddd}t|dkr&|jddd}t|dkrHtj||d  < n|d  ||d  < W qL   Y qLX qL|i kr4z|	j|dd}	W q4   t	j
|dgd}	Y q4X q4|	tj}	|	S )u*  
    Reads publication from database given by the format of the webpage.
    
    Parameters
    ----------
    file_name : name of the database file
        Structure: Two columns (1 = property, 2 = value )
                   Entries are separated by 'enditem
##################################'
            Example:
                '##########################################################################
                AG GC
                cvselected    no
                firstlast yes
                #filename
                cover ../pubs/reprints/covers/2020_Krok.jpg
                year 2020
                title        Modification of titanium implants using biofunctional nanodiamonds for enhanced antimicrobial properties
                authors Emilia Krok, Sascha Balakin, Jonas Jung, Frank Gross, J&ouml;rg Opitz, <b>G. Cuniberti</b>
                pubdate 2020.03.02
                type        regular
                reference    <i>Nanotechnology</i> <b>31</b>, 205603 (2020)
                DOI        10.1088/1361-6528/ab6d9b/
                abstract    The present study describes a novel antimicrobial surface using anodic oxidation of titanium and biofunctional
                detonation nanodiamonds (ND). ND have been loaded with antibiotics (amoxicillin or ampicillin) using poly
                (diallyldimethylammonium chloride)(PDDA). Successful conjugation with PDDA was determined by dynamic light scattering, …
                enditem
                ###########################################################################
                ...'

    Returns
    -------
    DataFrame
        Containing all publication entries from 'file_name'. Each row contains one publication and
        columns its values. If one property is not set in file_name, its value is set to 'np.NaN'.
    renditemr   r       	r~   #rt   r   r   r      journalfullname maxsplitarxivpasswdTrW   index)r'   readrp   r   r   ra   npNaNri   rg   rh   fillna)
	file_namefilerC   Zall_publicationsarticle
attributesdictioattrp   dfr5   r5   r6   read_publicationst  s>    $


*r   c                 C   s   |  d}i }|D ]}|dd}z|d dkr|dkrd|dd kr|j dd	d
}t|d	kr|j dd	d
}t|d	krtj||d  < q|d  ||d  < n|d  ||d  < W q   Y qX q|S )Nr   r   r   r~   r   rt   r      r   r   r   )rp   r   ra   r   r   r   )r0   r   r   r   rp   r5   r5   r6   entry_to_dict  s     
$r   c              	   C   s   t | d}| }W 5 Q R X |d}|D ]D}t|}|i kr,z|j|dd}W q,   tj|dgd}Y q,X q,|tj	}||d 
 |d 
 @  jj}|j|dd	 |S )
Nr   r   Tr   r   r   ZvornameZnachname)inplace)r'   r   rp   r   ri   rg   rh   r   r   r   rn   r   valuesdrop)r   r   rC   Zall_entriesr0   r   r   indr5   r5   r6   read_addresses  s    
 r   c           	   	   C   s   d}t | d}||jdddd  | D ]t\}}||   }| D ]J\}}t|dkr~||d t| d  qN||d t| d  qN|| q0W 5 Q R X d S )	N:enditem
#################################################
r>   r   r   r      r   		)r'   r(   rp   iterrowsrn   itemsra   r[   )	r   r   	seperatorr   r   rowZto_writeZindex_wvaluer5   r5   r6   write_addresses  s    r   c                 C   s   d || }tt| d S )Nz<span style=color:{}>{}</span>)formatr   r   )r   colorZcolorstrr5   r5   r6   printmd  s    r   c           	      C   s  d}d}d}g }| j D ]}| j| j}t|s@t|d}t|sR|dkrt| j| j}|| j|df< t|st	dt| d | j| j |d7 }t|r|
tj | j| jdkr| j| jd	krt	d
| j| j d| j| j d qt|}|d kr$|
| |d7 }qt| j| j}t|sJ||krX|
tj qt	d| d|  |d7 }|| j|df< t|}|d kr|
| |d7 }q|
tj qtdt| dt| ddd t	d| || d< | S )Nr   r   rt   DOIzFound the DOI(z) for:r   patentthesiszNo authorlist found for  ()zFound the correct DOI:z for the old one:z**z new DOIs has been found and z& incorrect DOIs have been corrected**.redr   z4We also have created {} formated author list entriesauthors_new)r   locr   rg   rn   r[   r   r   rZ   rb   ri   r   r   r   get_authors_doir   r   )	r   Z
N_new_doisZN_corrected_doisZN_authorlistr   r   r   Zauthor_Znew_doir5   r5   r6   correct_authors_and_doi  sJ    


 (





"r   c                 C   sn   z8t d|  ddd}t|jdkr6|jd j}|W S W n   Y nX t| |dd}|dkrd|d	 S tjS dS )
z.Looks for the DOI in Scopus and Crossref.
    ztitle(r   T)verboser   r   N)author_searchr   r   )ZScopusSearchra   resultsr   find_crossref_for_titler   r   )rZ   Zauthor_presetZdocument_searchZ
doi_scopusr   r5   r5   r6   r   )  s    
r   c                 C   s   d}t  }|d kr8|d kr(|j| d}q`|j| |d}n(|d krP|j| |d}n|j| ||d}|D ]B}ztd |d d |  }W n   d}Y nX |dkrd|  S qdd S )	Ng        )bibliographic)r   r`   )r   container_title)r   r`   r   rZ   r   g?)r   queryr   ratio)rZ   r   r   simr   Zw1r   Zsim_newr5   r5   r6   r   ?  s"    

r   c              	   C   s<   z
t | W S    zt| W  Y S    Y Y d S X Y nX d S N)r   r   )r   r5   r5   r6   r   \  s    
r   c           	      C   sv  |dkrt  }|| }d}dd |d D }t|D ],\}}|t|d d kr\|d7 }|d  d	krt|d7 }|d
  D ]}|d dkr|d dkrt|ddkr|dD ]}||d  d 7 }q|dd d }n||d  d 7 }q|d|d   d 7 }q|d  d	kr0|d }q6|d  rT||d  d 7 }q6||d d 7 }q6|dd }|S )z
    For a given DOI the authors will be searched in Crossref and will be
    formated in the following way:
    'E. Krok, S. Balakin, J. Jung, F. Gross, J. Opitz, and <b>G. Cuniberti</b>''
    Nrt   c                 S   s   g | ]}d |kr|qS )familyr5   ).0xr5   r5   r6   
<listcomp>r  s      z+format_authors_crossref.<locals>.<listcomp>r`   r   and r  r:   givenr   r      –.-r~   r   r   Cuniberti, , r   )r   r   	enumeratera   
capitalizerp   upperisupper)	r   r   r   r   r   r   personfirstspr5   r5   r6   r   h  s2    

r   c                 C   s|  |dkrt | }d}t|jD ]J\}}|t|jd krB|d7 }|j dkrX|d7 }|jdkr|jdkr|j D ]}|d dkr|d d	krt|ddkr|dD ]}|t|d 	 d
 7 }q|dd d }n|t|d 	 d 7 }qz|dt|d 	  d 7 }qz|j dkr4|d }q|j
 rX|t|j d 7 }q|t|jd 7 }q|dd }|S )z
    For a given DOI the authors will be searched in Scopus and formated in the following way:
    'E. Krok, S. Balakin, J. Jung, F. Gross, J. Opitz, and <b>G. Cuniberti</b>''
    Nrt   r   r  r:   r   r   r   r
  r  r~   r   r   r  r  r   )r   r  r   ra   surnamer  r   rp   html_codingr  r  )r   r   r   r   r  r  r  r5   r5   r6   r     s0    
r   c                 C   s   t t| } | S )z&Converts string to HTML entities.
    )neZnamed_entitiesr   escape)r   r5   r5   r6   r    s    r  c              O   C   s  d}dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddN}t |d}| jD ]}| D ]\}}|| jkrt| j| | s| j| | dkst| j| |  dkrnj|d	krdt| j| d
 sd|	|| t| j| d
  d  n$|	|| t| j| |  d  qq|	| qW 5 Q R X t
d| dS )z
    Writes the Dataframe df in the structure needed by the webpage to be imported.
    The database is saved in the file 'filename'.
    r   r   r   r   )NAGr?   r   	firstlastcoverr_   rZ   r   pubdate	referencer   r   filenamenonmieiECEMPCover	publisher
submisdateISBNbbllxbbllybburxbburyfieldhyperrefr   
journalrefarxiv	arxivmeshendpagerefreport_no	report_no	startpager   journalacronymWCUISSNr   r   IF
acceptdate
revisedate
webpubdateESFZIHpudate	booktitleeditorsorder_itpubtownreference_morepISSNaddress	WCUTOPTEN
stampamelovjntref
openaccessAG2journalcover	arxivdate	commentcvaddmaterial
receivdateIFyearPRaccessioncodePRstatusURLIFrefplacemisurelibrarycongressseriescoverdetailsreferenceshortIEEEcnINSPECaneconophysicsother_order_itsenzaISBNlinkr>   r   rt   r   r   r   zDatabase written to file N)r'   r   r   columnsrg   rn   r   r[   r   r(   rb   )r   r  r   sepr   r   keyspacingr5   r5   r6   write_corrected_db  s                                                            

F &&
r`  Fc                 C   s   |dkrt   td td t| }t|}|jdgd}t }|jD ]}|j	|j
ddd}qL|j|d< |jd	ddd
 ||d    }|d d|d< |S )z
    Receives all publications from Google Scholar for a given searchfield
    
    Returns
    -------
        DataFrame
            DataFrame with all publications on Google Scholar with short info (title, year, bib)
    TzOIf nothing happen in the next minute your IP might already be blocked by GooglezTry: proxy, VPN, TOR, ...rT   )sectionsFrV   rY   r_   )	ascendingr   rZ   r[   )use_proxy_scholarlyrb   rc   rd   rf   re   rg   rh   rT   ri   rU   Zsort_valuesrj   rk   )Zsearchfield	use_proxyrq   r`   rr   rM   r5   r5   r6   get_pub_google  s    



re  c                  C   sD   t d tdddddgd } tj| | d td	t|   d S )
NzLet's try to use a proxy...Tr   DEUSCA)randtimeout
country_id)httphttpsz(**proxy is used to access Google with:**)rb   r   r   rc   rd  r   r[   )proxyr5   r5   r6   rc     s
    rc  c                 C   s|   t ddd t }| jD ]>}t| d j|  |j| d |  jddd}t	
d	 q|d
 d|d
< t ddd |S )NzCFill all informations available at **Google** for the publications:bluer   rZ   rY   TFrV      r_   ro   z**Finished**)r   rg   rh   r   rb   r   ri   re   rU   timesleeprk   )ZDF
pub_filledr   r5   r5   r6   google_details_publication	  s    
 rt  r:   c                 C   s  g g g g g g g g f\}}}}}}}}	| d   }
| d   }| d   }td |  D ]\}}t|d  t|d ||d }|dkrz>t|d d	d
}z||j W n   |tj Y nX W n,   t	|d d  d}|tj Y nX ||d  ||d d  || |t
| |	t| z0|jdkrZ||j n||d d  W n<   z||d d  W n   |tj Y nX Y nX || dkrHzB|jdkrt|j| j|df< n|d dd | j|df< W nJ   z |d dd | j|df< W n   tj| j|df< Y nX Y nX |
| dkrzt|d | j|df< W n`   z6|jdkrtj| j|df< nt|j| j|df< W n   tj| j|df< Y nX Y nX || dkrXz|d | j|df< W n`   z6|jdkrtj| j|df< nt|j| j|df< W n   tj| j|df< Y nX Y nX d|d dd ksd|d dd krt||}|dkr||d  n(t|dkr|| n||d  n||d  qX|d ||d  |d ||d  |d ||d  |tj |	tj qXt|| d< t|| d< t|| d< t|| d< t|| d< t|| d< t|| d< | d t| d< t|	| d< t| } | S )z
    Function that gets DOI and title from Crossref and/or Scopus and writes it into the dataframe.
    If volume or issue is not given by Google it will search for it at Crossref and Scopus.
    r   r   r3   z%searching in crossref and scopus for:rZ   r   Nr   FULL)viewz8**not found in scopus**. API key and permission correct?r   r   Tr   r   r   u   …r      z	Full-Text   	new_titler   r   r   
cover_date
first_last)rj   rb   r   r   r   ri   r   r   r   r   r   first_last_authorr   r   r[   r   rp   r   ZissueIdentifierr   ra   arrayrk   clean_abstracts)rs  r`   Zdoi_listZ
title_list	item_listr   r   Zabstract_listrz  r{  Zvolume_conditionZnumber_conditionZpages_conditionelementr   r   r   r   r5   r5   r6   get_details_crossref_scopus  s    
   

  ,




r  c                 C   s   d}z| d  dd}| dd}| dd}| dd}t|dk r|dkrT|W S |jdkrv|jdkrn|W S |j}n|j}|W S |W S    |dkr| Y S |jdkr|jdkr| Y S |j}n|j}Y nX |S )	z
    Receives abstracts for publication from Crossref and Scopus, given the item found by Scopus and Crossref.
    
    Returns
    -------
        String
            abstract
    Nr   z	<jats:p> rt   z<jats:p>z
 </jats:p>z	</jats:p>rx  )r   ra   descriptionr   )r   r   r   r5   r5   r6   r     s2    	



r   c                 C   s"  | j D ]}d}| j| j}|d dkshd|dd kshd|d d kshd|dd kshd	|d d
 kr|ddkr|||dd d   d }nD|ddkr|||dd d   }n|||dd d   }n2d|dd  kr||d |d  }n|| }|| j|df< q| S )Nrt   r   ru   rv   rw   rx   ry   rz   r{   r|   r}   r~   r   r   r   r   r   r   r   r   )r   r   r   r   )r   r   r   r   r5   r5   r6   r~    s    L r~  add.database.papersc              P   C   s&  g }| j tjddd} d}ddddd	d
dddddddddg}ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddO}t|d0}|  D ]\}}z2t|d s*|d dkr*|d }	nt W n6   zt|d }	W n   t	|d }	Y nX Y nX t
|}
t|d }t|d }d|d d! kr||d"  || d#d$d%|d& d'd(t|d) ||	|d* |
|d+ |d" |d'g}t||D ]\}}||| d,  qqW 5 Q R X |S )-a=  
    Write publications from Google to filenname with the structure needed by the webpage of the chair.
    It aslo returns the urls of the articles with incomplete abstracts.
    'XXX' is written in the database where no data can be found. By searching for 'XXX' later we can complete
    those entries by hand.
    ZXXXT)regexr   zAG		zcvselected	ztype		z
firstlast	z
#filename	z#cover		zyear		ztitle		z	authors		z	pubdate		z
reference	zDOI		z	hyperref	z	abstract	r   r   r   r   z	 )Or  r?   r   r  r  r_   rZ   r   r  r  r   r   r  r  r   r!  r"  r#  r$  r%  r&  r'  r(  r)  r*  r   r+  r,  r-  r   r.  r/  r0  r1  r   r2  r3  r4  r   r   r5  r6  r7  r8  r9  r:  r;  r<  r=  r>  r?  r@  rA  rB  rC  rD  rE  rF  rG  rH  rI  rJ  rK  rL  rM  rN  rO  rP  rQ  rR  rS  rT  rU  rV  rW  rX  rY  rZ  r[  r>   r   r   r`   ry  r   z&hellip;iNurlZGCnoZtypeXXXr{  rt   z../pubs/reprints/covers/XXXr_   r   r   r   )r   r   nanr'   r   rg   rn   
ValueErrorr   format_authors_googleformat_referencer  ri   r(   r[   rm   )rT   r  Zurl_abstract_incompleter   Zname_rowr]  r   r   r   Zauthor_shortr  ry  r   rC   namer   r5   r5   r6   add_pubs_to_file  s    
                                                                 


 
      "r  c              	   C   s   d}|dt | d  d 7 }z |dtt| d  d 7 }W n$   |dt| d  d 7 }Y nX z0|tt| d d	 tt| d
  d 7 }W n4   |t| d d	 tt| d
  d 7 }Y nX |S )Nrt   z<i>r   z</i>z <b>r   z</b> r3   r   r_   r   r   )r  r[   ro   )r   r   r5   r5   r6   r    s     0.r  c                 C   s4   | d d d dks(| d d d dkr,dS dS dS )	z8 Checks if Cuniberti is either first or last author
    r`   r   r  r:   r~   yesr  Nr5   r   r5   r5   r6   r|    s    (r|  c                 C   s4   t | D ]&\}}|dkr$t| qt| qdS )Nr   T)r  
webbrowserr'   Zopen_new_tab)urlsr   r  r5   r5   r6   	open_urls  s
    r  c              	   C   s   z| d d d \}}}W n>   z| d d d \}}}W n   t j Y  Y S X Y nX tt|dkrvdt| }tt|dkrdt| }d|||S )Nzpublished-onliner   r   Zissuedr   0z{0}.{1}.{2})r   r   ra   r[   r   )r   r_   monthdayr5   r5   r6   r     s    r   c                 C   s  |dkrdd }d}| }| d}t|D ]\}}| dd }|t|d krX|d }|d	krh|d
 }| dd d }d}	|D ]J}
|
d dkr|
d dkr|	||
d d 7 }	q|	d||
d  d 7 }	q||	 || }|d	kr|}|d }q*|d d }|S )NFc                 S   s   | S r  r5   )r   r5   r5   r6   r  /  s    z*format_authors_google.<locals>.html_codingrt   z and r   r~   r   r:   z<b>r   r   r
  r   r  r   )rp   r  ra   )autZhtml_encoder  r   r   r   r  r  Zfirst_namesZfirst_shortr  r5   r5   r6   r  -  s.    

r  c              	   C   s   d}dddddddd	g}d
dd
d
d
dddg}t dd}| jD ]}| j| d dksf| j| d dkr>t|D ]X\}}t| j| | s| j| | dkrqn||||  t| j| |  d  qn|| q>q>W 5 Q R X d S )Nr   r   r  r_   rZ   r   r   r  r   r   r   z			r   Zdatabase_newr>   regularletterr   r   )r'   r   r   r  rg   rn   r(   r[   )r   r   rC   distr   r   r   contr5   r5   r6   write_corrected_database_shortL  s    
$&*r  )N)N)N)N)F)r:   )r  )F)BZpandasrg   numpyr   rq  r,   Znamedentitiesr  r   r  ZIPython.displayr   r   osenvironZpybliometrics.scopusr   Zfp.fpr   Zcrossref.restfulr   difflibr   django.template.loaderr   r	   django.templater
   django.httpr   r7   rD   rI   rN   rR   rS   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r`  re  rc  rt  r  r   r~  r  r  r|  r  r   r  r  r5   r5   r5   r6   <module>   sj   
&
/
 M
-

%
#.
	
l)
>		
