Skip to content

Instantly share code, notes, and snippets.

@thehowl
Created May 7, 2015 13:39
Show Gist options
  • Save thehowl/2c890ecdf0cacbb956b7 to your computer and use it in GitHub Desktop.
Save thehowl/2c890ecdf0cacbb956b7 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import json, threading, urllib.request, time, sys, traceback
import tweepy
auth = tweepy.OAuthHandler("Key", "Key")
auth.set_access_token("Key", "Key")
api = tweepy.API(auth)
# opens a file, reads it and closes it
def fgc(fto):
s = open(fto, "r")
a = s.read()
s.close()
return a
# fpc to put stuff into a file
# param fte: File To Edit (got the acronym again?)
# param contents: the contents to put inside the stuff
def fpc(fte, contents):
# clear it and open it in write mode
s = open(fte, "w")
s.write(contents)
s.close()
return True
# The queue of tweets to send, updated by the main thread every 5 seconds
tweetqueue = ["[debug note] Tweet sending: it works!"]
def fileUpdaterAndChecker():
while True:
try:
print("[fup&c] Starting it again...")
f = urllib.request.urlopen("http://openings.moe/api/list.php").read().decode('utf-8')
oldfile = fgc("data.json")
if f != oldfile:
dic = []
for item in json.loads(oldfile):
dic.append(item["file"])
for item in json.loads(f):
if not (item["file"] in dic):
print("[fup&c] New item found! %s" % item["file"])
tweetqueue.append("New video! %s from %s \nhttp://openings.moe/?video=%s" % (item["title"], item["source"], item["file"]))
fpc("data.json", f)
time.sleep(600)
except Exception as detail:
print(detail)
traceback.print_exc(file=sys.stdout)
fCheckThread = threading.Thread(target=fileUpdaterAndChecker)
fCheckThread.daemon = True
fCheckThread.start()
while True:
try:
element = tweetqueue.pop(0)
print("[qelab] Sending tweet: %s" % element)
api.update_status(status=element)
except Exception as detail:
#print(detail)
#traceback.print_exc(file=sys.stdout)
pass
time.sleep(10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment