This post has been de-listed
It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.
My function does the following:
- Reading a CSV file named
post_info.csv
using Pandas and extracting two lists,post_ids
andheadings
. - Looping through the list of post IDs and using the Reddit API to retrieve the corresponding post.
- For each post, if the link to the subreddit wiki is not found in the selftext, it logs a message and skips to the next post.
- If the link to the subreddit wiki is found, the function loops through the list of headings and uses regular expressions to search for a specific pattern in the selftext.
- If the pattern is found, the function replaces it with a link to the comments section of the post using the
title_id_dict
to look up the post ID from the heading. - The function then updates the post with the modified selftext. If an error occurs, it logs the error.
The input that the function receives is:
- [Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/about/wiki/2/#wiki_piping_.7C_cool_stuff)
The output that is in the edited post is:
- [Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/comments/10sjl4s/)|[Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/comments/10sjl4s/)
I'm expecting this here instead of that output: - [Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/comments/10sjl4s/)
I strongly suspect that the lines from pattern =
until post_content =
are causing the problem because during debug the value of the post_link
is the correct '[Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/comments/10sjl4s/)'
thingy.
The value of post_content
before the line post_content = re.sub etc.
:
'Reddit loves animals. Reddit loves strangeness. Here, we have both! So why doesn’t r/aardvarks have more love? Find aardvarks, post them there. Please. Update this post here. Please. And again for debugging. And again.\n\nExample for how to use the cheatsheet: \n- [Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/about/wiki/2/#wiki_piping_.7C_cool_stuff)\n\n#Subheading\n\nHow about a subheading?'
The value of that same variable after the line:
'Reddit loves animals. Reddit loves strangeness. Here, we have both! So why doesn’t r/aardvarks have more love? Find aardvarks, post them there. Please. Update this post here. Please. And again for debugging. And again.\n\nExample for how to use the cheatsheet: \n- [Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/comments/10sjl4s/)|[Piping | Cool stuff](https://www.reddit.com/r/EncyclopaediaOfReddit/comments/10sjl4s/)\n\n#Subheading\n\nHow about a subheading?'
And here would be the functions that do the conversion.
def wiki_to_post_link(reddit, title_id_dict):
df = pd.read_csv(f'post_info.csv')
post_ids = df['ID'].tolist()
headings = df['Title'].tolist()
for i in range(len(post_ids)):
post = reddit.submission(id=post_ids[i])
post_content = post.selftext
if "https://www.reddit.com/r/EncyclopaediaOfReddit/about/wiki" not in post_content:
log.info(f"Links in '{headings[i]}' already converted, skipping...")
continue
for heading in headings:
converted_heading = url_encoding(heading)
pattern = re.compile(f'\\[{heading}\\]\\(https://www.reddit.com/r/EncyclopaediaOfReddit/about/wiki/[0-9] /#wiki_{converted_heading}\\)')
post_link = f'[{heading}](https://www.reddit.com/r/EncyclopaediaOfReddit/comments/{title_id_dict[heading]}/)'
post_content = re.sub(pattern, post_link, post_content)
reddit.validate_on_submit = True
try:
post.edit(post_content)
log.info(f"Wiki links converted for '{headings[i]}'")
except Exception as e:
log.error(f"Error updating post for '{headings[i]}'. Likely post has been deleted. Error: {e}")
And the function that converts the wiki headings that contain special characters is this here:
def url_encoding(heading):
heading = heading.lower()
heading = heading.replace(' ', '_')
heading = heading.replace('/', '.2F')
heading = heading.replace('\\', '.5C')
heading = heading.replace('?', '.3F')
heading = heading.replace('!', '.21')
heading = heading.replace('“', '.201C')
heading = heading.replace('”', '.201D')
heading = heading.replace('"', '.22')
heading = heading.replace("'", '.27')
heading = heading.replace('’', '.2019')
heading = heading.replace('`', '.60')
heading = heading.replace('@', '.40')
heading = heading.replace(':', '.3A')
heading = heading.replace(';', '.3B')
heading = heading.replace('(', '.28')
heading = heading.replace(')', '.29')
heading = heading.replace(',', '.2C')
heading = heading.replace('#', '.23')
heading = heading.replace('~', '.7E')
heading = heading.replace('$', '.24')
heading = heading.replace('%', '.25')
heading = heading.replace('&', '.26amp.3B')
heading = heading.replace(' ', '.2B')
heading = heading.replace('<', '.26lt.3B')
heading = heading.replace('>', '.26gt.3B')
heading = heading.replace('=', '.3D')
heading = heading.replace('{', '.7B')
heading = heading.replace('}', '.7D')
heading = heading.replace('[', '.5B')
heading = heading.replace(']', '.5D')
heading = heading.replace('^', '.5E')
heading = heading.replace('|', '.7C')
return heading
Subreddit
Post Details
- Posted
- 1 year ago
- Reddit URL
- View post on reddit.com
- External URL
- reddit.com/r/redditdev/c...