Ejemplo n.º 1
0
    def sync(event):
        if sync_flag:
            MESSAGE_QUEUE.put("正在同步,请等待成功后再试")
            return
        usertext = user.GetValue()
        passwdtext = passwd.GetValue()
        MESSAGE_QUEUE.put("读取用户名和密码")
        selectOpt = chooseScoreChoice.GetCurrentSelection()
        save_file(usertext, passwdtext)
        printcolor("当前登录方式:" + options[selectOpt])
        action = execute

        def third_action(u, p):
            printcolor(options[selectOpt] + " 方式耗时较长,请耐心等待一下")
            cururl, cookies = third_login.login(options[selectOpt],
                                                driver=DRIVER)
            loginsuccess.Show()
            update_cookies(cookies)
            getUserAction(cururl)

        if options[selectOpt] in third_login.OPTIONS:
            action = third_action

        @exception_run
        def _sync():
            global sync_flag
            sync_flag = True
            MESSAGE_QUEUE.put("开始同步...")
            change_workspace()
            action(usertext, passwdtext)
            MESSAGE_QUEUE.put("操作完成,图片备份目录:文稿 => 花瓣网备份")
            sync_flag = False

        thread.start_new_thread(_sync, ())
Ejemplo n.º 2
0
 def chooseScoreFunc(event):
     index = event.GetEventObject().GetSelection()
     scoreChoosed = options[index]
     printcolor("选择登录方式:" + scoreChoosed)
     selectOpt = index
     if scoreChoosed in third_login.OPTIONS:
         thread.start_new_thread(sync, (event, ))
Ejemplo n.º 3
0
 def third_action(u, p):
     printcolor(options[selectOpt] + " 方式耗时较长,请耐心等待一下")
     cururl, cookies = third_login.login(options[selectOpt],
                                         driver=DRIVER)
     loginsuccess.Show()
     update_cookies(cookies)
     getUserAction(cururl)
Ejemplo n.º 4
0
def main(parser):
    global PICBED_URL, PICBED_TOKEN
    args = parser.parse_args()
    is_board = args.board
    is_user = args.user
    if not is_board and not is_user:
        parser.print_help()
        return
    bou = args.board_or_user
    PICBED_URL = args.picbed_url
    PICBED_TOKEN = args.picbed_token
    if not PICBED_URL:
        printcolor("请输入picbed地址")
        return
    if is_user:
        if isdir(join(BASE_DIR, bou)):
            boards = [
                join(BASE_DIR, bou, d) for d in listdir(bou)
                if isdir(join(BASE_DIR, bou, d))
            ]
        else:
            printcolor("用户目录不存在", "red")
    else:
        boards = [
            join(BASE_DIR, "boards", d) for d in bou.split(",")
            if isdir(join(BASE_DIR, "boards", d))
        ]
    for board_path in boards:
        board_id = board_path.split(sep)[-1]
        album = board_id
        try:
            board_info = request.get("%s/boards/%s" % (BASE_URL, board_id),
                                     timeout=5).json()
        except requests.exceptions.RequestException:
            pass
        else:
            if isinstance(board_info, dict) and "board" in board_info:
                board_info = board_info["board"]
                album = board_info.get("title") or board_info["board_id"]
        board2piced(album, board_path)
Ejemplo n.º 5
0
def board2piced(album, board_path):
    if isdir(board_path):
        apiurl = "%s/api/upload" % PICBED_URL.rstrip("/")
        headers = {
            "User-Agent": "grab_huaban_board/%s" % __version__,
            "Authorization": "Token %s" % PICBED_TOKEN
        }
        storage = LocalStorage()
        success = []
        for filename in listdir(board_path):
            suffix = filename.split(".")[-1]
            filepath = join(board_path, filename)
            sindex = filepath.split(BASE_DIR)[-1]
            if storage.get(sindex):
                printcolor("%s 已上传,继续下一个" % filepath, "blue")
                continue
            if isfile(filepath) and suffix in ALLOWED_SUFFIX:
                files = {
                    'picbed': (filename, open(filepath,
                                              'rb'), 'image/%s' % suffix)
                }
                try:
                    r = requests.post(apiurl,
                                      files=files,
                                      headers=headers,
                                      data=dict(album=album),
                                      timeout=30)
                except requests.exceptions.RequestException as e:
                    printcolor(u"%s 上传错误:%s" % (filepath, e.message), "yellow")
                else:
                    result = r.json()
                    if result.get("code") == 0:
                        success.append(sindex)
                        printcolor(u"%s 上传成功" % filepath, "green")
                    else:
                        printcolor(u"%s 上传失败:%s" % (filepath, result["msg"]),
                                   "yellow")
        if success:
            storage.setmany(**{f: time() for f in success})
Ejemplo n.º 6
0
def change_workspace():
    makedir(BASE_PATH)
    os.chdir(BASE_PATH)
    printcolor("图片备份目录:文稿 => 花瓣网备份")
Ejemplo n.º 7
0
 def wrapper(*args, **kws):
     try:
         return f(*args, **kws)
     except Exception as e:
         printcolor(os.getcwd() + ' 程序运行出错:' + str(e))
         printcolor(traceback.format_exc())
Ejemplo n.º 8
0
import wx
import thread
import time
import os
import sys
import getpass
from grab_huaban_board import execute, MESSAGE_QUEUE, printcolor, makedir, \
    update_cookies, getUserAction
import third_login
import traceback
from selenium import webdriver
if getattr(sys, 'frozen', False):
    runtime_dir = sys._MEIPASS
else:
    runtime_dir = os.path.dirname(os.path.abspath(__file__))
printcolor("当前运行目录:" + runtime_dir)
os.environ['PATH'] += ':' + runtime_dir
passwd_file = 'password.conf'
sync_flag = False
user_name = getpass.getuser()
BASE_PATH = '/Users/%s/Documents/花瓣网备份/' % user_name
DRIVER = None


def exception_run(f):
    def wrapper(*args, **kws):
        try:
            return f(*args, **kws)
        except Exception as e:
            printcolor(os.getcwd() + ' 程序运行出错:' + str(e))
            printcolor(traceback.format_exc())