Skip to content

Instantly share code, notes, and snippets.

@artu-hnrq
Last active August 20, 2020 03:27
Show Gist options
  • Save artu-hnrq/bfe1df326b3ac70287961d8b54e0e904 to your computer and use it in GitHub Desktop.
Save artu-hnrq/bfe1df326b3ac70287961d8b54e0e904 to your computer and use it in GitHub Desktop.
Some created scripts
#!/usr/bin/python3
"""Update UNIX-like system user profile picture with its GitHub account's one
This script assumes same system and platform username, but a different one can be declared.
It downloads target GitHub account's picture and set it up to current system user
"""
import os
import requests
import configparser
import click
# Get system's logged username
USER = os.getlogin()
def save_github_picture(username, path):
"""Gets GitHub user account's profile picture and saves it into target path
:param username: GitHub account username
:param path: Picture's path destination
"""
with open(path, "wb") as picture:
gh_profile_picture = requests.get(f"https://github.com/{username}.png").content
picture.write(gh_profile_picture)
def set_user_profile_picture(path):
"""Edits logged user AccountsService file to setup its profile picture path
:param path: New profile picture path
"""
# UNIX-like system user settings file
accounts_service_path = f"/var/lib/AccountsService/users/{USER}"
cfg = configparser.ConfigParser()
cfg.read(accounts_service_path)
cfg['User']['Icon'] = path
with open(accounts_service_path, 'w') as accounts_service:
cfg.write(accounts_service)
@click.command()
@click.argument('github_username', default=USER)
@click.option('--path', default=f"/home/{USER}/.face", help='define picture target path.')
def run(github_username, path):
"""Command line interface to define script's arguments
:param github_username: GitHub account username (default: system user)
:param path: New profile picture path (default: ~/.face)
"""
try:
save_github_picture(github_username, path)
set_user_profile_picture(path)
except Exception:
raise
else:
print(f"User profile picture updated with {github_username}'s GitHub one")
if __name__ == '__main__':
if os.geteuid() != 0:
raise PermissionError("This action requires root privileges")
else:
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment