Skip to content

Commit 55d8494

Browse files
committed
Repository created
1 parent ea56670 commit 55d8494

File tree

28 files changed

+4163
-0
lines changed

28 files changed

+4163
-0
lines changed

Activity01/Activity01.ipynb

Lines changed: 489 additions & 0 deletions
Large diffs are not rendered by default.

Activity02/Activity02.ipynb

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import urllib.request, urllib.parse, urllib.error\n",
10+
"import json\n"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"2.\tLoad the secret API key (you have to get one from the OMDB website and use that; it has a 1,000 daily limit) from a JSON file, stored in the same folder into a variable, by using json.loads(). \n",
18+
"Note\n",
19+
"The following cell will not be executed in the solution notebook because the author cannot give out their private API key.\n",
20+
"The students/users/instructor will need to obtain a key and store it in a JSON file. We are calling this file APIkeys.json. \n"
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": null,
26+
"metadata": {},
27+
"outputs": [],
28+
"source": [
29+
"with open('APIkeys.json') as f:\n",
30+
" keys = json.load(f)\n",
31+
" omdbapi = keys['OMDBapi']\n"
32+
]
33+
},
34+
{
35+
"cell_type": "code",
36+
"execution_count": null,
37+
"metadata": {},
38+
"outputs": [],
39+
"source": [
40+
"serviceurl = 'http://www.omdbapi.com/?'"
41+
]
42+
},
43+
{
44+
"cell_type": "code",
45+
"execution_count": null,
46+
"metadata": {},
47+
"outputs": [],
48+
"source": [
49+
"apikey = '&apikey='+omdbapi"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": null,
55+
"metadata": {},
56+
"outputs": [],
57+
"source": [
58+
"def print_json(json_data):\n",
59+
" list_keys=['Title', 'Year', 'Rated', 'Released', 'Runtime', 'Genre', 'Director', 'Writer', \n",
60+
" 'Actors', 'Plot', 'Language', 'Country', 'Awards', 'Ratings', \n",
61+
" 'Metascore', 'imdbRating', 'imdbVotes', 'imdbID']\n",
62+
" print(\"-\"*50)\n",
63+
" for k in list_keys:\n",
64+
" if k in list(json_data.keys()):\n",
65+
" print(f\"{k}: {json_data[k]}\")\n",
66+
" print(\"-\"*50)\n"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": [
75+
"def save_poster(json_data):\n",
76+
" import os\n",
77+
" title = json_data['Title']\n",
78+
" poster_url = json_data['Poster']\n",
79+
" # Splits the poster url by '.' and picks up the last string as file extension\n",
80+
" poster_file_extension=poster_url.split('.')[-1]\n",
81+
" # Reads the image file from web\n",
82+
" poster_data = urllib.request.urlopen(poster_url).read()\n",
83+
" \n",
84+
" savelocation=os.getcwd()+'\\\\'+'Posters'+'\\\\'\n",
85+
" # Creates new directory if the directory does not exist. Otherwise, just use the existing path.\n",
86+
" if not os.path.isdir(savelocation):\n",
87+
" os.mkdir(savelocation)\n",
88+
" \n",
89+
" filename=savelocation+str(title)+'.'+poster_file_extension\n",
90+
" f=open(filename,'wb')\n",
91+
" f.write(poster_data)\n",
92+
" f.close()\n"
93+
]
94+
},
95+
{
96+
"cell_type": "code",
97+
"execution_count": null,
98+
"metadata": {},
99+
"outputs": [],
100+
"source": [
101+
"def search_movie(title):\n",
102+
" try:\n",
103+
" url = serviceurl + urllib.parse.urlencode({'t': str(title)})+apikey\n",
104+
" print(f'Retrieving the data of \"{title}\" now... ')\n",
105+
" print(url)\n",
106+
" uh = urllib.request.urlopen(url)\n",
107+
" data = uh.read()\n",
108+
" json_data=json.loads(data)\n",
109+
" \n",
110+
" if json_data['Response']=='True':\n",
111+
" print_json(json_data)\n",
112+
" # Asks user whether to download the poster of the movie\n",
113+
" if json_data['Poster']!='N/A':\n",
114+
" save_poster(json_data)\n",
115+
" else:\n",
116+
" print(\"Error encountered: \",json_data['Error'])\n",
117+
" \n",
118+
" except urllib.error.URLError as e:\n",
119+
" print(f\"ERROR: {e.reason}\"\n"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": null,
125+
"metadata": {},
126+
"outputs": [],
127+
"source": [
128+
"search_movie(\"Titanic\")"
129+
]
130+
},
131+
{
132+
"cell_type": "code",
133+
"execution_count": null,
134+
"metadata": {},
135+
"outputs": [],
136+
"source": [
137+
"search_movie(\"Random_error\")"
138+
]
139+
}
140+
],
141+
"metadata": {
142+
"kernelspec": {
143+
"display_name": "Python 3",
144+
"language": "python",
145+
"name": "python3"
146+
},
147+
"language_info": {
148+
"codemirror_mode": {
149+
"name": "ipython",
150+
"version": 3
151+
},
152+
"file_extension": ".py",
153+
"mimetype": "text/x-python",
154+
"name": "python",
155+
"nbconvert_exporter": "python",
156+
"pygments_lexer": "ipython3",
157+
"version": "3.7.3"
158+
}
159+
},
160+
"nbformat": 4,
161+
"nbformat_minor": 2
162+
}

Exercise01/Exercise01.ipynb

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import requests "
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": 3,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"# First assign the URL of Wikipedia home page to a strings \n",
19+
"\n",
20+
"wiki_home = \"https://en.wikipedia.org/wiki/Main_Page\" "
21+
]
22+
},
23+
{
24+
"cell_type": "code",
25+
"execution_count": 7,
26+
"metadata": {},
27+
"outputs": [
28+
{
29+
"data": {
30+
"text/plain": [
31+
"<Response [200]>"
32+
]
33+
},
34+
"execution_count": 7,
35+
"metadata": {},
36+
"output_type": "execute_result"
37+
}
38+
],
39+
"source": [
40+
"response = requests.get(wiki_home) \n",
41+
"response\n"
42+
]
43+
},
44+
{
45+
"cell_type": "code",
46+
"execution_count": 14,
47+
"metadata": {},
48+
"outputs": [
49+
{
50+
"data": {
51+
"text/plain": [
52+
"requests.models.Response"
53+
]
54+
},
55+
"execution_count": 14,
56+
"metadata": {},
57+
"output_type": "execute_result"
58+
}
59+
],
60+
"source": [
61+
"type(response) "
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": []
70+
}
71+
],
72+
"metadata": {
73+
"kernelspec": {
74+
"display_name": "Python 3",
75+
"language": "python",
76+
"name": "python3"
77+
},
78+
"language_info": {
79+
"codemirror_mode": {
80+
"name": "ipython",
81+
"version": 3
82+
},
83+
"file_extension": ".py",
84+
"mimetype": "text/x-python",
85+
"name": "python",
86+
"nbconvert_exporter": "python",
87+
"pygments_lexer": "ipython3",
88+
"version": "3.7.3"
89+
}
90+
},
91+
"nbformat": 4,
92+
"nbformat_minor": 2
93+
}

Exercise02/Exercise02.ipynb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"def status_check(r):\n",
10+
" if r.status_code==200:\n",
11+
" print(\"Success!\")\n",
12+
" return 1\n",
13+
" else:\n",
14+
" print(\"Failed!\")\n",
15+
" return -1\n"
16+
]
17+
},
18+
{
19+
"cell_type": "code",
20+
"execution_count": 2,
21+
"metadata": {},
22+
"outputs": [],
23+
"source": [
24+
"import requests"
25+
]
26+
},
27+
{
28+
"cell_type": "code",
29+
"execution_count": 3,
30+
"metadata": {},
31+
"outputs": [],
32+
"source": [
33+
"wiki_home = \"https://en.wikipedia.org/wiki/Main_Page\""
34+
]
35+
},
36+
{
37+
"cell_type": "code",
38+
"execution_count": 4,
39+
"metadata": {},
40+
"outputs": [],
41+
"source": [
42+
"response = requests.get(wiki_home)"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 5,
48+
"metadata": {},
49+
"outputs": [
50+
{
51+
"name": "stdout",
52+
"output_type": "stream",
53+
"text": [
54+
"Success!\n"
55+
]
56+
},
57+
{
58+
"data": {
59+
"text/plain": [
60+
"1"
61+
]
62+
},
63+
"execution_count": 5,
64+
"metadata": {},
65+
"output_type": "execute_result"
66+
}
67+
],
68+
"source": [
69+
"status_check(response)"
70+
]
71+
},
72+
{
73+
"cell_type": "code",
74+
"execution_count": null,
75+
"metadata": {},
76+
"outputs": [],
77+
"source": []
78+
}
79+
],
80+
"metadata": {
81+
"kernelspec": {
82+
"display_name": "Python 3",
83+
"language": "python",
84+
"name": "python3"
85+
},
86+
"language_info": {
87+
"codemirror_mode": {
88+
"name": "ipython",
89+
"version": 3
90+
},
91+
"file_extension": ".py",
92+
"mimetype": "text/x-python",
93+
"name": "python",
94+
"nbconvert_exporter": "python",
95+
"pygments_lexer": "ipython3",
96+
"version": "3.7.3"
97+
}
98+
},
99+
"nbformat": 4,
100+
"nbformat_minor": 2
101+
}

0 commit comments

Comments
 (0)