Skip to content

Commit 7097c55

Browse files
committed
async
1 parent e3b44fa commit 7097c55

File tree

2 files changed

+57
-49
lines changed

2 files changed

+57
-49
lines changed

postprocessing.py

Lines changed: 55 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
from tiktokapipy.api import TikTokAPI
1+
import asyncio
22
import csv
3-
from feedgen.feed import FeedGenerator
43
from datetime import datetime, timezone
4+
from feedgen.feed import FeedGenerator
5+
from tiktokapipy.async_api import AsyncTikTokAPI
56

67
# Now using a new TikTok library https://github.com/Russell-Newton/TikTokPy
78

@@ -13,57 +14,63 @@
1314

1415
maxItems = 5
1516

16-
with open('subscriptions.csv') as f:
17-
cf = csv.DictReader(f, fieldnames=['username'])
18-
try:
19-
for row in cf:
20-
csvuser = row['username']
2117

22-
print(f'Running for user \'{csvuser}\'')
18+
async def runAll():
19+
with open('subscriptions.csv') as f:
20+
# TODO: Switch to 3.11 TaskGroup or trio nursery
21+
await asyncio.gather(*[
22+
run(row['username']) for row in csv.DictReader(f, fieldnames=['username'])])
2323

24-
fg = FeedGenerator()
25-
fg.id('https://tiktok.com/@' + csvuser)
26-
fg.title(csvuser + ' TikTok')
27-
fg.author( {'name':'Conor ONeill','email':'[email protected]'} )
28-
fg.link( href='http://tiktok.com', rel='alternate' )
29-
fg.logo(ghPagesURL + 'tiktok-rss.png')
30-
fg.subtitle('OK Boomer, all the latest TikToks from ' + csvuser)
31-
fg.link( href=ghPagesURL + 'rss/' + csvuser + '.xml', rel='self' )
32-
fg.language('en')
3324

34-
# Set the last modification time for the feed to be the most recent post, else now.
35-
updated=None
25+
async def run(csvuser):
26+
try:
27+
print(f'Running for user \'{csvuser}\'')
3628

29+
fg = FeedGenerator()
30+
fg.id('https://tiktok.com/@' + csvuser)
31+
fg.title(csvuser + ' TikTok')
32+
fg.author( {'name':'Conor ONeill','email':'[email protected]'} )
33+
fg.link( href='http://tiktok.com', rel='alternate' )
34+
fg.logo(ghPagesURL + 'tiktok-rss.png')
35+
fg.subtitle('OK Boomer, all the latest TikToks from ' + csvuser)
36+
fg.link( href=ghPagesURL + 'rss/' + csvuser + '.xml', rel='self' )
37+
fg.language('en')
3738

38-
with TikTokAPI() as api:
39-
# with TikTokAPI(navigation_retries=3, navigation_timeout=60) as api:
40-
tiktokuser = api.user(csvuser, video_limit=maxItems)
41-
for video in tiktokuser.videos:
42-
# print(video.create_time, video.desc)
43-
print("URL = " + "https://tiktok.com/@" + csvuser + "/video/" + str(video.id))
44-
fe = fg.add_entry()
45-
link = "https://tiktok.com/@" + csvuser + "/video/" + str(video.id)
46-
fe.id(link)
47-
ts = video.create_time
48-
print(ts)
49-
fe.published(ts)
50-
fe.updated(ts)
51-
updated = max(ts, updated) if updated else ts
52-
if video.desc:
53-
fe.title(video.desc[0:255])
54-
else:
55-
fe.title("No Title")
56-
fe.link(href=link)
57-
# fe.description("<img src='" + tiktok.as_dict['video']['cover'] + "' />")
58-
if video.desc:
59-
fe.description(video.desc)
60-
else:
61-
fe.description( "No Description")
62-
#print(fg.rss_str(pretty=True))
39+
# Set the last modification time for the feed to be the most recent post, else now.
40+
updated=None
6341

64-
fg.updated(updated)
42+
async with AsyncTikTokAPI() as api:
43+
# with TikTokAPI(navigation_retries=3, navigation_timeout=60) as api:
44+
tiktokuser = await api.user(csvuser, video_limit=maxItems)
45+
async for video in tiktokuser.videos:
46+
# print(video.create_time, video.desc)
47+
print("URL = " + "https://tiktok.com/@" + csvuser + "/video/" + str(video.id))
48+
fe = fg.add_entry()
49+
link = "https://tiktok.com/@" + csvuser + "/video/" + str(video.id)
50+
fe.id(link)
51+
ts = video.create_time
52+
print(ts)
53+
fe.published(ts)
54+
fe.updated(ts)
55+
updated = max(ts, updated) if updated else ts
56+
if video.desc:
57+
fe.title(video.desc[0:255])
58+
else:
59+
fe.title("No Title")
60+
fe.link(href=link)
61+
# fe.description("<img src='" + tiktok.as_dict['video']['cover'] + "' />")
62+
if video.desc:
63+
fe.description(video.desc)
64+
else:
65+
fe.description( "No Description")
66+
#print(fg.rss_str(pretty=True))
6567

66-
fg.atom_file('rss/' + csvuser + '.xml', pretty=True) # Write the RSS feed to a file
67-
except Exception:
68-
print("Some error")
68+
fg.updated(updated)
69+
fg.atom_file('rss/' + csvuser + '.xml', pretty=True) # Write the RSS feed to a file
70+
except Exception as e:
71+
print(f"Some error: {e}")
6972
pass
73+
74+
75+
76+
asyncio.run(runAll())

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
tiktokapipy
1+
asyncio
22
feedgen
3+
tiktokapipy
34

0 commit comments

Comments
 (0)