Last active
December 21, 2015 14:09
-
-
Save vim13/6317518 to your computer and use it in GitHub Desktop.
エロいね!Twitter連動
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/local/bin/python | |
#vim:fileencoding=utf-8 | |
import re | |
import time | |
import urllib2 | |
from pit import Pit | |
import tweepy | |
import BeautifulSoup | |
def parseHtml(uri): | |
my_uri = uri | |
html = urllib2.urlopen(my_uri).read() | |
soup = BeautifulSoup.BeautifulSoup(html) | |
return soup | |
def getId(soup): | |
article = soup.findAll('article') | |
ids = [opt['id'] for opt in article] | |
return ids | |
def readFile(path): | |
opened_file = open(path, 'r') | |
logs = [] | |
for line in opened_file: | |
logs.append(line[:-1]) | |
opened_file.close() | |
return logs | |
def writeFile(path,ids): | |
opend_file = open(path, 'w') | |
for x in ids: | |
opend_file.write(str(x)+'\n') | |
opend_file.close() | |
def checkId(ids,logs): | |
new = [] | |
for x in ids: | |
if x not in logs: | |
new.append(x) | |
return new | |
def postTweet(title, api): | |
for x in title: | |
api.update_status('「エロいね!」しました'+ ' / '+ x['alt'].encode('utf-8') + ' ' + x['href'].encode('utf-8') + ' ' + x['src'].encode('utf-8')) | |
time.sleep(30) | |
def eroine(new,soup,api): | |
title = [] | |
for x in new: | |
select = soup.find('article',{'id':x}) | |
a = select.find('a',{'class':'pac'}) | |
if a: | |
href = a['href'] | |
src = re.search(u'src="(.*?)"', str(a)).group(1).decode('utf-8') | |
#80 | |
alt = re.search(u'alt="(.*?)"', str(a)).group(1).decode('utf-8')[:80] | |
dic = {'href':href,'src':src,'alt':alt} | |
title.append(dic) | |
postTweet(title,api) | |
if __name__ == '__main__': | |
key = Pit.get('twitter.com') | |
consumer_key = key['consumer_key'] | |
consumer_secret = key['consumer_secret'] | |
access_token = key['access_token'] | |
access_token_secret = key['access_token_secret'] | |
auth = tweepy.OAuthHandler(consumer_key, consumer_secret) | |
auth.set_access_token(access_token, access_token_secret) | |
api = tweepy.API(auth) | |
uri = 'http://sns.dmm.co.jp/member/463536' | |
soup = parseHtml(uri) | |
#ids = [u'activity_687684', u'activity_687681', u'activity_687680', u'activity_687679', u'activity_687678', u'activity_687677', u'activity_687676', u'activity_687675', u'activity_687674'] | |
ids = getId(soup) | |
path = 'ids.txt' | |
logs = readFile(path) | |
new = checkId(ids,logs) | |
writeFile(path,ids) | |
if new: | |
eroine(new,soup,api) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
タイトル(alt) 81文字以上スライス