How to have a script pull from a txt file instead of coding it in the actual script

vinnypolston

Boot Camp
Joined
Oct 7, 2014
Messages
124
Likes
151
Degree
1
Hey guys! Been a long time. I have been learning Python. I have built a script that will automatically log into my Facebook account(s) and browse to a list of Facebook groups I am a member of at random intervals; which then submits whatever post I have set, one by one. The process is slower but it is as if I am actually doing it manually. Did this to look natural.

The idea is automating traffic leaking from Facebook groups (that I am not an admin of). I've done this manually in the past and was able to get roughly 30k visitors to a website I was doing for testing social traffic; made about $100 from it. I proved the concept and was happy, learned a lot. I dropped that project because sitting there manually logging in and out of five Facebook accounts and posting to the 10 groups was mind-numbingly tedious; and to do it slow enough to not draw attention from Facebook took a couple hours. Decided to learn programming python so I could automate and then tackle it again.

Anyways. Obviously the message each day I post will be different. What I would like to do is just have the script pull what I want to post from a txt file instead of being within the actual script. Any ideas?

Thanks! Have googled it but for some reason my brain isn't getting it.
 
I have built a script that will automatically log into my Facebook account(s) and browse to a list of Facebook groups I am a member of at random intervals;
[..]
What I would like to do is just have the script pull what I want to post from a txt file instead of being within the actual script. Any ideas?

Do you have the script on a local machine or server? Either way it makes sense to have it pull a file just by opening it with Python. https://stackoverflow.com/questions/7409780/reading-entire-file-in-python

I would recommend having the script read a JSON file that updates with new content and targets, so you can organize better: https://stackoverflow.com/questions/22366748/reading-json-files-from-curl-in-python

I don't code in python, but the code looks simple enough to understand the basics of getting started with it.

P.S. for what it's worth, I don't necessarily recommend traffic leaking in this manner unless you go into your account and manually reply to users and engage - otherwise after 3-4 posts several users will realize you are a bot very very quickly, especially if you never reply to comments - something to think about. (If you don't want to interact with users then you are just spamming, not traffic leaking - traffic leaking's ultimate goal is to build you up as an authority within that group so people will gravitate towards you and your site more and more often).
 
Anyways. Obviously the message each day I post will be different. What I would like to do is just have the script pull what I want to post from a txt file instead of being within the actual script. Any ideas?

Thanks! Have googled it but for some reason my brain isn't getting it.

To expand on what CCarter said, simple use case is to just use some kind of data file. JSON works well for basically any kind of data and is everywhere. So lets say you have a JSON file that looks like this:

Code:
{
    "data": [{
        "date": "somedate",
        "message": "Hi guys this is a sweet facebook group eh?"
    },
    {
        "date": "anotherdate",
        "message": "Hi guys! Check out my sweet site eh!"
    }]
}

Use CCarters method or check out the documentation here:

Code:
f = open('somefile', 'r')
file_contents = f.read()

Now you have all the file data in the file_contents variable. Now you need to convert the JSON to a Python dictionary.

Code:
import json
json_data = json.loads(file_contents)

Now you can access the JSON data through the Python dictionary!

Really though this was just a cock tease. There is still a mountain of things you need to do to actually automate this and then post your message to Facebook.

You should get familiar with the Requests library. It's really the easiest way to make web requests with Python. Because from here on out your goal is to break down all the requests in the process and pretend you are a desktop browser. So:
  • Figure out how to log in to Facebook using requests. Pull up your browsers dev tools and get to work in the Networking tab looking at the requests and what data is passed during login. Then replicate it in your Python script.
  • Figure out how to spoof your requests so you look like a browser and not like a bot. This means setting proper user agents and limiting how fast your bot moves through the site.
  • Figure out how to post to the Facebook group using web requests. Basically same process as the login one.
Then if you want to go deeper:
  • How to scale by using multiple accounts.
  • How to set up and connect through proxies.
If you don't want to use the Requests library mentioned above, look at PhantomJS or similar project. These type of browser projects are really great for javascript / ajax heavy websites.
 
Last edited:
I'd go a different route than using a plain .txt file. There is no structure there. @CCarter recommended using JSON and that's definitely a step in the right direction.

If it were me, I'd create a small in-house app, store the posts in a Database and use forms to update/maintain the data. You probably don't want to deal with a raw JSON file yourself as far as creating the posts. Since you're using Python, I'd recommend reading up on the Django framework (https://www.djangoproject.com). Once you get past the learning curve, you can build this type of thing in a couple of hours. By starting your own in-house system and taking it slowly, you'll get yourself out of the "bucket of scripts" mindset and get more into developing sophisticated software. The investment in time will pay you back, don't let anyone tell you otherwise.
 
@CCarter , Running it on a local machine. Thanks for the help. Looks like concensus is going the JSON route. Yeah, I 100% agree. Not looking to spam, just wanting to automate the repetitive part.

@Rageix , Thanks a ton. I'll start going over the requests library. Right now I'm just using pyautogui; not looking to create anything massive.

@SmokeTree , You're right! Django is what I'm planning on tackling after I get Python itself down a bit better.
 
Back