-
Notifications
You must be signed in to change notification settings - Fork 0
/
readContent.py
75 lines (53 loc) · 1.8 KB
/
readContent.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# -*- coding: utf-8 -*-
import os
import re
#return a dictionary represent all the food topics and their contents
def readContent():
foods = {}
#get content directory
dirName = os.path.join( os.path.dirname( __file__ ), "static" )
#get all exact content
list = os.listdir( dirName )
for item in list:
food = {}
contentPath = os.path.join( dirName, item )
#get all tags for a topic
tagsContentPath = os.path.join( contentPath, "tags.txt" )
tagsFile = open( tagsContentPath, "r" )
tagList = []
for tag in tagsFile:
tag = tag.strip("\n")
tagList.append( tag )
food["tags"] = tagList
tagsFile.close()
#get all introdutions for a topic if they exist
introContentPath = os.path.join( contentPath, "introduction.txt" )
if os.path.isfile( introContentPath ):
introsFile = open( introContentPath, "r" )
introDic = {}
for introItem in introsFile:
intro = introItem.split( ":" )
introDic[intro[0].strip()] = intro[1].strip()
food["intros"] = introDic
introsFile.close()
#get all the related link of the topic
viewPath = os.path.join( contentPath, "view.txt" )
viewFile = open( viewPath, "r" )
pattern = re.compile( r"\d+" )
relatedLinkList = []
for relatedLine in viewFile:
for relatedItem in pattern.findall( relatedLine ):
relatedLinkList.append( relatedItem )
food["relatedLink"] = relatedLinkList
#get the link and name of the topic
linkPath = os.path.join( contentPath, "link.txt" )
linkFile = open( linkPath, "r" )
name = linkFile.readline().strip("\n")
link = linkFile.readline().strip("\n")
food["link"] = link
linkFile.close()
#store the number of the topic in baidu baike
food["number"] = item
#store the topic as an element of the 'foods' dictionary
foods[name] = food
return foods