File size: 7,786 Bytes
8334186
 
 
 
 
 
 
 
 
 
 
 
 
 
c7017cd
124f795
8334186
124f795
8334186
 
 
 
b92863b
8334186
 
a6bff4f
8334186
 
 
 
 
a6bff4f
8334186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
124f795
 
 
 
 
 
 
 
 
 
 
 
 
 
8334186
 
 
 
 
a6bff4f
8334186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8df3557
 
 
 
 
 
 
 
 
 
 
8334186
 
 
 
256d47b
b3b13a6
8df3557
 
b3b13a6
 
 
 
 
 
d52017c
 
8df3557
 
 
 
 
 
 
 
 
 
a6bff4f
8334186
 
 
 
 
5c00c6f
8334186
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a35298e
 
8334186
 
 
 
 
 
4285ee6
8334186
 
bb28086
 
 
 
8334186
 
 
 
 
 
 
 
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


import os 
import json
import requests
import time

from github import Github, GithubIntegration

import gradio as gr


app_id = '307916'
owner='duerrsimon'
repo_name='bioicons'
    
app_key = os.environ.get('GITHUB_TOKEN')
    


licenses = ['mit', 'cc-by-3.0', 'cc-0', 'cc-by-4.0', 'bsd', 'cc-by-sa-3.0', 'cc-by-sa-4.0']

categories = ["Machine_Learning", "Genetics", "Chemistry", "Scientific_graphs", "Cell_membrane", "Microbiology", "Oncology", "Molecular_modelling", "Intracellular_components", "Cell_types", "Cell_lines", "Plants_Algae", "Chemo-_and_Bioinformatics", "Cell_culture", "Tissues", "Computer_hardware", "Nucleic_acids", "General_items", "Safety_symbols", "Receptors_channels", "Human_physiology", "Animals", "Lab_apparatus", "Blood_Immunology", "Viruses",  "Parasites"]


def create_branch(site, branch_name):
    return site.create_git_ref(
        'refs/heads/{branch_name}'.format(**locals()),
        site.get_branch('main').commit.sha
    )

def create_pull_request(site,branch_name, github_user):
    if github_user!="":
        message = f"submitted by: @{github_user}."
    else: 
        message ="No github account provided."
    return site.create_pull(
        title="New icons submitted via web",
        body= message,
        base="main",
        head=branch_name
    )



def remove_special_chars(author):
    author = author.replace(" ", "-")
    author = author.replace(".", "-")
    author = author.replace("(", "-")
    author = author.replace(")", "-")
    author = author.replace(":", "-")
    author = author.replace(";", "-")
    author = author.replace(",", "-")
    author = author.replace("!", "-")
    author = author.replace("?", "-")
    author = author.replace("=", "-")
    author = author.replace("+", "-")
    author = author.replace("/", "-")
    return author

def process_and_create_pull(icon_files,category, new_cateogry_check, new_category, license, creator_name, creator_url, copyright, github_user):
    
    
    git_integration = GithubIntegration(
        app_id,
        app_key,
    )
    api = Github(
            login_or_token=git_integration.get_access_token(
                git_integration.get_repo_installation(owner, repo_name).id
            ).token
        )
    
    site = api.get_repo('duerrsimon/bioicons')

    if copyright:
        if new_cateogry_check:
            category = remove_special_chars(new_category)
        hash = '{0:010x}'.format(int(time.time() * 256))[:8]
        branch_name = f"new-icons-{hash}"
        create_branch(site,branch_name)

        authors = requests.get("https://bioicons.com/icons/authors.json").json()

        #remove whitespace and other special characters
        author = remove_special_chars(creator_name)

        if author not in authors.keys():
            index_file = site.get_contents('static/icons/authors.json')

            authors[author]=creator_url
            updated_content = json.dumps(authors, indent=4)
            site.update_file(
                path='static/icons/authors.json',
                message='Insert author',
                content=updated_content,
                sha=index_file.sha,
                branch=branch_name
            )
        #all_files = []
        #contents = site.get_contents("static/icons/{license}/{category}/{author}/")
        #print(contents)
        #while contents:
        #    file_content = contents.pop(0)
        #    if file_content.type == "dir":
        #        contents.extend(site.get_contents(file_content.path))
        #    else:
        #        file = file_content
        #        all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
        #print("files in directory:", all_files)
        for file in icon_files:
            #get file content from filesystem
            with open(file.name, 'r') as fp:
                file_content = fp.read()
            filename = os.path.basename(file.name).replace(" ", "_")
            git_file = f'static/icons/{license}/{category}/{author}/{filename}'

            try:
                site.create_file(
                    path=git_file,
                    message=f'create {filename}',
                    content=file_content,
                    branch=branch_name
                )
            except Exception as e:
                print(e, git_file, "already may exist, trying updating")
                contents = site.get_contents(git_file)
                site.update_file(
                    path=git_file, 
                    message=f'update {filename}', 
                    content=file_content, 
                    sha=contents.sha, 
                    branch=branch_name
                )
                
                
        pull = create_pull_request(site,branch_name, github_user)
        return f"""<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.css"  rel="stylesheet" />
<div class="flex p-4 mb-4 text-sm text-green-800 rounded-lg bg-green-200 " role="alert">
  
  <span class="sr-only">Success</span>
  <div>
    <span class="font-medium">Thank you!</span> Your pull request for the submitted illustrations has been submitted on GitHub. You can track your submission on Github at <br> <a class="text-green-400 text-lg hover:underline" target="_blank" href="{pull.html_url}">{pull.html_url}</a>.
  </div>
</div>
"""
    else:
        return """<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.6.4/flowbite.min.css"  rel="stylesheet" />
<div class="flex p-4 mb-4 text-sm text-red-800 rounded-lg bg-red-200 dark:bg-gray-800 dark:text-red-400" role="alert">
  <svg aria-hidden="true" class="flex-shrink-0 inline w-5 h-5 mr-3" fill="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
  <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v3.75m-9.303 3.376c-.866 1.5.217 3.374 1.948 3.374h14.71c1.73 0 2.813-1.874 1.948-3.374L13.949 3.378c-.866-1.5-3.032-1.5-3.898 0L2.697 16.126zM12 15.75h.007v.008H12v-.008z" />
</svg>

  <span class="sr-only">Info</span>
  <div>
    <span class="font-medium">Copyright error.</span> You need to certify that the illustrations have a suitable license and that copyright allows to share it on Bioicons.
  </div>
</div>
"""

def shownewcategory(new_category, check):
    if check:
        return gr.Textbox(visible=True)
    return gr.Textbox(visible=False)
    

with gr.Blocks() as demo:
    with gr.Row():
         creator_name = gr.Textbox(label="Creator")
         creator_url = gr.Textbox(label="Creator URL", placeholder="Link to personal website,Twitter,Mastodon, GitHub")
    github_user = gr.Textbox(label="Your GitHub Username", placeholder="GitHub username is used to communicate with you")
    icon_files = gr.File(label="Icon files", file_types=["svg"], file_count="multiple")
    with gr.Row():
        category = gr.Dropdown(label="Category", choices=categories)
        new_category_check = gr.Checkbox(info="New category?", label="Desired category not listed?")
        new_category = gr.Textbox(label="New category", visible=False)
        new_category_check.change(fn=shownewcategory, inputs=[new_category,new_category_check], outputs=new_category)
        license = gr.Dropdown(label="License", choices=licenses)      
    copyright = gr.Checkbox(info="Copyright", label="I certify that I own the copyright of the submitted illustrations or have appropriately credited the original creator as required by the license of the illustration.")
    btn = gr.Button(value="Submit illustrations")
    output = gr.HTML(label="Output", value="")

    btn.click(process_and_create_pull, inputs=[icon_files,category, new_category_check, new_category, license, creator_name, creator_url, copyright, github_user], outputs=output, api_name="bioicons-submit")

demo.launch()