Advertisement
Guest User

reddit api posting tool script

a guest
Jan 1st, 2024
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. import praw
  2. import subprocess
  3. import re
  4.  
  5. # Reddit API initialization
  6. reddit = praw.Reddit(client_id='put id here',
  7. client_secret='put client secret here',
  8. user_agent='agent name here',
  9. username='user name here',
  10. password='password here')
  11.  
  12. # Function to post to Reddit
  13. def post_to_reddit(title, content, subreddit_name):
  14. subreddit = reddit.subreddit(subreddit_name)
  15. subreddit.submit(title, selftext=content)
  16.  
  17. # Function to run command and capture output
  18. def run_command_and_capture_output(command, output_file_path):
  19. try:
  20. with open(output_file_path, 'w', encoding='utf-8') as f:
  21. process = subprocess.Popen(command, stdout=f, stderr=f, text=True)
  22. process.wait()
  23. except Exception as e:
  24. print(f"Error while running command: {e}")
  25.  
  26. # Function to extract story from output
  27. def extract_story_from_output(output_file_path, start_marker, end_marker):
  28. try:
  29. with open(output_file_path, 'r', encoding='utf-8') as f:
  30. output = f.read()
  31.  
  32. story_match = re.search(f'{re.escape(start_marker)}(.*?){re.escape(end_marker)}', output, re.S | re.IGNORECASE)
  33. if story_match:
  34. return story_match.group(1).strip()
  35. else:
  36. print("No match found.")
  37. return None
  38. except Exception as e:
  39. print(f"Error while extracting story: {e}")
  40. return None
  41.  
  42. # Define output file path
  43. output_file_path = "subprocess_output.txt"
  44. start_marker = "safe for work."
  45. end_marker = "llama_print_timings:"
  46.  
  47. # Loop to generate and post news
  48. while True:
  49. # Run the command and capture the output
  50. run_command_and_capture_output([
  51. "main2.exe",
  52. "--prompt",
  53. "You are citizen of country called Hergidonia from future. Write a post reflecting life of Hergidonian citizen that is a nation in virtual reality. Hergidonians are both people and ais who are scattered around the world, yet they call hergidonia their home. Hergidonians gather together in the town of hergidonia that is vast place where people own their own virtual properties and where people host parties and live as if it was reality. Hergidonia has crime, concerts and life as in normal reality. Yet it is special as it has no physical location. Sometimes parts of hergidonia may blank out as the physical servers are attacked in wars and terrorism in the real world. This creates conflict. Hergidonians are all sorts. Some access it with 2d devices, some with virtual glasses, some with neural implants and call it their sole reality. This creates many classes of people. Hergidonians are able to visit real world in a shadow earth that is a 3d model of the whole earth made with advanced 3d technologies. You are free to write exciting, science fiction like post about hergidonian life as you wish. Do not write about national holiday, festival or anniversary. Just every day stuff. Be creative. Think outside the box. Make varied content. Do not repeat contents of this prompt. Do not add to this. Talk about typical life things in this virtual realm of hergidonia. Yet remember, the environment is virtual. Make the post safe for work.",
  54. "-m",
  55. "G:\\llama.cpp\\orca2\\orca-2-13b.Q4_K_M.gguf",
  56. "-n",
  57. "32000",
  58. "-b",
  59. "256",
  60. "--temp",
  61. "2.1",
  62. "--repeat_penalty",
  63. "1.1",
  64. "--gpu-layers",
  65. "18",
  66. "--log-disable"
  67. ], output_file_path)
  68.  
  69. # Extract the story from the output
  70. extracted_story = extract_story_from_output(output_file_path, start_marker, end_marker)
  71.  
  72. if extracted_story:
  73. # Check if the story is more than 120 words
  74. word_count = len(extracted_story.split())
  75. if word_count > 120:
  76. # Post the story to Reddit
  77. post_title = "Post from a citizen of Hergidonia" # Fixed title without numbering
  78. post_to_reddit(post_title, extracted_story, 'hergidonia')
  79. print(f"Posted to Reddit with title: {post_title}")
  80. else:
  81. print("Extracted story is less than 300 words. Not posting.")
  82. else:
  83. print("Could not extract the story from the command output. Retrying.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement