python-短网址批量还原

python-短网址批量还原源码

#-*- coding = utf-8 -*-
#Time : 2021/1/23 
#Author : 小柠檬
#File : 短网址.py
import gevent.pool
from gevent import monkey
monkey.patch_all()
import requests
import json
import os
import re
from urllib.parse import unquote
import time

def makeFile():
    if not os.path.exists(r'C:\Users\Administrator\Desktop\短网址'):
        os.makedirs(r'C:\Users\Administrator\Desktop\短网址')

    with open(r'C:\Users\Administrator\Desktop\短网址\短网址.txt','a') as f:
        f.write('')

    with open(r'C:\Users\Administrator\Desktop\短网址\原网址.txt','a') as f:
        f.write('')

    with open(r'C:\Users\Administrator\Desktop\短网址\还原失败网址.txt','a') as f:
        f.write('')

def emptyTxt():
    with open(r'C:\Users\Administrator\Desktop\短网址\原网址.txt', 'w') as f:
        f.write('')

    with open(r'C:\Users\Administrator\Desktop\短网址\还原失败网址.txt', 'w') as f:
        f.write('')

def restoreUrl(shortUrl):
    url = 'http://api.dwzjh.com/api/reduction?url={}'.format(shortUrl)

    try:
        r = requests.get(url)

        if json.loads(r.text).get('code') == 1:
            regex = re.compile(r'qc=(.*)"')
            longUrl = re.findall(regex,r.text)[0]
            print(unquote(longUrl))

            with open(r'C:\Users\Administrator\Desktop\短网址\原网址.txt', 'a') as f:
                f.write(unquote(longUrl)+'\n')

        else:
            print(shortUrl,json.loads(r.text).get('msg'))

            with open(r'C:\Users\Administrator\Desktop\短网址\还原失败网址.txt', 'a') as f:
                f.write(unquote(shortUrl)+'\n')

    except Exception as e:
        print(e)

def main():
    with open(r'C:\Users\Administrator\Desktop\短网址\短网址.txt', 'r') as f:
        shortUrlList = f.readlines()

    shortUrlList = [short.strip() for short in shortUrlList if short != '\n']
    print('短网址数量:',len(shortUrlList))

    with open(r'C:\Users\Administrator\Desktop\短网址\短网址.txt', 'w') as f:
        f.write('')

    with open(r'C:\Users\Administrator\Desktop\短网址\短网址.txt', 'a') as f:
        for short in shortUrlList:
            f.write(short+'\n')

    pool = gevent.pool.Pool(10)
    pool.map(restoreUrl,shortUrlList)

    print('耗时:{}'.format(time.perf_counter()),'还原完成啦~')

    input('按下回车键结束程序...')

if __name__ == '__main__':
    makeFile()
    emptyTxt()
    main()

你可能感兴趣的:(python-短网址批量还原,python)