Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # coding: utf-8
- # In[351]:
- try:
- get_ipython().system('pip install requests')
- get_ipython().system('pip install gql')
- except:
- pass
- # In[352]:
- import csv
- from random import shuffle
- import random
- from collections import defaultdict
- import html
- from datetime import datetime, timedelta
- from datetime import timezone
- import threading
- import requests
- import re
- import os
- import json
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- import threading
- # In[353]:
- def nested_dict(n, type):
- if n == 1:
- return defaultdict(type)
- else:
- return defaultdict(lambda: nested_dict(n-1, type))
- # In[354]:
- LP_URL = '56cashnow.com'
- GQL_URL = 'http://prisma:4466/'
- # In[355]:
- HEADERS = {
- 'Content-Type': 'application/json',
- 'Accept': 'application/json',
- 'Connection': 'keep-alive',
- 'DNT': '1',
- 'Accept-Encoding': 'gzip, deflate, br'
- }
- # In[356]:
- # EMAIL_SERVER = smtplib.SMTP_SSL('mail.wheelchairpd.com', 465)
- EMAIL_SERVER = smtplib.SMTP('mail.wheelchairpd.com', 2525)
- EMAIL_SERVER.login('email@wheelchairpd.com', 'Localhost@123')
- # EMAIL_SERVER = smtplib.SMTP('smtp', 25)
- # In[357]:
- cwd = os.getcwd() + '\\'
- # In[358]:
- db = nested_dict(1, list)
- file_list = ['keyword', 'geo', 'title', 'subtitle', 'head', 'welcome', 'text1', 'text2', 'text3', 'text4', 'text5', 'text6', 'cta']
- def read_text():
- for f in file_list:
- with open(f + '.csv') as csvfile:
- rows = csv.reader(csvfile, delimiter='\t')
- next(rows, None)
- for r in rows:
- try:
- db[f].append(r[0])
- except:
- continue
- read_text()
- # In[359]:
- def retrieve_blog_networks():
- query = """
- query metadatas {
- metadatas {
- id
- blogUrl
- blogNetwork
- secretEmail
- nextPostDate
- }
- }
- """
- data = {'query': query}
- data = json.dumps(data)
- r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
- res = r.content.decode('utf8')
- resJson = json.loads(res)
- return resJson['data']['metadatas']
- blog_networks = retrieve_blog_networks()
- # In[360]:
- def replace_kw(s, kw, url):
- rand = random.randint(0, 6)
- if rand == 0:
- r_text = '<a href="' + url + '" alt="' + kw +'"><strong>' + kw + '</strong></a>'
- elif rand == 1:
- r_text = '<a href="' + url + '" alt="' + kw +'"><i>' + kw + '</i></a>'
- elif rand == 2:
- r_text = '<a href="' + url + '" alt="' + kw +'"><u>' + kw + '</u></a>'
- elif rand == 3:
- r_text = '<a href="' + url + '" alt="' + kw +'">' + kw + '</a>'
- elif rand == 4:
- r_text = '<strong>' + kw + '</strong>'
- else:
- r_text = kw
- s = s.replace('{{keyword}}', ' ' + r_text + ' ')
- s = s.replace(' ', ' ')
- return s
- # In[361]:
- def clean_text(text):
- text = text.strip()
- text = text.replace("“", "'")
- text = text.replace("’", "'")
- text = text.replace("–", "-")
- text = text.replace("¬", "")
- return text
- # In[362]:
- def sanitize_url(keyword, date):
- keyword = keyword.lower()
- keyword = re.sub('[^a-zA-Z0-9 \n]', '', keyword)
- keyword = keyword.replace(' a ', ' ')
- keyword = keyword.replace(' an ', ' ')
- keyword = keyword.replace(' the ', ' ')
- keywords = keyword.split(' ')
- slug = keywords[0]
- for i in range(1, len(keywords)):
- if (len(slug + '-' + keywords[i]) > 40 and i == len(keywords) - 1) or (len(slug + '-' + keywords[i]) >= 40 and i < len(keywords) - 1):
- break
- else:
- slug = slug + '-' + keywords[i]
- print(slug)
- date_slug = '%d/%02d' % (date.year, date.month)
- return '/' + date_slug + '/' + slug + '.html'
- # In[363]:
- def get_related_url(blog_url, blog_network_id, link_type):
- if link_type == "internal":
- query = """
- query posts(
- $where: PostWhereInput!
- ) {
- posts(where: $where, first: 50, orderBy: updatedAt_DESC) {
- id
- blogUrl
- keyword
- postUrl
- blogNetwork
- isPublished
- updatedAt
- }
- }
- """
- variables = {
- "where": {
- "AND": {
- "blogUrl": blog_url,
- "isPublished": True
- }
- }
- }
- else:
- query = """
- query posts(
- $where: PostWhereInput!
- ) {
- posts(where: $where, first: 1000, orderBy: updatedAt_DESC) {
- id
- blogUrl
- keyword
- postUrl
- blogNetwork
- isPublished
- updatedAt
- }
- }
- """
- variables = {
- "where": {
- "AND": {
- "blogNetwork": blog_network_id,
- "isPublished": True
- }
- }
- }
- data = {'query': query, 'variables': variables}
- data = json.dumps(data)
- r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
- res = r.content.decode('utf8')
- resJson = json.loads(res)
- resJson = resJson['data']['posts']
- shuffle(resJson)
- return resJson
- # In[364]:
- def gen_related_list(blog_url, blog_network_id, link_type):
- resJson = get_related_url(blog_url, blog_network_id, link_type)
- n = min(random.randint(3, 10), len(resJson))
- content = '<ul>'
- for i in range(n):
- if resJson[i]['postUrl'] == None:
- resJson[i]['postUrl'] = ''
- url = 'https://' + resJson[i]['blogUrl'] + resJson[i]['postUrl']
- content += '<li><a href="' + url + '">' + resJson[i]['keyword'] + '</a>'
- content += '</ul>'
- return content
- # In[365]:
- def gen_post(keyword, blog_url, blog_network_id):
- keyword = clean_text(keyword)
- lp_url = 'https://' + LP_URL + '/?keyword=' + keyword + '&refurl=' + blog_url
- internal_links = gen_related_list(blog_url, blog_network_id, "internal")
- external_links = gen_related_list(blog_url, blog_network_id, "external")
- urls = get_related_url(blog_url, blog_network_id, "internal")
- s = {}
- for f in file_list:
- shuffle(db[f])
- if len(urls) > 0:
- if urls[0]['postUrl'] == None:
- urls[0]['postUrl'] = ''
- url = 'https://' + urls[0]['blogUrl'] + urls[0]['postUrl']
- else:
- url = lp_url
- s[f] = replace_kw(db[f][0], keyword, url)
- s[f] = s[f].replace('Get Started Now', '<a href="' + lp_url + '" alt="' + keyword +'"><strong><u>' + LP_URL + '</u></strong></a>')
- s[f] = s[f].replace('{{lp}}', '<a href="' + lp_url + '" alt="' + keyword +'"><strong><u>' + LP_URL + '</u></strong></a>')
- if f == 'text5':
- s[f] = s[f].replace('Get Started Now', '<a href="' + lp_url + '" alt="' + keyword +'"><strong><u>Get Started Now</u></strong></a>')
- text = '<h1>' + keyword + '</h1>'
- text += '<p><font style="font-size:25px; color:#f01701; font-weight:700; line-height:30px; margin-bottom:3px; display: inline-block;">'
- text += s['title']
- text += '</font></p>'
- text += '<p style="color: #3d85c6;">' + s['subtitle'] + '</p><br>'
- button = '<a href="' + lp_url + '" alt="' + keyword +'" style="color:#73261e;font-size: 18px;display: block;width: 80%;border: none;background-color: #FBB914;padding: 14px 28px;text-align: center; font-weight:700;"><span>>>> Click here to Apply Now »</span></a></button><br>'
- text += button
- text += '<p style="font-size:12px;">By clicking Apply Now, you agree to our Terms of Use and that you have read our Privacy Policy. Get Cash with <i>' + keyword +'</i>.</p>'
- text += '<p><a href="' + lp_url + '"><strong><u>>>> Get Your Dream Loans Now !.</u></strong></a></p><br>'
- text += '<p><h2 style="font-size: 25px; color:#E74C3C;">' + keyword + '</h2></p>'
- text += '<p><font style="font-size:20px; color:#205297; font-style:italic;">' + s['head'] + '</font></p><br>'
- text += '<p>' + s['welcome'] + '</p>'
- text += '<p>' + s['text1'] + '</p>'
- text += '<p>' + s['text2'] + '</p>'
- text += '<p>' + s['text3'] + '</p>'
- text += '<p>' + s['text4'] + '</p>'
- text += '<p>' + s['text5'] + '</p>'
- text += '<p><h3 style="font-size:25px; color:#205297; font-style:italic;">' + keyword + ' - ' + s['cta'] + '</h3></p>'
- text += '<p>' + internal_links + '</p>'
- text += '<p>' + s['text6'] + '</p><br>'
- text += '<p style="font-weight:700; text-align:center;">Don\'t Worry About Anything. We Try To Help You Happy.</p><br>'
- text += button + '<br><br>'
- if external_links != '':
- text += '<h3>Reference</h3>'
- text += '<p>' + external_links + '</p>'
- text = clean_text(text)
- # text = text.encode('utf-8', 'ignore')
- return str(text)
- # In[366]:
- def gen_keyword(keywords, geos):
- shuffle(keywords)
- geo = geos[random.randint(0, len(geos))].split(',')
- keyword = keywords[0].lower()
- if keyword.find(geo[0].lower()) >= 0:
- geo[0] = ''
- if keyword.find(geo[1].lower()) >= 0:
- geo[1] = ''
- if keyword.find(geo[2].lower()) >= 0:
- geo[2] = ''
- keyword = keyword.title()
- kw = keyword + " in " + geo[1] + " " + geo[2] + " " + geo[3] + ", " + geo[0]
- kw = kw.replace(' ', ' ')
- return kw
- # In[367]:
- def add_post_queue(blog_url, blog_network_id):
- keyword = gen_keyword(db['keyword'], db['geo'])
- query = """
- mutation createPost(
- $data:PostCreateInput!
- ) {
- createPost(data: $data){
- id
- blogUrl
- keyword
- blogNetwork
- isPublished
- }
- }
- """
- variables = {
- "data": {
- "blogUrl": blog_url,
- "keyword": keyword,
- "blogNetwork": int(blog_network_id),
- "isPublished": False,
- "primaryKey": str(blog_network_id) + "_" + keyword
- }
- }
- data = {'query': query, 'variables': variables}
- data = json.dumps(data)
- r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
- res = r.content.decode('utf8')
- resJson = json.loads(res)
- return resJson['data']['createPost']
- # In[368]:
- def get_post_queue(blog_url):
- query = """
- query posts(
- $where: PostWhereInput!
- ) {
- posts(where: $where, first: 1) {
- id
- blogUrl
- keyword
- blogNetwork
- isPublished
- }
- }
- """
- variables = {
- "where": {
- "AND": {
- "blogUrl": blog_url,
- "isPublished": False
- }
- }
- }
- data = {'query': query, 'variables': variables}
- data = json.dumps(data)
- r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
- res = r.content.decode('utf8')
- resJson = json.loads(res)
- return resJson['data']['posts'][0]
- # In[369]:
- def update_post_published(id, post_url):
- query = """
- mutation updatePost(
- $data: PostUpdateInput!,
- $where: PostWhereUniqueInput!
- ) {
- updatePost(data: $data, where: $where) {
- id
- isPublished
- }
- }
- """
- variables = {
- "data": {
- "isPublished": True,
- "postUrl": post_url
- },
- "where": {
- "id": id
- }
- }
- data = {'query': query, 'variables': variables}
- data = json.dumps(data)
- r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
- res = r.content.decode('utf8')
- return res
- # In[370]:
- def update_blog_nextpostdate(id):
- query = """
- mutation updateMetadata(
- $data: MetadataUpdateInput!,
- $where: MetadataWhereUniqueInput!
- ) {
- updateMetadata(data: $data, where: $where) {
- id
- blogUrl
- nextPostDate
- }
- }
- """
- next_post_date = datetime.now() + timedelta(hours = random.randint(0, 24), minutes=random.randint(0, 60), seconds=random.randint(0, 60))
- variables = {
- "data": {
- "nextPostDate": str(next_post_date.isoformat())
- },
- "where": {
- "id": id
- }
- }
- data = {'query': query, 'variables': variables}
- data = json.dumps(data)
- r = requests.post(url=GQL_URL, data=data, headers=HEADERS)
- res = r.content.decode('utf8')
- return res
- # In[371]:
- def send_email(email, title, content):
- from_email = "email@a2hosting.com"
- to_email = email
- msg = MIMEMultipart('alternative')
- msg['Subject'] = title
- msg['From'] = from_email
- msg['To'] = to_email
- text = MIMEText(content, 'html')
- msg.attach(text)
- # print(msg)
- EMAIL_SERVER.sendmail(from_email, to_email, msg.as_string())
- # In[372]:
- def post_by_email(blog_id, blog_url, secret_email, blog_network_id):
- q = add_post_queue(blog_url, blog_network_id)
- title = clean_text(q['keyword'])
- content = gen_post(clean_text(q['keyword']), q['blogUrl'], q['blogNetwork']) + "#end"
- post_url = sanitize_url(title, datetime.now())
- print(post_url)
- print(q)
- print(secret_email)
- send_email(secret_email, title, content)
- update_post_published(q['id'], post_url)
- update_blog_nextpostdate(blog_id)
- # In[373]:
- def check_post_schedule(blog_networks):
- for blog_network in blog_networks:
- if datetime.now() > datetime.strptime(blog_network['nextPostDate'][:-5], '%Y-%m-%dT%H:%M:%S'):
- post_by_email(blog_network['id'], blog_network['blogUrl'], blog_network['secretEmail'], blog_network['blogNetwork'])
- # In[ ]:
- # def run_thread(n_thread = 10):
- # n = len(blog_networks)
- # f = lambda A, n=int(n/n_thread): [A[i:i+n] for i in range(0, len(A), n)]
- # thread_list = []
- # for sub_blog_network in f(blog_networks):
- # t = threading.Thread(target=check_post_schedule, args=(sub_blog_network,))
- # thread_list.append(t)
- # for thread in thread_list:
- # thread.start()
- # for thread in thread_list:
- # thread.join()
- # run_thread(10)
- check_post_schedule(blog_networks)
- # In[ ]:
- # EMAIL_SERVER.quit()
- # In[ ]:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement