-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiktok2rss.py
executable file
·63 lines (57 loc) · 2.12 KB
/
tiktok2rss.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
#!/usr/bin/python
# tiktok2rss.py - Takes the desired username as first argument.
# Copyright (C) 2021 ysh <thinkingaboutshu@waifu.club>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import json
import requests
import sys
from datetime import datetime
from lxml import html
from html import escape
user = sys.argv[1]
url = 'https://www.tiktok.com/@' + user
headers = {
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0'}
page = requests.get(url, headers=headers)
tree = html.fromstring(page.text)
data_json = tree.xpath('//*[@id="SIGI_STATE"]/text()')[0]
data_dict = json.loads(data_json)
feed_description = feed_title = user + ' TikTok'
print("""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>{0}</title>
<description>{1}</description>
<link>{2}</link>""".format(feed_title, feed_description, url))
for tiktok_id in data_dict['ItemModule']:
tiktok = data_dict['ItemModule'][str(tiktok_id)]
title = tiktok['desc']
if not title:
title = 'New video from ' + user
guid = link = 'https://www.tiktok.com/@' + user + '/video/' + tiktok_id
date = datetime.fromtimestamp(int(tiktok['createTime'])).isoformat()
description = tiktok['music']['title'] + ' - ' + \
tiktok['music']['authorName']
print("""
<item>
<title>{0}</title>
<link>{1}</link>
<guid>{2}</guid>
<pubDate>{3}</pubDate>
<description>{4}</description>
</item>""".format(escape(title), link, guid, date, escape(description)))
print("""
</channel>
</rss>""")