|
1 | | -from tiktokapipy.api import TikTokAPI |
| 1 | +import asyncio |
2 | 2 | import csv |
3 | | -from feedgen.feed import FeedGenerator |
4 | 3 | from datetime import datetime, timezone |
| 4 | +from feedgen.feed import FeedGenerator |
| 5 | +from tiktokapipy.async_api import AsyncTikTokAPI |
5 | 6 |
|
6 | 7 | # Now using a new TikTok library https://github.com/Russell-Newton/TikTokPy |
7 | 8 |
|
|
13 | 14 |
|
14 | 15 | maxItems = 5 |
15 | 16 |
|
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'] |
21 | 17 |
|
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'])]) |
23 | 23 |
|
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') |
33 | 24 |
|
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}\'') |
36 | 28 |
|
| 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') |
37 | 38 |
|
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 |
63 | 41 |
|
64 | | - fg.updated(updated) |
| 42 | + async with AsyncTikTokAPI(navigation_retries=3, navigation_timeout=60) as api: |
| 43 | + tiktokuser = await api.user(csvuser, video_limit=maxItems) |
| 44 | + async for video in tiktokuser.videos: |
| 45 | + # print(video.create_time, video.desc) |
| 46 | + print("URL = " + "https://tiktok.com/@" + csvuser + "/video/" + str(video.id)) |
| 47 | + fe = fg.add_entry() |
| 48 | + link = "https://tiktok.com/@" + csvuser + "/video/" + str(video.id) |
| 49 | + fe.id(link) |
| 50 | + ts = video.create_time |
| 51 | + print(ts) |
| 52 | + fe.published(ts) |
| 53 | + fe.updated(ts) |
| 54 | + updated = max(ts, updated) if updated else ts |
| 55 | + if video.desc: |
| 56 | + fe.title(video.desc[0:255]) |
| 57 | + else: |
| 58 | + fe.title("No Title") |
| 59 | + fe.link(href=link) |
| 60 | + # fe.description("<img src='" + tiktok.as_dict['video']['cover'] + "' />") |
| 61 | + if video.desc: |
| 62 | + fe.description(video.desc) |
| 63 | + else: |
| 64 | + fe.description( "No Description") |
| 65 | + #print(fg.rss_str(pretty=True)) |
65 | 66 |
|
66 | | - fg.atom_file('rss/' + csvuser + '.xml', pretty=True) # Write the RSS feed to a file |
67 | | - except Exception: |
68 | | - print("Some error") |
| 67 | + fg.updated(updated) |
| 68 | + fg.atom_file('rss/' + csvuser + '.xml', pretty=True) # Write the RSS feed to a file |
| 69 | + except Exception as e: |
| 70 | + print(f"Some error: {e}") |
69 | 71 | pass |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | +asyncio.run(runAll()) |
0 commit comments