NA
Python Programming
Homework

kcchung

kcchung@cs.nctu.edu.tw

You can use either
python2 or python3.

Homework 1

weather indicator
(60%)

Requirements(1)

Requirements(2)

  • Show weather information of cities
  • Can show different unit(Celsius, Fahrenheit)
  • Current weather condition
  • sunset/sunrise time
  • 5 days of forecast
  • Can read configuration from config.py
python weather.py [-h] [-l location] [-u unit] [-a | -c | -d day | -s]

-h     print help           (2%)
-l     locations            (8%)
-u     unit                 (3%) 
-a     equal to -c -d 5     (5%)
-c     current condition    (10%)
-d     forecast             (10%)
-s     sunset/sunrise       (2%)

Requirements(3)

Requirements(4)

Argument Check (10%)

  • No location
  • No type of information

Requirements(5)

unit and location configuration (10%)

  • config.py can set unit and location like below
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# file: config.py

LOCATION=Hsinchu
UNIT=c

Requirements(6)

Hint: packages you might use

  • re
  • urllib
  • requests
  • json
  • argparse

Requirements(7)

Hint: resources you might use

  • WOEID
  • yahoo weather api

Homework 2

youtube search crawler
(40%)

Requirements(1)

  • Show the search result
  • Use UrlFit api to shorten link
  • You must parse youtube yourself

Requirements(2)

Requirements(3)

Requirements(4)

Requirements(4)

python youtube.py [-h] [-n] keyword

-h     print help           (2%)
-n     number of search result. default is 5 (5%)
-p     page that you parse  (5%)

Requirements(6)

Output fields (10%)

{title} ({url})
{description}
Like: {number_of_like}, Dislike: {number_of_dislike}

For example:
勸世寶貝喵喵ㄉ喵電感應 (https://url.fit/ZA40H)
喵喵首張個人專輯"喵喵異次元" 好評發售中購買請洽勸世宗親會粉絲專頁&露天拍賣專區訂購 ...
Like: 6,483, Dislike: 2,914

Requirements(7)

Url Part

  • If you output youtube url, you get 5%
  • If you output shortened url, you get 10%
勸世寶貝喵喵ㄉ喵電感應 (https://www.youtube.com/watch?v=DDO_aCkivxU)
喵喵首張個人專輯"喵喵異次元" 好評發售中購買請洽勸世宗親會粉絲專頁&露天拍賣專區訂購 ...
Like: 6,483, Dislike: 2,914
勸世寶貝喵喵ㄉ喵電感應 (https://url.fit/ZA40H)
喵喵首張個人專輯"喵喵異次元" 好評發售中購買請洽勸世宗親會粉絲專頁&露天拍賣專區訂購 ...
Like: 6,483, Dislike: 2,914

Requirements(8)

  • Detect wrong format

Argument Check (8%)

jaychung@Utopi4 [01:56:20] [~/Desktop]
-> % python youtube.py 喵電感應 -n ad
usage: youtube.py [-h] [-n N] [-p P] keyword
youtube.py: error: argument -n: invalid int value: 'ad'

jaychung@Utopi4 [01:56:20] [~/Desktop]
-> % python youtube.py 喵電感應 -p abc
usage: youtube.py [-h] [-n N] [-p P] keyword
youtube.py: error: argument -p: invalid int value: 'abc'

Hint: packages you might use

  • urllib

  • ​beautifulsoup
  • requests
  • json
  • argparse

Requirements(9)

Tutorial

  • Note: url must be encoded

How to call UrlFit shorten api?

GET https://developer.url.fit/api/shorten?long_url={your_long_url}
RETURN {'url': hash}

=> https://url.fit/{hash}
Before encode:
https://www.google.com.tw/#q=python+requests+encode+url
After encode:
https%3A%2F%2Fwww.google.com.tw%2F%23q%3Dpython%2Brequests%2Bencode%2Burl

GET https://developer.url.fit/api/shorten?long_url=https%3A%2F%2Fwww.google.com.tw%2F%23q%3Dpython%2Brequests%2Bencode%2Burl

Your homework should contain three files like below

.
├── config.py (optional)
├── weather.py
└── youtube.py

0 directories, 3 files

If you still feel confused about requirements, please contact with me.

 

kcchung@cs.nctu.edu.tw