Hello and welcome,
in this tutorial i will take things slowly. I will not edit out errors from the video instead i will just fix them to show you what are some common errors and give you a better understanding of how this code works. If you have any questions make sure you leave them down below in the comments.
You can visit my website for text version or my Youtube channel for more videos:
hoxframework.com.hr / youtube.com/hoxframework
Also keep in mind that free APIs aren’t great so i recommend downloading a bunch of pictures at once and never download them again, it will be a lot easier for you. To automate this process is easy you just have to import time or any sort of counter that you want, else you can just add your python program to windows event scheduler to run every hour. To make this bot operate even better i recommend:
-Downloading a bunch of pictures and facts at once so you dont have to redownload every time you post
-make a cleaner.py to clean up pictures you posted or store them in a folder where you can compare the posted pics with newly downloaded (Im guessing its possible without machinelearning it has to be…)
-Make the thing automatic
-make an interface for it?
-and whatever else you like 😀
Thank you so much ! Have a nice day.
DogsDownloader.py:
import urllib.request
import requests
import json
i = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
for k in i:
print(‘Beginning file download with urllib2…Operation {0} out of 30’.format(k))
url = ‘https://some-random-api.ml/img/dog’
url = requests.get(url)
url = json.loads(url.text)
url = url[‘link’]
response = requests.get(url) if response.status_code == 200: with open("./pets/doggos/dog{}.jpg".format(k), 'wb') as f: f.write(response.content) req = requests.get("https://some-random-api.ml/facts/dog") qoute = req.text key = json.loads(qoute) #print("[+] Qoute:\n",key['fact']) myqoute = key['fact'] with open('./pets/doggofactos/dog{}.txt'.format(k), 'w+') as file: file.write(myqoute) file.close() print("[*] DONE !")
TweetMe.py:
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
def goTweet(textfile,imagefile):
# Printing out some basic stuff
#print(“[+] Tweeting : “,textfile,”\nAnd pic:”,imagefile,”\n”)
# Loading the APIs with open('accesstoken.txt', 'r') as file: file = str(file.read()) file = file.replace("'","") access_token = file with open('accesstokensecret.txt','r') as secret: secret = str(secret.read()) secret = secret.replace("'","") access_token_secret = secret with open('consumerapi.txt','r') as consumer: consumer = str(consumer.read()) consumer = consumer.replace("'","") consumer_api = consumer with open('consumerapisecret.txt','r') as consumersecret: consumersecret = str(consumersecret.read()) consumersecret = consumersecret.replace("'","") consumer_api_secret = consumersecret #here you can just use regular variables #like: #my_api = "dmiad922dn82n1dbu" #for each # Authentication attempt def OAuth(): try: auth = tweepy.OAuthHandler(consumer_api, consumer_api_secret) auth.set_access_token(access_token, access_token_secret) return auth except Exception as e: return None oauth = OAuth() api = tweepy.API(oauth) imagefile = imagefile.replace(" ","") #api.update_status('testtweet using API') api.update_with_media(imagefile, textfile) print("image:",imagefile,"\nText:", textfile) #print("[+] SUCCESS ! Tweet posted.")
Main.py:
import tweetMe
import random
import os
def doggettextfile():
path = “./pets/doggofactos/”
global dogfact
dogfact = os.listdir(path)
rndm = random.choice(dogfact)
dogfact = path + rndm
global doggofacto
with open(dogfact, ‘r’) as doggofacto:
doggofacto = doggofacto.read()
return doggofacto
def doggetimgfile():
global dogimage
path = ‘./pets/doggos/’
dogimage = os.listdir(path)
dogimage = random.choice(dogimage)
dogimage = path + dogimage
return dogimage
getimg = doggetimgfile()
getfact = doggettextfile()
totweet = tweetMe.goTweet(getfact, getimg)
Thanks for visiting 🙂