-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_xml.py
140 lines (120 loc) · 4.11 KB
/
read_xml.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
from lxml import etree
from io import StringIO, BytesIO
import re
from datetime import datetime
def time_ago(time=False):
"""
Get a datetime object or a int() Epoch timestamp and return a
pretty string like 'an hour ago', 'Yesterday', '3 months ago',
'just now', etc
Modified from: http://stackoverflow.com/a/1551394/141084
"""
now = datetime.utcnow()
if type(time) is int:
diff = now - datetime.fromtimestamp(time)
elif type(time) is float:
diff = now - datetime.fromtimestamp(time)
elif isinstance(time,datetime):
diff = now - time
elif not time:
diff = now - now
else:
raise ValueError('invalid date %s of type %s' % (time, type(time)))
second_diff = diff.seconds
day_diff = diff.days
# day_diff=round(day_diff)
if day_diff < 0:
return ''
if day_diff == 0:
if second_diff < 10:
return "just now"
if second_diff < 60:
return str(second_diff) + " seconds ago"
if second_diff < 120:
return "a minute ago"
if second_diff < 3600:
return str( second_diff / 60 ) + " minutes ago"
if second_diff < 7200:
return "an hour ago"
if second_diff < 86400:
return str( second_diff / 3600 ) + " hours ago"
if day_diff == 1:
return "Yesterday"
if day_diff < 7:
# return str(day_diff) + " days ago"
return str(int(round(day_diff,1))) + " days ago"
if day_diff < 31:
# return str(day_diff/7) + " weeks ago"
# return str(int(round(day_diff/7,1))) + " weeks ago"
ans=int(round(day_diff/7,1))
if ans>1:
return str(ans) + " weeks ago"
else:
return str(ans) + " week ago"
if day_diff < 365:
# return str(day_diff/30) + " months ago"
# return str(int(round(day_diff/30,1))) + " months ago"
ans=int(round(day_diff/30,1))
if ans>1:
return str(ans) + " months ago"
else:
return str(ans) + " month ago"
# return str(day_diff/365) + " years ago"
ans=int(round(day_diff/365,1))
if ans>1:
return str(ans) + " years ago"
else:
return str(ans) + " year ago"
def remove_tag(tag,s):
return s.replace(f'<{tag}>','').replace(f'</{tag}>','').strip()
# with open('videos.xml',encoding='utf-8') as fid:
# # xml=fid.read()
# # tree = etree.parse(StringIO(fid))
# # tree=etree.fromstring(fid.read())
# s=fid.read()
# print(s)
# ans=re.findall(r'<entry>.*</entry>',s)
# pat=r'(?<=<entry>)[\n]+.+?(?=/)'
URL_REGEX=r'https?:[^\n^"^\?]*'
# vids={}
def read_yt_xml_channel_feed(s):
vids=[]
entry0=False
author=''
for l in s.splitlines():
# print(l)
if entry0:
if '<yt:videoId>' in l:
# url=re.findall(URL_REGEX,l)[0]
# vids[-1]['videoId']=url.replace('https://www.youtube.com/v/','')
# ['lengthSeconds']
vids.append({'videoId':remove_tag('yt:videoId',l)})
elif '<title>' in l:
vids[-1]['title']=remove_tag('title',l)
vids[-1]['lengthSeconds']=0
# vids[-1]['publishedText']=''
vids[-1]['author']=author
# list(vids)[-1]
# vids[-1]=t
# vids[t]=0
# vids.append({'title':t})
elif '<published>' in l:
pub=remove_tag('published',l)
y,m,dd=pub.split('-')
d,hh=dd.split('T')
h,mi,ss,zz=hh.split(':')
dt = datetime(int(y), int(m), int(d), int(h), int(mi))
# print(dt)
# print(time_ago(dt.timestamp()))
vids[-1]['publishedText']=time_ago(dt.timestamp())
elif '<media:statistics' in l:
views=re.findall(r'[0-9]+',l)[0]
vids[-1]['viewCount']=int(views)
else:
if '<title>' in l:
author=remove_tag('title',l)
elif '<entry>' in l:
entry0=True
# print(vids)
return vids
# print(tree.getroot())