Exemplo n.º 1
0
class Message():
	def __init__(self,username, password):
		self.launcher = Launcher(username, password)
		self.driver = self.launcher.login_mobile()
		self.es = Es_fb()
		self.list = []
		self.update_time = int(time.time())

	def date2timestamp(self, date):
		date = date.replace(u'月', '-').replace(u'日', '').replace(' ', '')
		if date == '刚刚':
			timestamp = int(time.time())
			return timestamp
		if u'上午' in date:
			date = date.replace(u'上午', ' ')
		if u'下午' in date:
			if date.split(u'下午')[1].split(':')[0] == '12':
				date = date.replace(u'下午', ' ')
			elif eval(date.split(u'下午')[1].split(':')[0]) < 12:
				date = date.split(u'下午')[0] + ' ' + str(eval(date.split(u'下午')[1].split(':')[0])+12) + ':' + date.split(u'下午')[1].split(':')[1]
		if u'年' not in date and u'分钟' not in date and u'小时' not in date:
			date = str(time.strftime('%Y-%m-%d', time.localtime(time.time())).split('-')[0]) + '-' + date
		if u'年' in date and u'分钟' not in date and u'小时' not in date:
			date = date.replace(u'年', '-')
		if u'分钟' in date:
			timestamp = int(time.time()) - int(re.search(r'(\d+)', date).group(1)) * 60
			return timestamp 
		if u'小时' in date:
			timestamp = int(time.time()) - int(re.search(r'(\d+)', date).group(1)) * 60 * 60
			return timestamp

		try:
			timestamp = int(time.mktime(time.strptime(date, '%Y-%m-%d')))
		except:
			timestamp = int(time.mktime(time.strptime(date, '%Y-%m-%d %H:%M')))
		return timestamp

	def get_list(self):
		self.driver.get('https://m.facebook.com/messages/t/')

		sx_list = []
		for each in self.driver.find_elements_by_xpath('//div[@id="root"]/div[1]/div[2]/div/table'):
			try:
				author_name = each.find_element_by_xpath('./tbody/tr/td/div/h3[1]').text
			except:
				author_name = 'None'
			print author_name

			try:
				author_id = ''.join(re.findall(re.compile('%3A(\d+)#'),each.find_element_by_xpath('./tbody/tr/td/div/h3[1]/a').get_attribute('href')))
			except:
				author_id = 'None'
			print author_id

			try:
				message_url = each.find_element_by_xpath('./tbody/tr/td/div/h3[1]/a').get_attribute('href')
			except:
				message_url = False
			print message_url

			if message_url:
				sx_list.append({'author_name':author_name, 'message_url':message_url, 'author_id':author_id})
		return sx_list

	def get_message(self):
		sx_list = self.get_list()
		for sx in sx_list:
			self.driver.get('https://m.facebook.com/profile.php?id=' + str(sx['author_id']))
			try:
				photo_url = self.driver.find_element_by_xpath('//div[@id="m-timeline-cover-section"]/div[1]/div[2]/div[1]/div/a/img').get_attribute('src')
			except:
				try:
					photo_url = self.driver.find_element_by_xpath('//div[@id="m-timeline-cover-section"]/div[2]/div/div[1]/div[1]/a/img').get_attribute('src')
				except:
					photo_url = self.driver.find_element_by_xpath('//div[@id="m-timeline-cover-section"]/div[2]/div/div[1]/a/img')
			sx['photo_url'] = photo_url


		for sx in sx_list:
			self.driver.get(sx['message_url'])
			time.sleep(1)

			for message in self.driver.find_elements_by_xpath('//div[@id="messageGroup"]/div/div'):
				try:
					date = message.find_element_by_xpath('./div[2]/abbr').text
				except:
					break
				print date

				try:
					messageTime = self.date2timestamp(date)					
				except:
					messageTime = 0
				print messageTime

				try:
					#messageId = re.findall(re.compile('"fbid:(\d+)"'),message.find_element_by_xpath('./div/div').get_attribute('participants'))[-1]
					#if messageId == sx['author_id']:
					#	private_type = 'receive'
					#	text = message.text
					#	root_text = 'None'
					#else:
					#	private_type = 'make'
					#	text = 'None'
					#	root_text = message.text
					if re.findall(r'id=(\d+)&', message.find_element_by_xpath('./div[1]/a').get_attribute('href')):
						private_type = 'receive'
						text = message.text
						root_text = ''
					else:
						private_type = 'make'
						text = ''
						root_text = message.text
				except:
					private_type = 'unknown'
					text = message.text
					root_text = ''
			self.list.append({'uid':sx['author_id'], 'photo_url':sx['photo_url'], 'nick_name':sx['author_name'], 'timestamp':messageTime, 'update_time':self.update_time, 'text':text, 'root_text':root_text, 'private_type':private_type})

		self.driver.quit()
		return self.list

	def save(self, indexName, typeName, list):
		self.es.executeES(indexName, typeName, list)
Exemplo n.º 2
0
class Userinfo:
    def __init__(self, username, password):
        self.launcher = Launcher(username, password)

    def getUserinfo(self):
        driver = self.launcher.login_mobile()
        time.sleep(10)
        html = driver.page_source
        with open("Userinfo.html", "wb") as f:
            f.write(html)

        driver.find_element_by_xpath('//div[@role="navigation"]/a[2]').click()
        time.sleep(1)

        current_url = driver.page_source
        with open('Userinfo111.html', 'wb') as f:
            f.write(current_url)
        #driver.find_element_by_xpath('//a[@data-tab-key="about"]').click()
        try:
            driver.find_element_by_xpath(
                '//div[@id="m-timeline-cover-section"]/div[3]/a[1]').click()
        except:
            pass
        html = driver.page_source
        with open("Userinfo222.html", "wb") as f:
            f.write(html)
        time.sleep(1)
        #current_url = driver.page_source
        pattern0 = re.compile(';id=(\d+)&')
        id = re.findall(pattern0, current_url)[0]
        print id
        #eachs = [each for each in driver.find_elements_by_xpath('//div[@data-pnref="overview"]//li')]
        #career = eachs[0].text
        #location = eachs[2].text
        #pattern = re.compile(u'(\d+)年')
        #print(eachs[5].text)
        #birth = int(re.findall(pattern,eachs[5].text)[0])
        career = driver.find_element_by_xpath(
            '//div[@id="work"]/div/div[2]/div/div[2]/a/span').text
        if u'添加' in career:
            career = None
        print career
        try:
            location = driver.find_element_by_xpath(
                '//div[@id="living"]/div/div[2]/div[1]/div[@title="所在地"]/table/tbody/tr/td[2]/div/a'
            ).text
        except:
            location = None
        print location
        birth = int(
            driver.find_element_by_xpath(
                '//div[@id="basic-info"]/div/div[2]/div[@title="生日"]/table/tbody/tr/td[2]/div'
            ).text.replace(u'年',
                           '-').replace(u'月',
                                        '-').replace(u'日',
                                                     '').split('-')[0].strip())
        print birth
        today = int(time.strftime('%Y', time.localtime(int(time.time()))))
        age = today - birth
        print age
        discription = driver.find_element_by_xpath(
            '//div[@id="bio"]/div/div[2]/div/a/span').text
        if u'介绍一下自己吧' in discription:
            discription = None
        #driver.find_element_by_xpath('//ul[@data-testid="info_section_left_nav"]/li[6]/a').click()
        time.sleep(1)
        #description = driver.find_element_by_xpath('//div[@id="pagelet_bio"]/div/ul/li').text

        dict = {
            'id': id,
            'career': career,
            'location': location,
            'age': age,
            'description': discription
        }
        driver.quit()
        return dict
Exemplo n.º 3
0
class Operation():
    def __init__(self, username, password):
        print '11'
        self.launcher = Launcher(username, password)
        print '22'
        self.username = username
        self.password = password

    def publish(self, text):
        driver = self.launcher.login_mobile()
        time.sleep(20)
        html = driver.page_source
        with open("publish1.html", "wb") as f:
            f.write(html)
        driver.save_screenshot("publish1.png")
        driver.find_element_by_xpath('//input[@type="submit"]').click()
        print "publish111111"
        driver.find_element_by_xpath(
            '//textarea[@name="xc_message"]').send_keys(text)
        print "publish222222"
        driver.find_element_by_xpath('//input[@name="view_post"]').click()
        print "publish333333"

        time.sleep(60)

        html = driver.page_source
        with open("publish2.html", "wb") as f:
            f.write(html)
        driver.save_screenshot("publish.png")
        driver.quit()

        print "publish Success!!!!!!"

        return [True, '']

    def mention(self, username, text):
        driver = self.launcher.login()
        #driver,display = self.launcher.login()
        try:
            # 退出通知弹窗进入页面
            time.sleep(1)
            try:
                #driver.find_element_by_xpath('//div[@class="_n8 _3qx uiLayer _3qw"]').click()
                driver.find_element_by_xpath(
                    '//div[@class="_1k67 _cy7"]').click()
                print "mention Success11111111111111111111111"
            except BaseException, e:
                print "mention Position11111111111111111", e
                pass

            try:
                driver.find_element_by_xpath(
                    '//textarea[@title="分享新鲜事"]').click()
                driver.find_element_by_xpath(
                    '//textarea[@title="分享新鲜事"]').send_keys(text)
            except:
                driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
                ).click()
                driver.find_element_by_xpath(
                    '//div[@class="_1mwp navigationFocus _395 _1mwq _4c_p _5bu_ _34nd _21mu _5yk1"]'
                ).send_keys(text)
            time.sleep(2)
            try:
                driver.find_element_by_xpath(
                    '//table[@class="uiGrid _51mz"]/tbody/tr[1]/td[1]//a/div'
                ).click()
            except:
                driver.find_element_by_xpath(
                    '//table[@class="uiGrid _51mz _5f0n"]/tbody/tr[2]/td[2]//a/div'
                ).click()
            time.sleep(1)
            driver.find_element_by_xpath(
                '//input[@aria-label="你和谁一起?"]').send_keys(username)
            driver.find_element_by_xpath(
                '//input[@aria-label="你和谁一起?"]').send_keys(Keys.ENTER)
            time.sleep(1)
            try:
                driver.find_element_by_xpath(
                    '//button[@class="_1mf7 _4jy0 _4jy3 _4jy1 _51sy selected _42ft"]'
                ).click()
            except:
                driver.find_element_by_xpath(
                    '//button[@data-testid="react-composer-post-button"]'
                ).click()
            time.sleep(5)
            return [True, '']
        except Exception as e:
            return [False, e]