Skip to content

Instantly share code, notes, and snippets.

@glasslion
Created April 15, 2019 13:00
Show Gist options
  • Save glasslion/fa134eeeeae3a38584e3de45ca92280c to your computer and use it in GitHub Desktop.
Save glasslion/fa134eeeeae3a38584e3de45ca92280c to your computer and use it in GitHub Desktop.

Revisions

  1. glasslion created this gist Apr 15, 2019.
    72 changes: 72 additions & 0 deletions bilibili_screenshots.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,72 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    """
    Bilibili Screenshots Syncer
    It can download `/Pictures/bili/screenshot` from a Android phone via ftp.
    It also rename the picture so the timestamp comes first.
    """
    from __future__ import print_function
    import argparse
    from ftplib import FTP
    import glob
    import re
    import os
    import subprocess

    try:
    input = raw_input
    except NameError:
    pass


    # -r: Is for recursively download.
    # -np: Is for no parent ascending.
    # -nH: Is for disabling creation of directory having name same as URL i.e. abc.xyz.com
    # --cut-dirs: Is for ignoring number of parent directories. e.g. Pictures/bili/screenshot/*.png has 3 parent dirs
    # -N: Is for skiping existing files
    WGET_CMD = "wget -r -N -np -nH --cut-dirs=2 ftp://{ip}:{port}/Pictures/bili/screenshot"


    def download_pics(ip, port):
    cmd = WGET_CMD.format(ip=ip, port=port)
    subprocess.call(cmd, shell=True)


    def rename_file():
    pic_paths = glob.glob('screenshot/*.png')
    for path in pic_paths:
    new_path = re.sub(r"(\d+)@(\d+)@(.*.png)", r"\g<2>_\g<1>_\g<3>", path)
    os.rename(path, new_path)

    def yes_no(question_to_be_answered):
    while True:
    choice = input(question_to_be_answered + ' [y/n]: ').lower()
    if choice[:1] == 'y':
    return True
    elif choice[:1] == 'n':
    return False
    else:
    print("Please respond with 'Yes' or 'No'\n")

    def delete_remote(ip, port):
    ftp = FTP()
    ftp.connect(ip, port)
    ftp.login()
    if yes_no('Delete the remote dir /Pictures/bili/screenshot?'):
    ftp.rmd('/Pictures/bili/screenshot')


    def main():
    parser = argparse.ArgumentParser(description='Bilibili Screenshots Syncer')
    parser.add_argument('ip', help='FTP server IP')
    parser.add_argument('--port', default=2121, type=int, help='FTP server port')
    args = parser.parse_args()

    download_pics(args.ip, args.port)
    rename_file()
    delete_remote(args.ip, args.port)


    if __name__ == '__main__':
    main()