My Python Script
from telethon import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import ImportChatInviteRequest
import json
import time
import asyncio
file_json = open("data.json")
data = json.loads(file_json.read())
api_id = MY_ID
api_hash = 'MY_HASH'
u1 = TelegramClient('u1', api_id, api_hash)
async def main():
me = await u1.get_me()
c1 = await u1.get_entity("#somechannel")
await u1(JoinChannelRequest(c1))
with u1:
u1.loop.run_until_complete(main())
My Json.File
{
"c1" : "#somechannel"
}
how to read data from json file to join channel c1
TIA sorry for bad english
nvm i solve my problem
#IMPORTING MODULE
from telethon import TelegramClient
from telethon.tl.functions.channels import JoinChannelRequest
from telethon.tl.functions.messages import ImportChatInviteRequest
from telethon.tl.functions.channels import LeaveChannelRequest
import time
import asyncio
import json
#API ID & API HASH
api_id = XXXXX
api_hash = 'XXXXXXXXXXXXXXXXXX'
#JSON LOADS
file_json = open("data.json")
data = json.loads(file_json.read())
#JSON LOADS VALUE
bot = (data["bot"])
ch1 = (data["ch1"])
ch2 = (data["ch2"])
ch3 = (data["ch3"])
ch4 = (data["ch4"])
ch5 = (data["ch4"])
ch6 = (data["ch4"])
ch7 = (data["ch4"])
ch8 = (data["ch4"])
ch9 = (data["ch4"])
ch10 = (data["ch4"])
#START USER 1
u01 = TelegramClient('u01', api_id, api_hash)
async def main():
me = await u01.get_me()
username = me.username
print("User Sekarang :", username)
with u01:
u01.loop.run_until_complete(main())
Related
The following is a screenshot of my code and database table data:
from sqlalchemy import Column, String, create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mssql+pymssql://sa:12345678#XXXX:1433/YYYY')
Base = declarative_base(engine)
class User(Base):
__tablename__ = 'Products'
Id = Column(String(20), primary_key=True)
ProductName = Column(String(20))
ProductDesc = Column(String(50))
CreationTime = Column(String(20))
ProductCategory = Column(String(50))
def test():
db_session = sessionmaker(bind=engine)
session = db_session()
user = session.query(User).filter(User.Id == 5).all()
print(user)
=========================
query results:[<main.User object at 0x7fd56b265400>]
I want it to return the specific values of all data that meet the filtering conditions.
So,what went wrong?
This is the product table mapped above.
class BaseModel(object):
__abstract__ = True
def __init__(self):
self.__mapper__ = None
def __repr__(self):
fmt = u'[{}]'
attrs = (
(k, str(getattr(self, k)).replace(' ', '')) for k in self.__mapper__.columns.keys()
)
sattrs = ','.join('{}={!r}'.format(*x) for x in attrs)
return fmt.format(sattrs)
Base = declarative_base(cls=BaseModel)
I want to save all data of both these classes (Product_Items and Variant_Product) as JSON output files.
getProductDetails() : In this function I want to extract the data for just 1st element in product_variants list and ading it to the dict(item_list) and for rest of the elements I am creating a req to hit the same function recursively untill I have all the keys in my dict(item_list).
At the end of the function I want to write the extracted data as JSON file, but I can't return two values from a function.
Similarly, in getListingDetails() function I need to save the item as JSON file. PLEASE HELP!!!
Following is the snippet:
import scrapy
from scrapy.http import Request
from scrapy.selector import Selector
from scrapy.item import Item, Field
import re,json
class Product_Items(Item):
Image_URL = Field()
Product_Title = Field()
Price = Field()
PPU_Price = Field()
Product_URL = Field()
Product_SKU = Field()
Product_UPC = Field()
class Variant_Product(Item):
Image_URL = Field()
Product_Title = Field()
Price = Field()
PPU_Price = Field()
Product_URL = Field()
Product_SKU = Field()
Product_UPC = Field()
Product_Size = Field()
Meta = Field()
class walmartSpider(scrapy.Spider):
name = "walmart"
start_urls = ['https://www.walmart.com/all-departments']
item_list = {}
def parse(self,response):
reqs = []
base_url='https://www.walmart.com/'
hxs = Selector(text=response.body)
json_response = hxs.xpath('//script[#id="home"]//text()').get()
data = json.loads(json_response)
cat_urls = self.getCategoryUrls(data)
for url in cat_urls:
if url[:7] == '/browse':
url = base_url + url
link=Request(url=url,callback=self.getListingDetails)
reqs.append(link)
return reqs
def getCategoryUrls(self,data):
.....
return final_cat_url
def getListingDetails(self,response):
reqs = []
hxs = Selector(text=response)
data = json.loads(hxs.xpath('//script[#id="searchContent"]//text()').get())
products = data['searchContent']['preso']['items']
item = Product_Items()
for product in products:
item['Image_URL'] = product['imageUrl']
item['Product_Title'] = product['title']
item['Product_URL'] = base_url + product['productPageUrl']
item['Product_SKU'] = product['productId']
item['Product_UPC'] = product['standardUpc'][0]
try:
item['PPU_Price'] = product['primaryOffer']['unitPriceDisplayCondition']
except:
item['PPU_Price'] = ''
try:
regular_price = product['primaryOffer']['offerPrice']
except:
regular_price = ''
if regular_price:
item['Price'] = product['primaryOffer']['offerPrice']
else:
product_req = Request(url=item['Product_URL'],callback=self.getProductDetails)
reqs.append(product_req)
**Want to save this item as JSON file**
**#Pagination**
try:
next_page = data['searchContent']['preso']['pagination']['next']['url']
except:
next_page = ''
if next_page:
next_page_url = str(re.findall(r'^[\S]+\?',response.url)[0])+str(next_page)
req = Request(url=next_page_url,callback=self.getListingDetails)
reqs.append(req)
return reqs
def getProductDetails(self,response):
reqs = []
base_url = 'https://www.walmart.com/ip/'
hxs = Selector(text=response)
variant = Variant_Product()
prod_data = json.loads(hxs.xpath('//script[#id="item"]//text()').get())
product_variants = prod_data['item']['product']['buyBox']['products']
for product_variant in product_variants[1:]:
item_id = product_variant['usItemId']
if item_id not in self.item_list.keys():
self.item_list[item_id] = ''
req = Request(url=base_url+str(item_id),callback=self.getProductDetails)
reqs.append(req)
product_0 = prod_data['item']['product']['buyBox']['products'][0]
variant['Product_Title'] = product_0['productName']
variant['Product_SKU'] = product_0['walmartItemNumber']
variant['Product_UPC'] = product_0['upc']
variant['Product_Size'] = product_0['variants'][0]['value']
variant['Product_URL'] = product_0['canonicalUrl ']
variant['Price'] = product_0['priceMap']['price']
variant['PPU_Price'] = product_0['priceMap']['unitPriceDisplayValue']
variant['Meta'] = (product_0['categoryPath']).replace('Home Page/','')
**Want to save this item as JSON file**
return reqs
According to the scrapy docs, there are several built in "Exporters" that can serialize your data into several different formats (including JSON).
You should be able to do something like:
# ...
from scrapy.exporters import JsonItemExporter
# ...
def getListingDetails(self, response):
# ...
for product in products:
item = Product_Items(
Image_URL = product['imageUrl'],
Product_Title = product['title'],
Product_URL = base_url + product['productPageUrl'],
Product_SKU = product['productId'],
Product_UPC = product['standardUpc'][0],
PPU_Price = product.get('primaryOffer', {}).get('unitPriceDisplayCondition', ''),
Price = product.get('primaryOffer', {}).get('offerPrice', '')
)
if not item['Price']:
product_req = Request(url=item['Product_URL'],callback=self.getProductDetails)
reqs.append(product_req)
JsonItemExporter(open(f"{item['Product_SKU']}.json", "wb")).export_item(item)
Some notes:
The JsonItemExporter.__init__ method expects a file-like object whose write method accepts bytes, which is why the "wb"
dict.get() in Python allows you to specify a default value as the second argument, in case a key doesn't exist (not strictly necessary here, but reduces the try/except logic)
When handling exceptions, it's recommended by PEP8 standards to catch more specific exception types (in the above cases, except KeyError: might be appropriate) than just a bare except clause
Please let me know if the above works for you!
I'm trying to read the coordinates from a MySQL database with Python and reproduce them on a map with Folium.
But I noticed that only the last of the 43 records are output and entered into the map and I don't know why.
I have the feeling that I can't see the wood for the trees anymore.
Maybe you can help me with how to read out all 43 data sets?
I have the complete code below including a screenshot of what this code outputs:
import folium
import mysql
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="firmen"
)
if(mydb):
print("Verbindung erfolgreich")
else:
print("Verbindung fehlgeschlagen")
cursor = mydb.cursor()
cursor.execute("SELECT * from leipzig")
result = cursor.fetchall()
cursor.close()
#13 = Longitude and 12 = Latitude
for data in result:
ID = data[0]
name = data[1]
lon = data[13]
lat = data[12]
mydb.close()
print("Verbindung geschlossen")
# Create a Map instance
mymap = folium.Map(location=[51.268360, 12.419357], tiles='stamentoner',
zoom_start=10, control_scale=True)
tooltipMeta = ID, name
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
folium.TileLayer('stamenwatercolor').add_to(mymap)
folium.LayerControl().add_to(mymap)
# Display the map
mymap
#sentence & #borisdonchev are right, the part where
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
should become
for data in result:
ID = data[0]
name = data[1]
lon = data[13]
lat = data[12]
tooltipMeta = ID, name
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
I first had to create the map and then access the database.
So I managed to create the markers inside the loop
import folium
import mysql
import mysql.connector
mymap = folium.Map(location=[51.268360, 12.419357], tiles='stamentoner',
zoom_start=10, control_scale=True)
folium.TileLayer('stamenwatercolor').add_to(mymap)
folium.LayerControl().add_to(mymap)
mydb = mysql.connector.connect(
host="localhost",
user="root",
passwd="",
database="firmen"
)
if(mydb):
print("Verbindung erfolgreich")
else:
print("Verbindung fehlgeschlagen")
cursor = mydb.cursor()
cursor.execute("SELECT * from leipzig")
result = cursor.fetchall()
cursor.close()
mydb.close()
print("Verbindung geschlossen")
#13 = Longitude and 12 = Latitude
for data in result:
ID = data[0]
name = data[1]
lon = data[13]
lat = data[12]
tooltipMeta = ID, name
folium.Marker([lon,lat], tooltip=tooltipMeta).add_to(mymap)
# Display the map
mymap
Script working completely but its not entering any data.
Here my code:
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import datetime
from login_credentials import *
from common_file import *
from selenium.webdriver.firefox.options import Options
from pyvirtualdisplay import Display
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
start_time = str(sdate)+" "+ str(stime)
end_time = str(edate)+" "+ str(etime)
options = Options()
options.headless = True
driver = webdriver.Firefox(executable_path='/usr/bin/geckodriver',options=options)
driver.get("https://www.goeventz.com/")
driver.find_element_by_xpath("//a[contains(text(),'Login')]").click()
print("going")
#driver.find_element_by_id("userlogin")
driver.find_element_by_id("user_email").send_keys(ge_email)
driver.find_element_by_id("password").send_keys(ge_pswd)
#driver.find_elements_by_class_name(".btn-login").click()
#driver.find_element_by_css_selector('btn-login').click()
driver.find_element_by_xpath("//button[#type='submit']").click()
driver.find_element_by_xpath("//a[contains(text(),'Create Event') and #id='headerbtn']").click()
driver.find_element_by_name("title").clear()
driver.find_element_by_name("title").send_keys(eventname)
driver.find_element_by_xpath("//a[contains(text(),'Enter Address')]").click()
driver.find_element_by_xpath("//input[contains(#name,'venue_name')]").send_keys(full_address)
driver.find_element_by_name("start_date_time").clear()
driver.find_element_by_name("start_date_time").send_keys(start_time)
driver.find_element_by_name("end_date_time").clear()
driver.find_element_by_name("end_date_time").send_keys(end_time)
driver.find_element_by_id("fileToUpload").send_keys("/var/www/html/crons/event_posting/manual/test.jpg")
driver.find_element_by_xpath("//div[contains(#class,'fr-element fr-view')]").send_keys('description')
select = Select(driver.find_element_by_name("booknow_button_value"))
select.select_by_value('Register')
select = Select(driver.find_element_by_name("category"))
select.select_by_value("Sports")
select = Select(driver.find_element_by_name("othercategory"))
select.select_by_value('Festival')
driver.find_element_by_name("support_mobile").send_keys(cont_number)
driver.find_element_by_name('support_email').send_keys(email_id)
driver.find_element_by_name("makeeventlive").click()
print("its complted")
and it running completly on server, this is output:
but its not entering any data as provided it just output it blank.
here the output im getting on browser:
output on browser
this is common_file:
from dbconnection import get_conn
from datetime import datetime
connection_object, cursor = get_conn()
json_0 = []
json12_in_list = []
json_12 = []
json34_in_list = []
json_34 = []
json5 = []
json678_in_list = []
json_678 = []
json9 = []
json10 = []
main_json = {}
event_details = ''
with open('event_details.txt', 'r') as f:
event_details = f.read()
event_id = int(event_details.split(',')[0])
site_id = int(event_details.split(',')[1])
site_name = str(event_details.split(',')[2])
#event_id =
sql = """SELECT * FROM articles2 WHERE id ='%d'""" %event_id
cursor.execute(sql)
data = cursor.fetchall()
for info in data:
eventid = info[0]
countryname = info[1]
eventname = info[2]
profileimg = info[5]
banner0 = info[6]
sdate = str(info[7])[:10]
edate = str(info[8])[:10]
addr1 = info[9]
addr2 = info[10]
pincode = info[11]
full_address = info[15]
state = info[12]
city = info[13]
stime = str(info[18])
#s_time = datetime.strptime(stime,"%H:%M:%S")
#stime = s_time.strftime("%I:%M:%S %p")
etime = str(info[19])
# e_time = datetime.strptime(etime,"%H:%M:%S")
# etime = e_time.strftime("%I:%M:%S %p")
description = info[20]
src_url = info[26]
json0 = {"event id":eventid, "country":countryname, "event name":eventname, "profile image":profileimg, "banner":banner0, "start date":sdate,
"end date":edate, "address 1":addr1, "address 2":addr2, "pincode":pincode, "full address":full_address, "state":state, "city":city,
"start time":stime, "end time":etime, "description":description, "source url":src_url}
json_0.append(json0)
main_json['event info'] = json_0
#tickets
sql1 = """SELECT * FROM tickets WHERE event_id = '%d'""" %event_id
cursor.execute(sql1)
data1 = cursor.fetchall()
for info1 in data1:
tktid = info1[0]
eventid1 = info1[1]
tktname = info1[2]
original_tkt_price = info1[3]
other_charges = info1[4]
other_charges_type = info1[5]
tkt_qty = info1[6]
min_qty = info1[7]
max_qty = info1[8]
qty_left = info1[9]
ticket_msg = info1[10]
ticket_start_date = str(info1[11])[:10]
ticket_start_time = str(info1[11])[11:]
expiry_date = str(info1[12])[:10]
expiry_time = str(info1[12])[11:]
ticket_label= info1[13]
active1 = info1[14]
..........................................................................
I am tried to retrieve entry_time from mysql table (named user) and then find the time difference between now (exitTime) and the entry_time. My code doesn't seem to recover the data from mysql table. f is the id search key for the code which searches against the ID (PRIMARY KEY) of the mysql database to find the corresponding entry_time.
I have tried taking it as a string as well to see if i could retrieve the value but to no avail.
from tkinter import *
import time,datetime
import mysql.connector as mc
from tkinter import messagebox
import cv2
import matplotlib.pyplot as plt
import sys
import time
def billCalc():
global exitTime
global EntryTime
EntryTime = datetime.datetime
exitTime = datetime.datetime.now()
try:
conn = mc.connect(host='localhost', user='root', password='', db='car_park_master')
except mc.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
sql_Query = "SELECT `entry_time` FROM `user` WHERE `ID` ="+f.get()
#id = (f.get(),)
print(record[1])
cursor = conn.cursor(buffered=True)
cursor.execute(sql_Query, id)
record = cursor.fetchone()
# selecting column value into varible
EntryTime = datetime(record[1])
print(record[1])
conn.close()
print(exitTime)
print(EntryTime)
print(f.get())
BillTime = EntryTime - exitTime
Bill = BillTime * 2
print(Bill.get())
def main_screen():
screen = Tk()
screen.geometry("300x400")
screen.title("EXIT GATE")
global f
f = StringVar()
Entry(screen, textvariable=f).place(x=150, y=200)
print(f.get())
Button(screen,text="Exit",height='2',width='15',command=billCalc).place(x=150,y=300)
screen.mainloop()
main_screen()
It is supposed to take an exit time using datetime.datetime.now() which it does.
Then it is supposed to take input ID from the user to search in the database.
After that it is supposed to retrieve the corresponding entry time
Find the difference between entrytime and exit time in seconds
Then provide a bill.
EDITED AND FIXED CODE:
from tkinter import *
import datetime
import mysql.connector as mc
from tkinter import messagebox
import cv2
import matplotlib.pyplot as plt
import sys
import time
def billCalc():
global exitTime
global EntryTime
EntryTime = datetime.datetime
exitTime = datetime.datetime.now()
try:
conn = mc.connect(host='localhost', user='root', password='', db='car_park_master')
except mc.Error as e:
print("Error %d: %s" % (e.args[0], e.args[1]))
sys.exit(1)
cursor = conn.cursor()
cursor.execute("SELECT entry_time FROM user where id="+f.get())
record = cursor.fetchone()
EntryTime = (record[0])
conn.close()
print(exitTime)
print(EntryTime)
print(f.get())
BillTime = exitTime - EntryTime
print(BillTime)
def main_screen():
screen = Tk()
screen.geometry("300x400")
screen.title("EXIT GATE")
global f
f = StringVar()
Entry(screen, textvariable=f).place(x=150, y=200)
print(f.get())
Button(screen,text="Exit",height='2',width='15',command=billCalc).place(x=150,y=300)
screen.mainloop()
main_screen()