中央财经大学图书馆:座位对应的人Name获取

# THIS FILE IS PART OF Mr.Guo WULIAO.PY FILES
# check.py - TO FIND NAEM OR FIND PLACE OF CUFE LIBRARY
# THIS PROGRAM IS A FREE SOFTWARE
# YOU SHOULD HAVE RECEIVED A COPY OF LICENSE, IF NOT,YOU SHOULD ADD QQ:3206283501
# @AUTHOR: 
# Copyright (c) CUFE COMPUTER 15 
# Copyright (c) 2017 Mr.Guo 
# WANTING FOR A GIRL FRIEND ADD QQ:3206283501

#import lib
import random
import json
from urllib import request
import time
from bs4 import BeautifulSoup
import threading

#init flag
flag = True

#print now time
print("当前时间")
print("========================")
print(time.asctime(time.localtime(time.time())))
print("========================")

#get_content
def get_html_content(url):
    html = request.urlopen(url)
    content = html.read()
    return content

#append urls to a list
url = "http://10.12.162.31/ClientWeb/xcus/ic2/Default.aspx"
html = request.urlopen(url)
bs_obj = BeautifulSoup(html,"html.parser")
url = bs_obj.findAll("li",{"class":"it"})
url_list = []
date = time.strftime('%Y-%m-%d',time.localtime(time.time()))

for i in range(5,len(url)):
    fuck = url[i].attrs["url"] 
    fuck = list(fuck)
    site_id = ''
    for i in range(40,49):
        site_id += fuck[i]

    page_url = "http://10.12.162.31/ClientWeb/pro/ajax/device.aspx?byType=devcls&classkind=8&display=fp&md=d&room_id=%s"%site_id + "&purpose=&cld_name=default&date=%s"%date + "&fr_start=15%3A30&fr_end=16%3A30" + "&act=get_rsv_sta&_=%s"%str(time.time())[0:10]
    url_list.append(page_url)

#function for check
def append(start,end):
    inside_url = []
    for i in range(start,end):
        inside_url.append(url_list[i])
    return inside_url

#check area sites state
def check_area(site_id,inside_url):
    site_id_list = list(site_id)
    area = site_id_list[1]
    url = ''
    area = area.upper()
    
    if area == 'A':
        url = inside_url[0]
    elif area == 'B':
        url = inside_url[1]
    elif area == 'C':
        url = inside_url[2]
    elif area == 'D':
        url = inside_url[3]
    elif area == 'E':
        url = inside_url[4]
    elif area == 'F':
        url = inside_url[5]
    else:
        print("You enter a bad area")
        
    return url

#find person function
def find_person(url):
    global flag
    json_content = get_html_content(url)
    json_content = str(json_content,encoding = "utf-8")
    #print(json_content)
    json_read = json.loads(json_content)
    #print(json_read)
    for i in range(len(json_read['data'])):
        try:
            if name == json_read['data'][i]['ts'][0]['member']:
                print(json_read['data'][i]['devName'])
                flag = False
                break
            elif name == json_read['data'][i]['ts'][0]['owner']:
                print(json_read['data'][i]['devName'])
                flag = False
                break
        except Exception:
            pass

#main function
choice = input('''enter u choice:
    a.enter place and find name
    b.enter name find place
    c.print total site-name list
    d.check area place name
    ''')
print("========================")
choice = choice.lower()

if choice == 'a':
        site_id = input("enter the place.eg 2F512\n:")
        
        if site_id.startswith("1"):
            inside_url = append(0,3)
            url = check_area(site_id,inside_url)

        elif site_id.startswith("2"):
            inside_url = append(3,9)
            url = check_area(site_id,inside_url)
            
        elif site_id.startswith("3"):
            inside_url = append(9,15)
            url = check_area(site_id,inside_url)

        elif site_id.startswith("4"):
            inside_url = append(15,21)
            url = check_area(site_id,inside_url)

        elif site_id.startswith("5"):
            inside_url = append(21,26)
            url = check_area(site_id,inside_url)

        else:
            print("You are sb for that u enter a false floor id")
            
        
        json_content = get_html_content(url)
        json_content = str(json_content,encoding = "utf-8")
        json_read = json.loads(json_content)

        for i in range(len(json_read['data'])):
            try:
                if site_id == json_read['data'][i]['devName']:
                    if json_read['data'][i]['ts'][0]['member'] != '':
                        print(json_read['data'][i]['ts'][0]['member'])
                    elif json_read['data'][i]['ts'][0]['owner'] != '':
                        print(json_read['data'][i]['ts'][0]['owner'])
                    else:
                        print("There is no one in this place")
            except Exception as e:
                pass
        print("========================")

            
elif choice == 'b':
    name = input("enter the name.eg 郭磊\n:")
    threads = []
    for url in url_list:
        t = threading.Thread(target = find_person,args = (url,))
        t.start()
        threads.append(t)
    for t in threads:
        t.join()
    if flag:
            print("The person u want to find don't occupy any site")
    print("========================")

            
elif choice == 'c':
    for url in url_list:
        json_content = get_html_content(url)
        json_content = str(json_content,encoding = "utf-8")
        #print(json_content)
        
        json_read = json.loads(json_content)
        #print(json_read)
        for i in range(len(json_read['data'])):
            try:
                if json_read['data'][i]['ts'][0]['member'] != '':
                    print(json_read['data'][i]['devName']+' '+json_read['data'][i]['ts'][0]['member'])
                else:
                    print(json_read['data'][i]['devName']+' '+json_read['data'][i]['ts'][0]['owner'])
            except Exception:
                pass
        print("========================")
        
elif choice == 'd':
        floor_id = input("enter the floor.eg 2F\n:")
        
        if floor_id.startswith("1"):
            inside_url = append(0,3)
            url = check_area(floor_id,inside_url)

        elif floor_id.startswith("2"):
            inside_url = append(3,9)
            url = check_area(floor_id,inside_url)
            
        elif floor_id.startswith("3"):
            inside_url = append(9,15)
            url = check_area(floor_id,inside_url)

        elif floor_id.startswith("4"):
            inside_url = append(15,21)
            url = check_area(floor_id,inside_url)

        elif floor_id.startswith("5"):
            inside_url = append(21,26)
            url = check_area(floor_id,inside_url)

        else:
            print("You are sb for that u enter a false floor id")

        json_content = get_html_content(url)
        json_content = str(json_content,encoding = "utf-8")
        #print(json_content)
        
        json_read = json.loads(json_content)
        #print(json_read)
        for i in range(len(json_read['data'])):
            try:
                if json_read['data'][i]['ts'][0]['member'] != '':
                    print(json_read['data'][i]['devName']+' '+json_read['data'][i]['ts'][0]['member'])
                else:
                    print(json_read['data'][i]['devName']+' '+json_read['data'][i]['ts'][0]['owner'])
            except Exception:
                pass
        print("========================")

else :
    print("You enter a bad choice please try later")
        
input("PRESS ENTER TO QUIT")

效果图

中央财经大学图书馆:座位对应的人Name获取_第1张图片

中央财经大学图书馆:座位对应的人Name获取_第2张图片


你可能感兴趣的:(中央财经大学)